Installation
-
1 $ docker pull redis
- 2.1 $ docker run –name some-redis -d redis
- This image includes EXPOSE 6379 (the redis port), so standard container linking will make it automatically available to the linked containers (as the following examples illustrate).
- 2.2 $ docker run –name some-redis -d redis redis-server –appendonly yes
- If persistence is enabled, data is stored in the VOLUME /data, which can be used with –volumes-from some-volume-container or -v /docker/host/dir:/data
- 3 docker run –name some-app –link some-redis:redis -d application-that-uses-redis
Common Command
redis-cli
1.String
| command | description |
|---|---|
| Set name ray | |
| Get name | |
| Setnx name ray | if name exists, return 0, otherwise, 1 |
| Mset name ray age 23 salary 6666666 | set multiple keys |
| Msetnx name lee age 23 hoby basketball | if any attr exists, return 0, otherwise, 1 |
| Mget name age salary | get multiple keys |
| Getset name lee | get name & set name |
| Setrange name 3 lee | replace the first 3 letters |
| Getrange name 3 6 | get substring from position 3~6 |
| Append name .com | |
| Incr age | +1 |
| Incrby age 6 | +6 |
| Decr age | -1 |
| Decrby age 6 | -6 |
| Strlen name | string length |
2.Hash
| command | description |
|---|---|
| Hset user:001 name ray | |
| Hsetnx user name lee | |
| Hget user:001 name | |
| Hmset user:003 name lee age 23 | set multiple attrs |
| Hmget user:003 name age | get multiple attrs |
| Hincrby user:003 age 3 | +3 |
| Hexists user:003 name | check if there exists name |
| Hlen user:003 | return length |
| Hkeys user:003 | return keys |
| Hvals user:003 | return vals |
| Hgetall user:003 | return keys & vals |
| Hdel user:003 name | del attr |
3.list
| command | description |
|---|---|
| Lpush list1 “world” | left push |
| Lrange list1 0 -1 | read all list from left side |
| rpush list2 “ray” | |
| Linsert list3 before “ray” “new item” | insert before |
| Linsert list3 after “ray” “new item” | insert after |
| Lset list5 0 “new value” | reset a existing attr in list5 |
| Lrem list6 1 “ray” | remove a value |
| Ltrim list7 1 2 | remove values except the values in the position 0 & 1 |
| Lpop list8 | left pop |
| rpop list8 | |
| Rpoplpush list1 list2 | r pop value list1, left push it to list2 |
| lindex list 0 | return the value in the position 0 |
| llen list | length |
4.set
unsorted
| command | description |
|---|---|
| sadd set1 ray | |
| srem set1 ray | |
| smembers set1 | |
| spop set1 | pop value randomly |
| sdiff set1 set2 | show diff values |
| Sdiffstore set3 set1 set2 | set3 store the diff values of set1&set2 |
| sinter set1 set2 | intersection of set1 & set2 |
| sinterstore destination key [key …] | store intersection |
| sunion set1 set2 | union set |
| sunionstore set3 set1 set2 | |
| smove set1 set2 member | remove member from set1 to set2 |
| Scard set1 | size of set1 |
| sismember set1 “value” | whether “value” exists |
| srandmember set1 2 | read 2 values from set1 randomly |
sorted set
| command | description |
|---|---|
| Zadd sortedset1 1 ray1 | zadd setname weight(index no.) value |
| zrange sortedset1 0 -1 [withscores] | withscores is optional |
| Zrem myzsent ray1 | |
| zincrby sortedset1 10 “ray1” | |
| zrank sortedset1 ray3 | get position |
| zrevrank sortedset1 ray3 | inverted order position |
| zrangebyscore sortedset1 0 100 withscores | return values (index/weight 0~100) |
| Zcount sortedset1 0 100 | return # of values(index/weight 0~100) |
| zcard sortedset1 | return the size of sortedset1 |
| zremrangebyrank sortedset1 0 1 | remove value from 0 to 1 |
| zremrangebyscore sortedset1 1 20 | Remove all members in a sorted set within the given scores |
5 Redis Command
- Keys-Vals
| No. | command | description |
|---|---|---|
| 1 | keys * | keys pattern |
| 2 | exists set1 | exists key [key …] |
| 3 | del set1 | del key [key …] |
| 4 | expire set1 120 | Set a key’s time to live in seconds |
| 5 | ttl set1 | Get the time to live for a key |
| 6 | select database | Change the selected database for the current connection |
| 7 | move key dababase1 | move key into database1 |
| 8 | persist set1 | Remove the expiration from a key |
| 9 | randomkey | Return a random key from the keyspace |
| 10 | rename set1 set01 | rename |
| 11 | type list1 | Determine the type stored at key |
- Server
| No. | command | description |
|---|---|---|
| 1 | ping | Ping the server |
| 2 | echo message | output directly |
| 3 | select 2 | select [0-16] |
| 4 | quit/exit | Ping the server |
| 5 | dbsize | Return the number of keys in the selected database |
| 6 | info | sys info |
| 7 | CONFIG GET | |
| 8 | flushdb | clear selected db |
| 9 | flushall | clear all db |
6 Redis: Advanced Cmd
- config file
| No. | command | description |
|---|---|---|
| 1 | redis-server /path/to/redis.conf | to specify a config file |
| 2 | requirepass password | add password |
| 3 | master & slave | many to many(a slave can access other slaves) |
| 4 | save 500 32 | snapshotting:save within 500 sec, 32 keys are changed |
- Transaction
| No. | command | description |
|---|---|---|
| 1 | multi | Mark the start of a transaction block |
| 2 | exec | Execute all commands issued after MULTI |
| 3 | discard | Discard all commands issued after MULTI |
- Subscribe & Publish
| No. | command | description |
|---|---|---|
| 1 | Subscribe tv1 tv2 | |
| 2 | Publish tv1 “can you see it?” |