Redis Crash Course [ ريدس بالعربى ] Redis in ARABIC

Redis Crash Course [ ريدس بالعربى ] Redis in ARABIC

3.376 Lượt nghe
Redis Crash Course [ ريدس بالعربى ] Redis in ARABIC
### Redis: Is described as an in-memory persistent key-value store * In Redis, databases are simply identified by a number with the default database being number 0 * Change the database using `select 0` * Get all keys `keys *` `set key value` `set users:leto '{"name": "leto", "planet": "dune", "likes": ["spice"]}'` `get key` `get users:leto` * Erase all the values in a database using `flushdb` ### Querying: Redis doesn’t allow you to query an object’s values, Redis isn’t a one- size-fits-all solution ### Memory and Persistence * (Snapshotting) By default, Redis will save the database every 60 seconds if 1000 or more keys have changed all the way to 15 minutes if 9 or less keys has changed * (Append Mode) Any time a key changes, an append-only file is updated on disk ### The Data Structures #### Strings * `set key value` * `get key` * `del key` * `set users:leto '{"name": leto, "planet": dune, "likes": ["spice"]}'` * `get users:leto` * `del users:leto` * Get the length of a key’s value `strlen users:leto` * Sub-string with inclusive end `getrange key start end` * `getrange users:leto 31 48` * `append key value` appends the value to the existing value (or creates it if it doesn’t exist already) * `append users:leto " OVER 9000!!"` * `incr key`, `incrby key number`, `decr key`, `decrby key number` * `incr stats:page:about`, `incrby stats:page:about 3`, `decr stats:page:about`, `decrby stats:page:about 3` ### REDIS BITMAPS – FAST, EASY, REALTIME METRICS * `setbit key offset bit` * `getbit key offset` * `setbit daily_active_users 1 1` * `getbit daily_active_users 1` #### Hashes * They provide an extra level of indirection: a field * The benefit would be the ability to pull and update/delete specific pieces of data, without having to get or write the entire serialized value. * `hset key hash_key hash_value` * `hget key hash_key` * `hmset key hash_key_1 hash_value_1 hash_key_2 hash_value_2 ... hash_key_n hash_value_n` * `hmget key hash_key_1 hash_key_2 ... hash_key_n` * `hgetall key` * `hdel key hash_key` * `hkeys key` * `hset users:goku powerlevel 9000` * `hget users:goku powerlevel` * `hmset users:goku race saiyan age 737` * `hmget users:goku race age` * `hgetall users:goku` * `hdel users:goku age` * `hkeys users:goku` #### Lists * Lists let you store and manipulate an array of values for a given key. You can add values to the list, get the first or last value and manipulate values at a given index. * l is the head and r is the tail * `lpush key value` * `rpush key value` * `lpop key` * `rpop key` * `ltrim key start end` * `lrange key start end` * `lrange key 0 -1` * `lindex key index` * `lpush newusers user1` * `ltrim newusers 0 1` * `lpush newusers user2` * `ltrim newusers 0 1` * `lrange newusers 0 1` * `lpush newusers user3` * `lrange newusers 0 2` * `ltrim newusers 0 1` * `lrange newusers 0 2` * If we wanted to get the details of the last 10 users, we’d do the following combination: ```ruby ids = redis.lrange('newusers', 0, 9) redis.mget(*ids.map {|u| "users:#{u}"}) ```` * You could use lists to store logs or track the path a user is taking through a site. If you were building a game, you might use one to track queued user actions. #### Sets * Sets are used to store unique values and provide a number of set-based operations, like unions. Sets aren’t ordered but they provide efficient value-based operations. * `sadd key value_1 value_2 ... value_n` * `sadd friends:leto ghanima paul chani jessica` * `sadd friends:duncan paul jessica alia` * O(1) to check if a value is a member of a set * `sismember key value` * `smembers key` * `srem key value` * `sinter key_1 key_2` * `sinterstore new_key key_1 key_2` * `sismember friends:leto Jessica` * `sismember friends:leto vladimir` * `sinter friends:leto friends:duncan` * `sinterstore friends:leto_duncan friends:leto friends:duncan` * Sets are great for tagging or tracking any other properties of a value for which duplicates don’t make any sense (or where we want to apply set operations such as intersections and unions). #### Sorted Sets * If hashes are like strings but with fields, then sorted sets are like sets but with a score. The score provides sorting and ranking capabilities. * `zadd key score_1 value_1 score_2 value_2 ... score_n value_n` * `zcount key start_score end_score` * `zrank key value` * `zrevrank key value` * `zrange key index_start index_end withscores` * `zrangebyscore key score_start score_end withscores` * `zrem key value` * `zadd friends:duncan 70 ghanima 95 paul 95 chani 75 jessica 1 vladimir` * `zcount friends:duncan 90 100` * `zrank friends:duncan vladimir` * `zrevrank friends:duncan vladimir` * The most obvious use-case for sorted sets is a leaderboard system. In reality though, anything you want sorted by some integer, and be able to efficiently manipulate based on that score, might be a good fit for a sorted set.