set 不重复
sadd
SADD key member [member ...] summary: Add one or more members to a set since: 1.0.0 group: set 127.0.0.1:6379> sadd s_set 1 3 2 4 2 (integer) 4 127.0.0.1:6379> SMEMBERS s_set 1) "1" 2) "2" 3) "3" 4) "4"
scard s_set #获取元素个数
sdiff
SDIFF key [key ...] summary: Subtract multiple sets since: 1.0.0 group: set 127.0.0.1:6379> sdiff s_set s_set2 # s_set有,s_set2没有 1) "1" 2) "2"
SDIFFSTORE
SDIFFSTORE destination key [key ...] summary: Subtract multiple sets and store the resulting set in a key since: 1.0.0 group: set 127.0.0.1:6379> SDIFFSTORE s_set3 s_set s_set2 (integer) 2 127.0.0.1:6379> SMEMBERS s_set3 1) "1" 2) "2"
sinter 交集 ,参数是多个key
sinter s_set s_set2
SINTERSTORE
SINTERSTORE destination key [key ...] summary: Intersect multiple sets and store the resulting set in a key since: 1.0.0 group: set
SISMEMBER
SISMEMBER key member summary: Determine if a given value is a member of a set #是返回1 since: 1.0.0 group: set
smove
SMOVE source destination member summary: Move a member from one set to another since: 1.0.0 group: set
spop
SPOP key [count] summary: Remove and return one or multiple random members from a set since: 1.0.0 group: set
SRANDMEMBER
SRANDMEMBER key [count] summary: Get one or multiple random members from a set since: 1.0.0 group: set
srem
SREM key member [member ...] summary: Remove one or more members from a set since: 1.0.0 group: set
sunion 并集
SUNION key [key ...] summary: Add multiple sets since: 1.0.0 group: set
SUNIONSTORE
SUNIONSTORE destination key [key ...] summary: Add multiple sets and store the resulting set in a key since: 1.0.0 group: set
sscan
sortedset 有序集合
zadd
ZADD key [NX|XX] [CH] [INCR] score member [score member ...] summary: Add one or more members to a sorted set, or update its score if it already exists since: 1.2.0 group: sorted_set
zrange
ZRANGE key start stop [WITHSCORES] summary: Return a range of members in a sorted set, by index since: 1.2.0 group: sorted_set
zrank
ZRANK key member summary: Determine the index of a member in a sorted set since: 2.0.0 group: sorted_set
其它操作:
del
DEL key [key ...] summary: Delete a key since: 1.0.0 group: generic
keys
KEYS pattern summary: Find all keys matching the given pattern since: 1.0.0 group: generic
expire
EXPIRE key seconds summary: Set a key's time to live in seconds since: 1.0.0 group: generic
rename
RENAME key newkey summary: Rename a key since: 1.0.0 group: generic
move
MOVE key db summary: Move a key to another database since: 1.0.0 group: generic
RANDOMKEY
RANDOMKEY - summary: Return a random key from the keyspace since: 1.0.0 group: generic
connection
select #共 16 个
SELECT index summary: Change the selected database for the current connection since: 1.0.0 group: connection
python
import redis #连接池 # pool = redis.ConnectionPool(host='localhost',port=6379,db=0) # r = redis.Redis(connection_pool=pool) #pipe pool = redis.ConnectionPool(host='localhost',port=6379,db=0) r = redis.Redis(connection_pool=pool) pipe = r.pipeline(transaction=True) pipe.set('name','lyb') pipe.set('age','12') pipe.execute()
发布订阅:
#redis_p_s.py import redis class RedisPS(object): def __init__(self): pool = redis.ConnectionPool(host='localhost', port=6379, db=0) self._conn = redis.Redis(connection_pool=pool) self.chan_pub = 'ps' self.chan_sub = 'ps' def r_pub(self,msg): self._conn.publish(self.chan_pub,msg) return True def r_sub(self): pub = self._conn.pubsub() pub.subscribe(self.chan_sub) pub.parse_response() return pub
#publisher.py from redis_p_s import RedisPS obj = RedisPS() while True: msg = input('msg: ') obj.r_pub(msg) if msg == 'q': break
#subscriber.py from redis_p_s import RedisPS obj = RedisPS() s = obj.r_sub() print('recv: ') while True: msg = s.parse_response() #阻塞监听 print('>> ',msg[2].decode())
持久化配置:
sudo gedit /etc/redis/redis.conf
################################ SNAPSHOTTING(快照) ################################ # # Save the DB on disk: # # save <seconds> <changes> # # Will save the DB if both the given number of seconds and the given # number of write operations against the DB occurred. # # In the example below the behaviour will be to save: # after 900 sec (15 min) if at least 1 key changed # after 300 sec (5 min) if at least 10 keys changed # after 60 sec if at least 10000 keys changed # # Note: you can disable saving completely by commenting out all "save" lines. # # It is also possible to remove all the previously configured save # points by adding a save directive with a single empty string argument # like in the following example: # # save "" save 900 1 save 300 10 save 60 10000
手动保存,即将数据快照保存到硬盘
save 同步保存操作(不推荐,因为会阻塞所有客户端)
bgsave 后台异步保存操作 (推荐,redis进程继续处理用户请求,子进程负责保存数据)
lastsave 查询保存情况