redis数据类型及支持的常用操作:
string
set/get/append/incr/decr/getrange/mget/mset/strlen
127.0.0.1:6379> set usersession:1 13323232323 OK 127.0.0.1:6379> get usersession:1 "13323232323" 127.0.0.1:6379> incr usersession:1 (integer) 13323232324 get usersession:1 // 13323232324 127.0.0.1:6379> append usersession:1 "extra data" (integer) 21 127.0.0.1:6379> get usersession:1 "13323232324extra data" 127.0.0.1:6379> set customer:1 "customer00123" OK 127.0.0.1:6379> getrange customer:1 8 13 "00123"
127.0.0.1:6379> strlen customer:1
(integer) 13
127.0.0.1:6379> mset order:1 "order1 data" order:2 "order2 data" OK 127.0.0.1:6379> mget order:1 order:2 1) "order1 data" 2) "order2 data"
list : order list
just a sorted list of strings, sorted by insertion order
可以执行以下操作:
lpush/rpush/lrem/lset/lindex/lrange/llen/lpop/rpop/ltrim
127.0.0.1:6379> lpush recentcomments "comment 1" (integer) 1 127.0.0.1:6379> lrange recentcomments 0 1 1) "comment 1" 127.0.0.1:6379> lpush recentcomments "comment 2" (integer) 2 127.0.0.1:6379> lrange recentcomments 0 2 1) "comment 2" 2) "comment 1" 127.0.0.1:6379> rpush recentcomments "comment 3" (integer) 3 127.0.0.1:6379> lrange recentcomments 0 3 1) "comment 2" 2) "comment 1" 3) "comment 3" 127.0.0.1:6379> rpush recentcomments "comment 4" (integer) 4 127.0.0.1:6379> rpush recentcomments "comment 5" (integer) 5 127.0.0.1:6379> lrange recentcomments 0 6 1) "comment 2" 2) "comment 1" 3) "comment 3" 4) "comment 4" 5) "comment 5" 127.0.0.1:6379> rpush recentcomments "comment 6" (integer) 6 127.0.0.1:6379> lrange recentcomments 0 6 1) "comment 2" 2) "comment 1" 3) "comment 3" 4) "comment 4" 5) "comment 5" 6) "comment 6" 127.0.0.1:6379> ltrim recentcomments 0 4 OK 127.0.0.1:6379> lrange recentcomments 0 6 1) "comment 2" 2) "comment 1" 3) "comment 3" 4) "comment 4" 5) "comment 5" 127.0.0.1:6379> 127.0.0.1:6379> lindex recentcomments 2 "comment 3" 127.0.0.1:6379> lrange recentcomments 0 5 1) "comment 2" 2) "comment 1" 3) "comment 3" 4) "comment 4" 5) "comment 5" 127.0.0.1:6379> lpop recentcomments "comment 2" 127.0.0.1:6379> lrange recentcomments 0 5 1) "comment 1" 2) "comment 3" 3) "comment 4" 4) "comment 5" 127.0.0.1:6379> lpop recentcomments "comment 1" 127.0.0.1:6379> lrange recentcomments 0 5 1) "comment 3" 2) "comment 4" 3) "comment 5" 127.0.0.1:6379> rpop recentcomments "comment 5" 127.0.0.1:6379> lrange recentcomments 0 5 1) "comment 3" 2) "comment 4" 127.0.0.1:6379> redis> RPUSH mylist "hello" (integer) 1 redis> RPUSH mylist "hello" (integer) 2 redis> RPUSH mylist "foo" (integer) 3 redis> RPUSH mylist "hello" (integer) 4 redis> LREM mylist -2 "hello" (integer) 2 redis> LRANGE mylist 0 -1 1) "hello" 2) "foo"
sets:
sets contain a collection of unique strings
sadd/scard/sdiff/sinter/sunion/sismember/smembers/smove/srem
127.0.0.1:6379> sadd post:1:likes "joe" "bob" "mary" (integer) 3 127.0.0.1:6379> scard post:1:likes (integer) 3 127.0.0.1:6379> smembers post:1:likes 1) "joe" 2) "bob" 3) "mary" 127.0.0.1:6379> sadd post:2:likes "joe" "tim" (integer) 2 127.0.0.1:6379> sdiff post:1:likes post:2:likes 1) "bob" 2) "mary" 127.0.0.1:6379> sinter post:1:likes post:2:likes 1) "joe" 127.0.0.1:6379>
sorted sets:
zadd,zcard,zcount,zincrby,zrange,zrank,zrem,zscore
127.0.0.1:6379> zadd highscores 120 "joe" 100 "bob" 150 "mary" 104 "tim" (integer) 4 127.0.0.1:6379> zrange highscores 0 4 1) "bob" 2) "tim" 3) "joe" 4) "mary" 127.0.0.1:6379> zrange highscores 0 4 withscores 1) "bob" 2) "100" 3) "tim" 4) "104" 5) "joe" 6) "120" 7) "mary" 8) "150" 127.0.0.1:6379> zadd highscores 125 "joe" (integer) 0 127.0.0.1:6379> zrange highscores 0 4 withscores 1) "bob" 2) "100" 3) "tim" 4) "104" 5) "joe" 6) "125" 7) "mary" 8) "150"
hash
maps between string fields and string values
hset/hmset/hget/hmget/hgetall/hdel/hexists/hincrby/hkeys/hvals
pub and sub
redis can be used as a message bus
127.0.0.1:6379> subscribe greetings Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "greetings" 3) (integer) 1 1) "message" 2) "greetings" 3) "hello redis" 1) "message" 2) "greetings" 3) "hello redis again" psubscribe gree* // 則這種情況下會subscribe所有的gree // 另外一個窗口 C:Redis>redis-cli.exe 127.0.0.1:6379> publish greetings "hello redis" (integer) 1 127.0.0.1:6379> publish greetings "hello redis again" (integer) 1 127.0.0.1:6379>
transction