STRING
# INCR
127.0.0.1:6379> SET key1 1
OK
127.0.0.1:6379> INCR key1
(integer) 2
127.0.0.1:6379> GET key1
"2"
# DECR
127.0.0.1:6379> DECR key1
(integer) 1
127.0.0.1:6379> GET key1
"1"
# INCRBY
127.0.0.1:6379> INCRBY key1 1000
(integer) 1001
127.0.0.1:6379> GET key1
"1001"
# DECRBY
127.0.0.1:6379> DECRBY key1 9000
(integer) -7999
127.0.0.1:6379> GET key1
"-7999"
# INCRBYFLOAT
127.0.0.1:6379> INCRBYFLOAT key1 8.22
"-7990.7799999999999998"
127.0.0.1:6379> GET key1
"-7990.7799999999999998"
# APPEND
127.0.0.1:6379> SET key2 hello
OK
127.0.0.1:6379> APPEND key2 ,world
(integer) 11
127.0.0.1:6379> GET key2
"hello,world"
# GETRANGE
127.0.0.1:6379> GETRANGE key2 1 5
"ello,"
127.0.0.1:6379> GETRANGE key2 0 -1
"hello,world"
# SETRANGE
127.0.0.1:6379> SETRANGE key2 5 myroom
(integer) 11
127.0.0.1:6379> GET key2
"hellomyroom"
# GETBIT
127.0.0.1:6379> GETBIT key2 5
(integer) 0
# SETBIT
127.0.0.1:6379> GET key2
"hellomyroom"
127.0.0.1:6379> GETBIT key2 5
(integer) 0
127.0.0.1:6379> SETBIT key2 5 1
(integer) 0
127.0.0.1:6379> GETBIT key2 5
(integer) 1
127.0.0.1:6379> GET key2
"lellomyroom"
# BITOP
127.0.0.1:6379> SET key3 hello,world
OK
127.0.0.1:6379> SET key4 hello,myroom
OK
127.0.0.1:6379> BITOP AND dest-key key3 key4
(integer) 12
127.0.0.1:6379> GET dest-key
"hello,eirld\x00"
SETBIT に関して
“h” は ASCII コードによると 0x68 ( 0110 1000 )
“SETBIT key2 5 1” により、0x6C ( 0110 1100 ) へと変化し、0x6C は “l”。よって、“lellomyroom” となる。
LIST
# RPUSH LPUSH
127.0.0.1:6379> RPUSH list-key1 item
(integer) 1
127.0.0.1:6379> LPUSH list-key1 item1
(integer) 2
# LRANGE
127.0.0.1:6379> LRANGE list-key1 0 -1
1) "item1"
2) "item"
# RPOP LPOP
127.0.0.1:6379> RPOP list-key1
"item"
127.0.0.1:6379> LPOP list-key1
"item1"
# LINDEX
127.0.0.1:6379> RPUSH list-key2 item1 item2 item3 item4
(integer) 4
127.0.0.1:6379> LINDEX list-key2 2
"item3"
# LTRIM
127.0.0.1:6379> LRANGE list-key2 0 -1
1) "item1"
2) "item2"
3) "item3"
4) "item4"
127.0.0.1:6379> LTRIM list-key2 2 4
OK
127.0.0.1:6379> LRANGE list-key2 0 -1
1) "item3"
2) "item4"
# BLPOP
127.0.0.1:6379> BLPOP list-key4 100
( 下は別コンソールからの操作 )
127.0.0.1:6379> LPUSH list-key4 item1 item2 item3
(integer) 3
127.0.0.1:6379> BLPOP list-key4 100
1) "list-key4"
2) "item3"
(25.29s)
# RPOPLPUSH
127.0.0.1:6379> LRANGE list-key3 0 -1
1) "item2"
2) "item3"
3) "item4"
4) "item5"
127.0.0.1:6379> LRANGE list-key4 0 -1
1) "item2"
2) "item1"
127.0.0.1:6379> RPOPLPUSH list-key3 list-key4
"item5"
127.0.0.1:6379> LRANGE list-key3 0 -1
1) "item2"
2) "item3"
3) "item4"
127.0.0.1:6379> LRANGE list-key4 0 -1
1) "item5"
2) "item2"
3) "item1"
SET
# SADD
127.0.0.1:6379> SADD set-key item0 item1 item2 item3
(integer) 4
# SREM
127.0.0.1:6379> SREM set-key item2
(integer) 1
# SMEMBERS
127.0.0.1:6379> SMEMBERS set-key
1) "item3"
2) "item0"
3) "item1"
# SCARD
127.0.0.1:6379> SCARD set-key
(integer) 3
# SISMEMBER
127.0.0.1:6379> SISMEMBER set-key item0
(integer) 1
# SRANDMEMBER
127.0.0.1:6379> SRANDMEMBER set-key 2
1) "item0"
2) "item3"
127.0.0.1:6379> SRANDMEMBER set-key 2
1) "item3"
2) "item1"
# SPOP
127.0.0.1:6379> SPOP set-key 1
1) "item3"
127.0.0.1:6379> SMEMBERS set-key
1) "item0"
2) "item1"
# SMOVE
127.0.0.1:6379> SADD set-key2 item0 item1 item2
(integer) 3
127.0.0.1:6379> SADD set-key1 item100
(integer) 1
127.0.0.1:6379> SMOVE set-key1 set-key2 item100
(integer) 1
127.0.0.1:6379> SMEMBERS set-key2
1) "item0"
2) "item100"
3) "item1"
4) "item2"
# SDIFF
127.0.0.1:6379> SADD set-key3 item0 item1 item2 item100
(integer) 4
127.0.0.1:6379> SADD set-key4 item0
(integer) 1
127.0.0.1:6379> SADD set-key5 item1 item2
(integer) 2
127.0.0.1:6379> SDIFF set-key3 set-key4 set-key5
1) "item100"
# SDIFFSTORE
127.0.0.1:6379> SDIFFSTORE dist-key set-key3 set-key4 set-key5
(integer) 1
127.0.0.1:6379> SMEMBERS dist-key
1) "item100"
# SINTER
127.0.0.1:6379> SINTER set-key3 set-key4
1) "item0"
# SUNION
127.0.0.1:6379> SUNION set-key4 set-key5
1) "item0"
2) "item1"
3) "item2"
# SUNIONSTORE
127.0.0.1:6379> SUNIONSTORE dist-key2 set-key4 set-key5
(integer) 3
127.0.0.1:6379> SMEMBERS dist-key2
1) "item0"
2) "item1"
3) "item2"
HASH
# HMSET
127.0.0.1:6379> HMSET hash-key key1 value1 key2 value2 key3 value3 key4 value4
OK
# HMGET
127.0.0.1:6379> HMGET hash-key key1 key2 key3 key4
1) "value1"
2) "value2"
3) "value3"
4) "value4"
# HDEL
127.0.0.1:6379> HDEL sub-key1 sub-key2
(integer) 0
# HGETALL
127.0.0.1:6379> HGETALL hash-key
1) "key1"
2) "value1"
3) "key3"
4) "value3"
5) "key4"
6) "value4"
# HLEN
127.0.0.1:6379> HLEN hash-key
(integer) 3
# HEXISTS
127.0.0.1:6379> HEXISTS hash-key key1
(integer) 1
127.0.0.1:6379> HEXISTS hash-key key100
(integer) 0
# HKEYS
127.0.0.1:6379> HKEYS hash-key
1) "key1"
2) "key3"
3) "key4"
# HVALS
127.0.0.1:6379> HVALS hash-key
1) "value1"
2) "value3"
3) "value4"
# HINCRBY HINCRBYFLOAT
127.0.0.1:6379> HMSET hash-key key5 100
OK
127.0.0.1:6379> HINCRBY hash-key key5 200
(integer) 300
127.0.0.1:6379> HINCRBYFLOAT hash-key key5 5.5
"305.5"
ZSET
# ZADD
127.0.0.1:6379> ZADD zset-key 1 item1 2 item2 3 item3 4 item4
(integer) 4
# ZREM
127.0.0.1:6379> ZREM zset-key item2
(integer) 1
# ZCARD
127.0.0.1:6379> ZCARD zset-key
(integer) 4
# ZINCRBY
127.0.0.1:6379> ZINCRBY zset-key 10 item1
"11"
# ZCOUNT
127.0.0.1:6379> ZCOUNT zset-key 2 100
(integer) 3
# ZRANGE
127.0.0.1:6379> ZRANGE zset-key 0 -1
1) "item3"
2) "item4"
3) "item1"
4) "member0"
# ZRANK
127.0.0.1:6379> ZRANK zset-key item3
(integer) 0
# ZSCORE
127.0.0.1:6379> ZSCORE zset-key item1
"11"
127.0.0.1:6379> ZREVRANK zset-key item1
(integer) 0
# ZREVRANGE
127.0.0.1:6379> ZREVRANGE zset-key 0 -1
1) "item1"
2) "item4"
3) "item3"
# ZRANGEBYSCORE
127.0.0.1:6379> ZRANGEBYSCORE zset-key 0 20 WITHSCORES
1) "item3"
2) "3"
3) "item4"
4) "4"
5) "item1"
6) "11"
# ZREVRANGEBYSCORE
127.0.0.1:6379> ZREVRANGEBYSCORE zset-key 20 0 WITHSCORES
1) "item1"
2) "11"
3) "item4"
4) "4"
5) "item3"
6) "3"
# ZREMRANGEBYRANK
127.0.0.1:6379> ZREMRANGEBYRANK zset-key 2 6
(integer) 1
127.0.0.1:6379> ZRANGE zset-key 0 -1
1) "item3"
2) "item4"
# ZINTERSTORE
127.0.0.1:6379> ZADD zset-key2 1 item1 2 item2 3 item3
(integer) 3
127.0.0.1:6379> ZADD zset-key3 2 item2 3 item3 4 item4
(integer) 3
127.0.0.1:6379> ZINTERSTORE dest-key 2 zset-key2 zset-key3
(integer) 2
127.0.0.1:6379> ZRANGE dest-key 0 -1 WITHSCORES
1) "item2"
2) "4"
3) "item3"
4) "6"
# ZUNIONSTORE
127.0.0.1:6379> ZUNIONSTORE dest-key2 2 zset-key2 zset-key3
(integer) 4
127.0.0.1:6379> ZRANGE dest-key2 0 -1 WITHSCORES
1) "item1"
2) "1"
3) "item2"
4) "4"
5) "item4"
6) "4"
7) "item3"
8) "6"
PUB/SUB
27.0.0.1:6379> SUBSCRIBE channel1
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channel1"
3) (integer) 1
( 別のコンソールから )
127.0.0.1:6379> PUBLISH channel1 'Hello, subscriber!'
(integer) 1
127.0.0.1:6379> SUBSCRIBE channel1
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channel1"
3) (integer) 1
1) "message"
2) "channel1"
3) "Hello, subscriber!"
トランザクション処理
1 つのトランザクション ( 一連のコマンドセット ) が完全に終了するまで、他の処理を行われないようにする。
ex1) 何も処理を施さない場合
import redis
import threading
import time
conn = redis.Redis()
def notrans():
print conn.incr('notrans:')
time.sleep(.1)
conn.incr('notrans:', -1)
if 1:
for i in xrange(3):
threading.Thread(target=notrans).start()
time.sleep(.5)
$ python NoTransaction.py
1
2
3
-> DECR する前に他のスレッドによる INCR 処理がされてしまう。
ex2) トランザクションパイプラインを作成した場合
import redis
import threading
import time
conn = redis.Redis()
def trans():
pipeline = conn.pipeline()
pipeline.incr('trans:')
time.sleep(.1)
pipeline.incr('trans:', -1)
print pipeline.execute()[0]
if 1:
for i in xrange(3):
threading.Thread(target=trans).start()
time.sleep(.5)
$ python Transaction.py
1
1
1
-> 各スレッドの一連のトランザクション ( INCR 及び DECR ) が終了するまで他のコマンドを受け付けない。
有効期限
# EXPIRE TTL
127.0.0.1:6379> SET keyA valueA
OK
127.0.0.1:6379> EXPIRE keyA 20
(integer) 1
127.0.0.1:6379> TTL keyA
(integer) 13
127.0.0.1:6379> GET keyA
(nil)
# PERSIST
127.0.0.1:6379> SET keyA valueA
OK
127.0.0.1:6379> EXPIRE keyA 100
(integer) 1
127.0.0.1:6379> TTL keyA
(integer) 95
127.0.0.1:6379> PERSIST keyA
(integer) 1
127.0.0.1:6379> TTL keyA
(integer) -1
ベンチマーク
ベンチマークツール redis-benchmark を使ってみる。
$ redis-benchmark -c 1 -q
PING_INLINE: 30684.26 requests per second
PING_BULK: 31357.79 requests per second
SET: 31250.00 requests per second
GET: 30816.64 requests per second
INCR: 31191.52 requests per second
LPUSH: 31220.73 requests per second
RPUSH: 31269.54 requests per second
LPOP: 31220.73 requests per second
RPOP: 31181.79 requests per second
SADD: 31017.37 requests per second
SPOP: 30950.17 requests per second
LPUSH (needed to benchmark LRANGE): 31104.20 requests per second
LRANGE_100 (first 100 elements): 30618.49 requests per second
LRANGE_300 (first 300 elements): 30413.62 requests per second
LRANGE_500 (first 450 elements): 30441.40 requests per second
LRANGE_600 (first 600 elements): 30562.35 requests per second
MSET (10 keys): 30202.36 requests per second