{"id":1057,"date":"2023-03-24T09:47:07","date_gmt":"2023-03-24T01:47:07","guid":{"rendered":""},"modified":"2023-03-24T09:47:07","modified_gmt":"2023-03-24T01:47:07","slug":"Python redis","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1057.html","title":{"rendered":"Python redis"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Python redis<\/h1>\n

\u641e\u61c2Python redis\u7684\u57fa\u672c\u64cd\u4f5c<\/span>\n <\/div>\n

Redis\u8fde\u63a5<\/h2>\n
\n redis\u63d0\u4f9b\u4e24\u4e2a\u7c7bRedis\u548cStrictRedis\u7528\u4e8e\u5b9e\u73b0Redis\u7684\u547d\u4ee4\uff0cStrictRedis\u7528\u4e8e\u5b9e\u73b0\u5927\u90e8\u5206\u5b98\u65b9\u7684\u547d\u4ee4\uff0c\u5e76\u4f7f\u7528\u5b98\u65b9\u7684\u8bed\u6cd5\u548c\u547d\u4ee4\uff0cRedis\u662fStrictRedis\u7684\u5b50\u7c7b\uff0c\u7528\u4e8e\u5411\u540e\u517c\u5bb9\u65e7\u7248\u672c\u7684redis-py\u3002\n <\/div>\n
\n redis\u8fde\u63a5\u5b9e\u4f8b\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u53ef\u4ee5\u76f4\u63a5\u5c06redis\u8fde\u63a5\u5b9e\u4f8b\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u76f4\u63a5\u4f7f\u7528\u3002\u5982\u679c\u9700\u8981\u53e6\u4e00\u4e2aRedis\u5b9e\u4f8b\uff08or Redis\u6570\u636e\u5e93\uff09\u65f6\uff0c\u5c31\u9700\u8981\u91cd\u65b0\u521b\u5efaredis\u8fde\u63a5\u5b9e\u4f8b\u6765\u83b7\u53d6\u4e00\u4e2a\u65b0\u7684\u8fde\u63a5\u3002\u540c\u7406\uff0cpython\u7684redis\u6ca1\u6709\u5b9e\u73b0select\u547d\u4ee4\u3002\n <\/div>\n

\u8fde\u63a5\u6c60<\/h2>\n
\n redis-py \u4f7f\u7528 connection pool \u6765\u7ba1\u7406\u5bf9\u4e00\u4e2a redis server \u7684\u6240\u6709\u8fde\u63a5\uff0c\u907f\u514d\u6bcf\u6b21\u5efa\u7acb\u3001\u91ca\u653e\u8fde\u63a5\u7684\u5f00\u9500\u3002
\n
\u9ed8\u8ba4\uff0c\u6bcf\u4e2aRedis\u5b9e\u4f8b\u90fd\u4f1a\u7ef4\u62a4\u4e00\u4e2a\u81ea\u5df1\u7684\u8fde\u63a5\u6c60\u3002\u53ef\u4ee5\u76f4\u63a5\u5efa\u7acb\u4e00\u4e2a\u8fde\u63a5\u6c60\uff0c\u7136\u540e\u4f5c\u4e3a\u53c2\u6570 Redis\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5b9e\u73b0\u591a\u4e2a Redis \u5b9e\u4f8b\u5171\u4eab\u4e00\u4e2a\u8fde\u63a5\u6c60\u3002\n <\/div>\n
\n
 # \u5bfc\u5165redis <\/span>\u6a21\u5757
<\/span> import <\/span>redis <\/span>
pool = redis.ConnectionPool<\/span>(host='localhost'<\/span><\/span>, port=6379, decode_responses=True)
r = redis.Redis<\/span>(host='localhost', port=6379, decode_responses=True)
# \u8bbe\u7f6e name \u5bf9\u5e94\u7684\u503c
<\/span> r.set<\/span>('name'<\/span><\/span>, 'lidihuo'<\/span>)
# \u53d6\u51fa\u952e name \u5bf9\u5e94\u7684\u503c
<\/span> print(r.get<\/span>('name'))
<\/span><\/code><\/pre>\n<\/p><\/div>\n

redis \u57fa\u672c\u547d\u4ee4 String<\/h2>\n
\n set(name, value, ex=None, px=None, nx=False, xx=False)
\n
\u5728 Redis \u4e2d\u8bbe\u7f6e\u503c\uff0c\u9ed8\u8ba4\uff0c\u4e0d\u5b58\u5728\u5219\u521b\u5efa\uff0c\u5b58\u5728\u5219\u4fee\u6539\u3002\u53c2\u6570\u5982\u4e0b\uff1a\n <\/div>\n

ex - \u8fc7\u671f\u65f6\u95f4\uff08\u79d2\uff09<\/span>
\n px - \u8fc7\u671f\u65f6\u95f4\uff08\u6beb\u79d2\uff09<\/span>
\n nx - \u5982\u679c\u8bbe\u7f6e\u4e3aTrue\uff0c\u5219\u53ea\u6709name\u4e0d\u5b58\u5728\u65f6\uff0c\u5f53\u524dset\u64cd\u4f5c\u624d\u6267\u884c<\/span>
\n xx - \u5982\u679c\u8bbe\u7f6e\u4e3aTrue\uff0c\u5219\u53ea\u6709name\u5b58\u5728\u65f6\uff0c\u5f53\u524dset\u64cd\u4f5c\u624d\u6267\u884c<\/span> <\/p>\n

\n 1.ex - \u8fc7\u671f\u65f6\u95f4\uff08\u79d2\uff09 \u8fd9\u91cc\u8fc7\u671f\u65f6\u95f4\u662f3\u79d2\uff0c3\u79d2\u540ep\uff0c\u952efood\u7684\u503c\u5c31\u53d8\u6210None\n <\/div>\n
\n
 import <\/span>redis
pool = redis.ConnectionPool<\/span>(host='localhost'<\/span>, port=6379, decode_responses=True)
r = redis.Redis<\/span>(connection_pool=pool)
# key=\"food\",value=\"mutton\"\u5c06\u952e\u503c\u5bf9\u5b58\u5165redis\u7f13\u5b58
<\/span> r.set<\/span>('food'<\/span><\/span>, 'mutton'<\/span>, ex=3)
# mutton \u53d6\u51fa\u952efood\u5bf9\u5e94\u7684\u503c
<\/span> print(r.get<\/span>('food'))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 2.px - \u8fc7\u671f\u65f6\u95f4\uff08\u8c6a\u79d2\uff09 \u8fd9\u91cc\u8fc7\u671f\u65f6\u95f4\u662f3\u8c6a\u79d2\uff0c3\u6beb\u79d2\u540e\uff0c\u952efoo\u7684\u503c\u5c31\u53d8\u6210None\n <\/div>\n
\n
 import <\/span>redis
pool = redis.ConnectionPool<\/span>(host='localhost'<\/span>, port=6379, decode_responses=True)
r = redis.Redis<\/span>(connection_pool=pool)
r.set<\/span>('food'<\/span><\/span>, 'beef'<\/span>, px=3)
print(r.get<\/span>('food'))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 3.nx\uff0c\u5982\u679c\u8bbe\u7f6e\u4e3aTrue\uff0c\u5219\u53ea\u6709name\u4e0d\u5b58\u5728\u65f6\uff0c\u5f53\u524dset\u64cd\u4f5c\u624d\u6267\u884c \uff08\u65b0\u5efa\uff09\n <\/div>\n
\n
 import <\/span>redis
pool = redis.ConnectionPool<\/span>(host='localhost'<\/span>, port=6379, decode_responses=True)
r = redis.Redis<\/span>(connection_pool=pool)
# True<\/span>--\u4e0d\u5b58\u5728
<\/span> print(r.set<\/span>('fruit'<\/span>, 'watermelon'<\/span>, nx=True))
# \u5982\u679c\u952efruit\u4e0d\u5b58\u5728\uff0c\u90a3\u4e48\u8f93\u51fa\u662fTrue\uff1b\u5982\u679c\u952efruit\u5df2\u7ecf\u5b58\u5728\uff0c\u8f93\u51fa\u662fNone
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 4.xx\uff0c\u5982\u679c\u8bbe\u7f6e\u4e3aTrue\uff0c\u5219\u53ea\u6709name\u5b58\u5728\u65f6\uff0c\u5f53\u524dset\u64cd\u4f5c\u624d\u6267\u884c \uff08\u4fee\u6539\uff09\n <\/div>\n
\n
 # True<\/span>--\u5df2\u7ecf\u5b58\u5728
<\/span> print((r.set<\/span>('fruit'<\/span>, 'watermelon'<\/span>, xx=True)))
# \u5982\u679c\u952efruit\u5df2\u7ecf\u5b58\u5728\uff0c\u90a3\u4e48\u8f93\u51fa\u662fTrue\uff1b\u5982\u679c\u952efruit\u4e0d\u5b58\u5728\uff0c\u8f93\u51fa\u662fNone
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 5.setnx(name, value)\u8bbe\u7f6e\u503c\uff0c\u53ea\u6709name\u4e0d\u5b58\u5728\u65f6\uff0c\u6267\u884c\u8bbe\u7f6e\u64cd\u4f5c\uff08\u6dfb\u52a0\uff09\n <\/div>\n
\n
 print(r.setnx<\/span>('fruit1'<\/span>, 'banana'<\/span>)) # fruit1\u4e0d\u5b58\u5728\uff0c\u8f93\u51fa\u4e3aTrue
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 6.setex(name, time, value),time - \u8fc7\u671f\u65f6\u95f4\uff08\u6570\u5b57\u79d2 \u6216 timedelta\u5bf9\u8c61\uff09\n <\/div>\n
\n
 import <\/span>redis
import <\/span>time
pool = redis.ConnectionPool<\/span>(host='localhost'<\/span>, port=6379, decode_responses=True)
r = redis.Redis<\/span>(connection_pool=pool)
r.setex<\/span>(\"fruit2\"<\/span>, 5, \"orange\"<\/span>)
time.sleep<\/span>(5)
# 5\u79d2\u540e\uff0c\u53d6\u503c\u5c31\u4eceorange\u53d8\u6210None
<\/span> print(r.get<\/span>('fruit2'<\/span>))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 7.psetex(name, time_ms, value),time_ms - \u8fc7\u671f\u65f6\u95f4\uff08\u6570\u5b57\u6beb\u79d2 \u6216 timedelta\u5bf9\u8c61\uff09\n <\/div>\n
\n
 r.psetex<\/span>(\"fruit3\"<\/span>, 5000, \"apple\"<\/span>)
time.sleep<\/span>(5)
print(r.get<\/span>('fruit3'<\/span>)) # 5000\u6beb\u79d2\u540e\uff0c\u53d6\u503c\u5c31\u4eceapple\u53d8\u6210None
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 8.mset(*args, **kwargs),\u6279\u91cf\u8bbe\u7f6e\u503c\n <\/div>\n
\n
 r.mget<\/span>({'k1'<\/span>: 'v1'<\/span>, 'k2'<\/span>: 'v2'<\/span>})
r.mset<\/span>(k1=\"v1\"<\/span>, k2=\"v2\"<\/span>) # \u8fd9\u91cck1 \u548ck2 \u4e0d\u80fd\u5e26\u5f15\u53f7 \u4e00\u6b21\u8bbe\u7f6e\u5bf9\u4e2a\u952e\u503c\u5bf9
<\/span> print(r.mget<\/span>(\"k1\"<\/span><\/span>, \"k2\"<\/span>)) # \u4e00\u6b21\u53d6\u51fa\u591a\u4e2a\u952e\u5bf9\u5e94\u7684\u503c
<\/span> print(r.mget<\/span>(\"k1\"))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 9.mget(keys, *args),\u6279\u91cf\u83b7\u53d6\n <\/div>\n
\n
 print(r.mget<\/span>('k1'<\/span><\/span>, 'k2'<\/span><\/span>))
print(r.mget<\/span>(['k1', 'k2']))
print(r.mget<\/span>(\"fruit\"<\/span>, \"fruit1\"<\/span>, \"fruit2\"<\/span>, \"k1\"<\/span>, \"k2\"<\/span>)) # \u5c06\u76ee\u524dredis\u7f13\u5b58\u4e2d\u7684\u952e\u5bf9\u5e94\u7684\u503c\u6279\u91cf\u53d6\u51fa\u6765
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 10.getset(name, value),\u8bbe\u7f6e\u65b0\u503c\u5e76\u83b7\u53d6\u539f\u6765\u7684\u503c\n <\/div>\n
\n
 # \u8bbe\u7f6e\u7684\u65b0\u503c\u662fbarbecue \u8bbe\u7f6e\u524d\u7684\u503c\u662fbeef
<\/span> print(r.getset<\/span>(\"food\"<\/span>, \"barbecue\"<\/span>))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 11.getrange(key, start, end),\u83b7\u53d6\u5b50\u5e8f\u5217\uff08\u6839\u636e\u5b57\u8282\u83b7\u53d6\uff0c\u975e\u5b57\u7b26\uff09\u53c2\u6570\uff1a\n <\/div>\n

name - Redis \u7684 name<\/span>
\n start - \u8d77\u59cb\u4f4d\u7f6e\uff08\u5b57\u8282\uff09<\/span>
\n end - \u7ed3\u675f\u4f4d\u7f6e\uff08\u5b57\u8282\uff09<\/span> <\/p>\n

\n \u5982\uff1a \"\u7acb\u5730\u8d27\" \uff0c0-3\u8868\u793a \"\u5730\"\n <\/div>\n
\n
 # \u6c49\u5b57
<\/span> r.set<\/span>(\"cn_name\"<\/span><\/span><\/span>, \"\u7acb\u5730\u8d27\"<\/span>)
# \u53d6\u7d22\u5f15\u53f7\u662f0-2 \u524d3\u4f4d\u7684\u5b57\u8282 \u7acb \u5207\u7247\u64cd\u4f5c \uff08\u4e00\u4e2a\u6c49\u5b573\u4e2a\u5b57\u8282 1\u4e2a\u5b57\u6bcd\u4e00\u4e2a\u5b57\u8282 \u6bcf\u4e2a\u5b57\u82828bit\uff09
<\/span> print(r.getrange<\/span>(\"cn_name\", 0, 2))
# \u53d6\u6240\u6709\u7684\u5b57\u8282 \u7acb\u5730\u8d27 \u5207\u7247\u64cd\u4f5c
<\/span> print(r.getrange<\/span>(\"cn_name\", 0, -1))
# \u5b57\u6bcd
<\/span> r.set<\/span>(\"en_name\"<\/span><\/span><\/span>,\"junxi\"<\/span>)
# \u53d6\u7d22\u5f15\u53f7\u662f0-2 \u524d3\u4f4d\u7684\u5b57\u8282 jun \u5207\u7247\u64cd\u4f5c \uff08\u4e00\u4e2a\u6c49\u5b573\u4e2a\u5b57\u8282 1\u4e2a\u5b57\u6bcd\u4e00\u4e2a\u5b57\u8282 \u6bcf\u4e2a\u5b57\u82828bit\uff09
<\/span> print(r.getrange<\/span>(\"en_name\", 0, 2))
# \u53d6\u6240\u6709\u7684\u5b57\u8282 junxi \u5207\u7247\u64cd\u4f5c
<\/span> print(r.getrange<\/span>(\"en_name\", 0, -1))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 12.setrange(name, offset, value),\u4fee\u6539\u5b57\u7b26\u4e32\u5185\u5bb9\uff0c\u4ece\u6307\u5b9a\u5b57\u7b26\u4e32\u7d22\u5f15\u5f00\u59cb\u5411\u540e\u66ff\u6362\uff08\u65b0\u503c\u592a\u957f\u65f6\uff0c\u5219\u5411\u540e\u6dfb\u52a0\uff09\n <\/div>\n

offset - \u5b57\u7b26\u4e32\u7684\u7d22\u5f15\uff0c\u5b57\u8282\uff08\u4e00\u4e2a\u6c49\u5b57\u4e09\u4e2a\u5b57\u8282\uff09<\/span>
\n value - \u8981\u8bbe\u7f6e\u7684\u503c<\/span> <\/p>\n

\n
 r.setrange<\/span>(\"en_name\"<\/span><\/span>, 1, \"ccc\"<\/span>)
# jccci \u539f\u59cb\u503c\u662fjunxi \u4ece\u7d22\u5f15\u53f7\u662f1\u5f00\u59cb\u66ff\u6362\u6210ccc \u53d8\u6210 jccci
<\/span> print(r.get<\/span>(\"en_name\"))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 13.setbit(name, offset, value),\u5bf9 name \u5bf9\u5e94\u503c\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u7684\u4f4d\u8fdb\u884c\u64cd\u4f5c,\u53c2\u6570\uff1a\n <\/div>\n

name - redis\u7684name<\/span>
\n offset - \u4f4d\u7684\u7d22\u5f15\uff08\u5c06\u503c\u53d8\u6362\u6210\u4e8c\u8fdb\u5236\u540e\u518d\u8fdb\u884c\u7d22\u5f15\uff09<\/span>
\n value - \u503c\u53ea\u80fd\u662f 1 \u6216 0<\/span> <\/p>\n

\n \u6ce8\uff1a\u5982\u679c\u5728Redis\u4e2d\u6709\u4e00\u4e2a\u5bf9\u5e94\uff1a n1 = \"foo\"
\n
\u90a3\u4e48\u5b57\u7b26\u4e32foo\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u4e3a\uff1a01100110 01101111 01101111
\n
\u6240\u4ee5\uff0c\u5982\u679c\u6267\u884c setbit('n1', 7, 1)\uff0c\u5219\u5c31\u4f1a\u5c06\u7b2c7\u4f4d\u8bbe\u7f6e\u4e3a1\uff0c
\n
\u90a3\u4e48\u6700\u7ec8\u4e8c\u8fdb\u5236\u5219\u53d8\u6210 01100111 01101111 01101111\uff0c\u5373\uff1a\"goo\"\u6269\u5c55\uff0c\u8f6c\u6362\u4e8c\u8fdb\u5236\u8868\u793a\uff1a\n <\/div>\n
\n
 source = \"\u7acb\u5730\u8d27\"<\/span>
source = \"foo\"<\/span>
for <\/span><\/span>i in <\/span>source:
   num = ord(i)
   print <\/span>bin(num).replace<\/span>('b'<\/span>,''<\/span>) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 14.getbit(name, offset),\u83b7\u53d6name\u5bf9\u5e94\u7684\u503c\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u4e2d\u7684\u67d0\u4f4d\u7684\u503c \uff080\u62161\uff09\n <\/div>\n
\n
 # 0 foo1 \u5bf9\u5e94\u7684\u4e8c\u8fdb\u5236 4\u4e2a\u5b57\u8282 32\u4f4d \u7b2c0\u4f4d\u662f0\u8fd8\u662f1
<\/span> print(r.getbit<\/span>(\"foo1\"<\/span>, 0))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 15.bitcount(key, start=None, end=None),\u83b7\u53d6name\u5bf9\u5e94\u7684\u503c\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u4e2d 1 \u7684\u4e2a\u6570,\u53c2\u6570\u5982\u4e0b\uff1a\n <\/div>\n

key - Redis\u7684name<\/span>
\n start - \u5b57\u8282\u8d77\u59cb\u4f4d\u7f6e<\/span>
\n end - \u5b57\u8282\u7ed3\u675f\u4f4d\u7f6e<\/span> <\/p>\n

\n
 print(r.get<\/span>(\"foo\"<\/span><\/span>)) # goo1 01100111
<\/span> print(r.bitcount<\/span>(\"foo\",0,1)) # 11 \u8868\u793a\u524d2\u4e2a\u5b57\u8282\u4e2d\uff0c1\u51fa\u73b0\u7684\u4e2a\u6570
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 16.bitop(operation, dest, *keys),\u83b7\u53d6\u591a\u4e2a\u503c\uff0c\u5e76\u5c06\u503c\u505a\u4f4d\u8fd0\u7b97\uff0c\u5c06\u6700\u540e\u7684\u7ed3\u679c\u4fdd\u5b58\u81f3\u65b0\u7684name\u5bf9\u5e94\u7684\u503c,\u53c2\u6570\u5982\u4e0b\uff1a\n <\/div>\n

operation - AND\uff08\u5e76\uff09 \u3001 OR\uff08\u6216\uff09 \u3001 NOT\uff08\u975e\uff09 \u3001 XOR\uff08\u5f02\u6216\uff09<\/span>
\n dest - \u65b0\u7684Redis\u7684name<\/span>
\n *keys - \u8981\u67e5\u627e\u7684Redis\u7684name<\/span> <\/p>\n

\n
 bitop(\"AND\"<\/span>, 'new_name'<\/span>, 'n1'<\/span>, 'n2'<\/span>, 'n3'<\/span>)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u83b7\u53d6Redis\u4e2dn1,n2,n3\u5bf9\u5e94\u7684\u503c\uff0c\u7136\u540e\u8bb2\u6240\u6709\u7684\u503c\u505a\u4f4d\u8fd0\u7b97\uff08\u6c42\u5e76\u96c6\uff09\uff0c\u7136\u540e\u5c06\u7ed3\u679c\u4fdd\u5b58 new_name \u5bf9\u5e94\u7684\u503c\u4e2d\n <\/div>\n
\n
 r.set<\/span>(\"foo\"<\/span><\/span><\/span><\/span>,\"1\"<\/span>) # 0110001
<\/span> r.set<\/span>(\"foo1\"<\/span><\/span><\/span><\/span>,\"2\"<\/span>) # 0110010
<\/span> print(r.mget<\/span>(\"foo\",\"foo1\")) # ['goo1'<\/span>, 'baaanew'<\/span>]
<\/span> print(r.bitop<\/span>(\"AND\"<\/span>,\"new\"<\/span><\/span><\/span>,\"foo\",\"foo1\")) # \"new\" 0 0110000
<\/span> print(r.mget<\/span>(\"foo\",\"foo1\",\"new\"))
source = \"12\"<\/span>
for <\/span><\/span>i in <\/span>source:
num = ord(i)
print(num) # \u6253\u5370\u6bcf\u4e2a\u5b57\u6bcd\u5b57\u7b26\u6216\u8005\u6c49\u5b57\u5b57\u7b26\u5bf9\u5e94\u7684ascii\u7801\u503c f-102-0b100111-01100111
<\/span> print(bin(num)) # \u6253\u5370\u6bcf\u4e2a10\u8fdb\u5236ascii\u7801\u503c\u8f6c\u6362\u6210\u4e8c\u8fdb\u5236\u7684\u503c 0b1100110\uff080b\u8868\u793a\u4e8c\u8fdb\u5236\uff09
<\/span> print <\/span>bin(num).replace<\/span>('b'<\/span>,''<\/span>) # \u5c06\u4e8c\u8fdb\u52360b1100110\u66ff\u6362\u621001100110
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 17.strlen(name),\u8fd4\u56dename\u5bf9\u5e94\u503c\u7684\u5b57\u8282\u957f\u5ea6\uff08\u4e00\u4e2a\u6c49\u5b573\u4e2a\u5b57\u8282\uff09\n <\/div>\n
\n
 print(r.strlen<\/span>(\"foo\"<\/span>)) # 4 'goo1'<\/span>\u7684\u957f\u5ea6\u662f4
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 18.incr(self, name, amount=1)\uff0c\u81ea\u589e name \u5bf9\u5e94\u7684\u503c\uff0c\u5f53 name \u4e0d\u5b58\u5728\u65f6\uff0c\u5219\u521b\u5efa name\uff1damount\uff0c\u5426\u5219\uff0c\u5219\u81ea\u589e\u3002\n <\/div>\n

name - Redis\u7684name<\/span>
\n amount - \u81ea\u589e\u6570\uff08\u5fc5\u987b\u662f\u6574\u6570\uff09,\u540c incrby<\/span> <\/p>\n

\n
 r.set<\/span>(\"foo\"<\/span><\/span><\/span><\/span>, 123)
print(r.mget<\/span>(\"foo\", \"foo1\"<\/span><\/span>, \"foo2\"<\/span><\/span>, \"k1\"<\/span><\/span>, \"k2\"<\/span><\/span>))
r.incr<\/span>(\"foo\", amount=1)
print(r.mget<\/span>(\"foo\", \"foo1\", \"foo2\", \"k1\", \"k2\"))
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u5e94\u7528\u573a\u666f \u2013 \u9875\u9762\u70b9\u51fb\u6570<\/h3>\n
\n \u5047\u5b9a\u6211\u4eec\u5bf9\u4e00\u7cfb\u5217\u9875\u9762\u9700\u8981\u8bb0\u5f55\u70b9\u51fb\u6b21\u6570\u3002\u4f8b\u5982\u8bba\u575b\u7684\u6bcf\u4e2a\u5e16\u5b50\u90fd\u8981\u8bb0\u5f55\u70b9\u51fb\u6b21\u6570\uff0c\u800c\u70b9\u51fb\u6b21\u6570\u6bd4\u56de\u5e16\u7684\u6b21\u6570\u7684\u591a\u5f97\u591a\u3002\u5982\u679c\u4f7f\u7528\u5173\u7cfb\u6570\u636e\u5e93\u6765\u5b58\u50a8\u70b9\u51fb\uff0c\u53ef\u80fd\u5b58\u5728\u5927\u91cf\u7684\u884c\u7ea7\u9501\u4e89\u7528\u3002\u6240\u4ee5\uff0c\u70b9\u51fb\u6570\u7684\u589e\u52a0\u4f7f\u7528redis\u7684INCR\u547d\u4ee4\u6700\u597d\u4e0d\u8fc7\u4e86\u3002
\n
\u5f53redis\u670d\u52a1\u5668\u542f\u52a8\u65f6\uff0c\u53ef\u4ee5\u4ece\u5173\u7cfb\u6570\u636e\u5e93\u8bfb\u5165\u70b9\u51fb\u6570\u7684\u521d\u59cb\u503c\uff0812306\u8fd9\u4e2a\u9875\u9762\u88ab\u8bbf\u95ee\u4e8634634\u6b21\uff09\n <\/div>\n
\n
 r.set<\/span>(\"visit:12306:totals\"<\/span><\/span>, 34634)
print(r.get<\/span>(\"visit:12306:totals\"))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6bcf\u5f53\u6709\u4e00\u4e2a\u9875\u9762\u70b9\u51fb\uff0c\u5219\u4f7f\u7528INCR\u589e\u52a0\u70b9\u51fb\u6570\u5373\u53ef\u3002\n <\/div>\n
\n
 r.incr<\/span>(\"visit:12306:totals\"<\/span><\/span>)
r.incr<\/span>(\"visit:12306:totals\")
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u9875\u9762\u8f7d\u5165\u7684\u65f6\u5019\u5219\u53ef\u76f4\u63a5\u83b7\u53d6\u8fd9\u4e2a\u503c\n <\/div>\n
\n
 print(r.get<\/span>(\"visit:12306:totals\"<\/span>))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 19.incrbyfloat(self, name, amount=1.0),\u81ea\u589e name\u5bf9\u5e94\u7684\u503c\uff0c\u5f53name\u4e0d\u5b58\u5728\u65f6\uff0c\u5219\u521b\u5efaname\uff1damount\uff0c\u5426\u5219\uff0c\u5219\u81ea\u589e\u3002\u53c2\u6570\uff1a\n <\/div>\n

name - Redis\u7684name<\/span>
\n amount - \u81ea\u589e\u6570\uff08\u6d6e\u70b9\u578b\uff09<\/span> <\/p>\n

\n
 r.set<\/span>(\"foo1\"<\/span><\/span><\/span><\/span>, \"123.0\"<\/span>)
r.set<\/span>(\"foo2\"<\/span><\/span><\/span><\/span>, \"221.0\"<\/span>)
print(r.mget<\/span>(\"foo1\", \"foo2\"))
r.incrbyfloat<\/span>(\"foo1\", amount=2.0)
r.incrbyfloat<\/span>(\"foo2\", amount=3.0)
print(r.mget<\/span>(\"foo1\", \"foo2\"))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 20.decr(self, name, amount=1),\u81ea\u51cf name \u5bf9\u5e94\u7684\u503c\uff0c\u5f53 name \u4e0d\u5b58\u5728\u65f6\uff0c\u5219\u521b\u5efa name\uff1damount\uff0c\u5426\u5219\uff0c\u5219\u81ea\u51cf\u3002\u53c2\u6570\uff1a\n <\/div>\n

name - Redis\u7684name<\/span>
\n amount - \u81ea\u51cf\u6570\uff08\u6574\u6570)<\/span> <\/p>\n

\n
 r.decr<\/span>(\"foo4\"<\/span><\/span>, amount=3) # \u9012\u51cf3
<\/span> r.decr<\/span>(\"foo1\"<\/span><\/span>, amount=1) # \u9012\u51cf1
<\/span> print(r.mget<\/span>(\"foo1\", \"foo4\"))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 21.append(key, value),\u5728redis name\u5bf9\u5e94\u7684\u503c\u540e\u9762\u8ffd\u52a0\u5185\u5bb9,\u53c2\u6570\uff1a\n <\/div>\n

key - redis\u7684name<\/span>
\n value - \u8981\u8ffd\u52a0\u7684\u5b57\u7b26\u4e32<\/span> <\/p>\n

\n
 # \u5728name\u5bf9\u5e94\u7684\u503cjunxi\u540e\u9762\u8ffd\u52a0\u5b57\u7b26\u4e32haha
<\/span> r.append<\/span>(\"name\"<\/span><\/span>, \"haha\"<\/span>)
print(r.mget<\/span>(\"name\"))
<\/span><\/code><\/pre>\n<\/p><\/div>\n

redis \u57fa\u672c\u547d\u4ee4 hash<\/h2>\n

1\u3001\u5355\u4e2a\u589e\u52a0--\u4fee\u6539(\u5355\u4e2a\u53d6\u51fa)--\u6ca1\u6709\u5c31\u65b0\u589e\uff0c\u6709\u7684\u8bdd\u5c31\u4fee\u6539<\/h3>\n
\n
 hset(name, key, value)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n name\u5bf9\u5e94\u7684hash\u4e2d\u8bbe\u7f6e\u4e00\u4e2a\u952e\u503c\u5bf9\uff08\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\uff1b\u5426\u5219\uff0c\u4fee\u6539\uff09\u53c2\u6570\uff1a\n <\/div>\n

name - redis\u7684name<\/span>
\n key - name\u5bf9\u5e94\u7684hash\u4e2d\u7684key<\/span>
\n value - name\u5bf9\u5e94\u7684hash\u4e2d\u7684value<\/span> <\/p>\n

\n \u6ce8\uff1ahsetnx(name, key, value) \u5f53name\u5bf9\u5e94\u7684hash\u4e2d\u4e0d\u5b58\u5728\u5f53\u524dkey\u65f6\u5219\u521b\u5efa\uff08\u76f8\u5f53\u4e8e\u6dfb\u52a0\uff09\n <\/div>\n
\n
 import <\/span>redis
import <\/span>time
pool = redis.ConnectionPool<\/span>(host='localhost'<\/span>, port=6379, decode_responses=True)
r = redis.Redis<\/span>(connection_pool=pool)
r.hset<\/span>(\"hash1\"<\/span><\/span><\/span><\/span><\/span><\/span><\/span>, \"k1\"<\/span><\/span><\/span>, \"v1\"<\/span>)
r.hset<\/span>(\"hash1\", \"k2\"<\/span><\/span><\/span><\/span>, \"v2\"<\/span>)
print(r.hkeys<\/span>(\"hash1\")) # \u53d6hash\u4e2d\u6240\u6709\u7684key
<\/span> print(r.hget<\/span>(\"hash1\", \"k1\")) # \u5355\u4e2a\u53d6hash\u7684key\u5bf9\u5e94\u7684\u503c
<\/span> print(r.hmget<\/span>(\"hash1\", \"k1\", \"k2\")) # \u591a\u4e2a\u53d6hash\u7684key\u5bf9\u5e94\u7684\u503c
<\/span> r.hsetnx<\/span>(\"hash1\", \"k2\", \"v3\"<\/span>) # \u53ea\u80fd\u65b0\u5efa
<\/span> print(r.hget<\/span>(\"hash1\", \"k2\"))
<\/span><\/code><\/pre>\n<\/p><\/div>\n

2\u3001\u6279\u91cf\u589e\u52a0\uff08\u53d6\u51fa\uff09<\/h3>\n
\n
 hmset(name, mapping) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728name\u5bf9\u5e94\u7684hash\u4e2d\u6279\u91cf\u8bbe\u7f6e\u952e\u503c\u5bf9\n <\/div>\n
\n \u53c2\u6570\uff1a\n <\/div>\n

name - redis\u7684name<\/span>
\n mapping - \u5b57\u5178\uff0c\u5982\uff1a{'k1':'v1', 'k2': 'v2'}<\/span> <\/p>\n

\n
 r.hmset<\/span>(\"hash2\"<\/span>, {\"k2\"<\/span>: \"v2\"<\/span>, \"k3\"<\/span>: \"v3\"<\/span>})
hget(name,key)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728name\u5bf9\u5e94\u7684hash\u4e2d\u83b7\u53d6\u6839\u636ekey\u83b7\u53d6value\n <\/div>\n
\n
 hmget(name, keys, *args)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728name\u5bf9\u5e94\u7684hash\u4e2d\u83b7\u53d6\u591a\u4e2akey\u7684\u503c\uff0c\u53c2\u6570\uff1a\n <\/div>\n

name - reids\u5bf9\u5e94\u7684name<\/span>
\n keys - \u8981\u83b7\u53d6key\u96c6\u5408\uff0c\u5982\uff1a['k1', 'k2', 'k3']<\/span>
\n *args - \u8981\u83b7\u53d6\u7684key\uff0c\u5982\uff1ak1,k2,k3<\/span> <\/p>\n

\n
 print(r.hget<\/span>(\"hash2\"<\/span><\/span><\/span><\/span><\/span><\/span>, \"k2\"<\/span><\/span><\/span>)) # \u5355\u4e2a\u53d6\u51fa\"hash2\"\u7684key-k2\u5bf9\u5e94\u7684value
<\/span> print(r.hmget<\/span>(\"hash2\", \"k2\", \"k3\"<\/span><\/span>)) # \u6279\u91cf\u53d6\u51fa\"hash2\"\u7684key-k2 k3\u5bf9\u5e94\u7684value --\u65b9\u5f0f1
<\/span> print(r.hmget<\/span>(\"hash2\", [\"k2\", \"k3\"])) # \u6279\u91cf\u53d6\u51fa\"hash2\"\u7684key-k2 k3\u5bf9\u5e94\u7684value --\u65b9\u5f0f2
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n

3\u3001\u53d6\u51fa\u6240\u6709\u7684\u952e\u503c\u5bf9<\/h3>\n
\n
 hgetall(name)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u83b7\u53d6name\u5bf9\u5e94hash\u7684\u6240\u6709\u952e\u503c\n <\/div>\n
\n
 print(r.hgetall<\/span>(\"hash1\"<\/span>))
<\/span><\/code><\/pre>\n<\/p><\/div>\n

4\u3001\u5f97\u5230\u6240\u6709\u952e\u503c\u5bf9\u7684\u683c\u5f0f hash\u957f\u5ea6<\/h3>\n
\n
 hlen(name) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u83b7\u53d6name\u5bf9\u5e94\u7684hash\u4e2d\u952e\u503c\u5bf9\u7684\u4e2a\u6570\n <\/div>\n
\n
 print(r.hlen(\"hash1\")) <\/span><\/code><\/pre>\n<\/p><\/div>\n

5\u3001\u5f97\u5230\u6240\u6709\u7684keys\uff08\u7c7b\u4f3c\u5b57\u5178\u7684\u53d6\u6240\u6709keys\uff09<\/h3>\n
\n
 hkeys(name) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u83b7\u53d6name\u5bf9\u5e94\u7684hash\u4e2d\u6240\u6709\u7684key\u7684\u503c\n <\/div>\n
\n
 print(r.hkeys(\"hash1\")) <\/span><\/code><\/pre>\n<\/p><\/div>\n

6\u3001\u5f97\u5230\u6240\u6709\u7684value\uff08\u7c7b\u4f3c\u5b57\u5178\u7684\u53d6\u6240\u6709value\uff09<\/h3>\n
\n
 hvals(name) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u83b7\u53d6name\u5bf9\u5e94\u7684hash\u4e2d\u6240\u6709\u7684value\u7684\u503c\n <\/div>\n
\n
 print(r.hvals(\"hash1\")) <\/span><\/code><\/pre>\n<\/p><\/div>\n

7\u3001\u5224\u65ad\u6210\u5458\u662f\u5426\u5b58\u5728\uff08\u7c7b\u4f3c\u5b57\u5178\u7684in\uff09<\/h3>\n
\n
 hexists(name, key) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u68c0\u67e5 name \u5bf9\u5e94\u7684 hash \u662f\u5426\u5b58\u5728\u5f53\u524d\u4f20\u5165\u7684 key\n <\/div>\n
\n
 print(r.hexists<\/span>(\"hash1\"<\/span><\/span>, \"k4\"<\/span>)) # False<\/span> \u4e0d\u5b58\u5728
<\/span> print(r.hexists<\/span>(\"hash1\", \"k1\"<\/span>)) # True<\/span> \u5b58\u5728
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n

8\u3001\u5220\u9664\u952e\u503c\u5bf9<\/h3>\n
\n
 hdel(name,*keys) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5c06name\u5bf9\u5e94\u7684hash\u4e2d\u6307\u5b9akey\u7684\u952e\u503c\u5bf9\u5220\u9664\n <\/div>\n
\n
 print(r.hgetall<\/span>(\"hash1\"<\/span><\/span><\/span><\/span><\/span>))
r.hset<\/span>(\"hash1\", \"k2\"<\/span>, \"v222\"<\/span>) # \u4fee\u6539\u5df2\u6709\u7684key k2
<\/span> r.hset<\/span>(\"hash1\", \"k11\"<\/span>, \"v1\"<\/span>) # \u65b0\u589e\u952e\u503c\u5bf9 k11
<\/span> r.hdel<\/span>(\"hash1\", \"k1\"<\/span>) # \u5220\u9664\u4e00\u4e2a\u952e\u503c\u5bf9
<\/span> print(r.hgetall<\/span>(\"hash1\"))
<\/span><\/code><\/pre>\n<\/p><\/div>\n

9\u3001\u81ea\u589e\u81ea\u51cf\u6574\u6570(\u5c06key\u5bf9\u5e94\u7684value--\u6574\u6570 \u81ea\u589e1\u6216\u80052\uff0c\u6216\u8005\u522b\u7684\u6574\u6570 \u8d1f\u6570\u5c31\u662f\u81ea\u51cf)<\/h3>\n
\n
 hincrby(name, key, amount=1) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u81ea\u589ename\u5bf9\u5e94\u7684hash\u4e2d\u7684\u6307\u5b9akey\u7684\u503c\uff0c\u4e0d\u5b58\u5728\u5219\u521b\u5efakey=amount\uff0c\u53c2\u6570\uff1a\n <\/div>\n

name - redis\u4e2d\u7684name<\/span>
\n key - hash\u5bf9\u5e94\u7684key<\/span>
\n amount - \u81ea\u589e\u6570\uff08\u6574\u6570\uff09<\/span> <\/p>\n

\n
 r.hset<\/span>(\"hash1\"<\/span><\/span><\/span><\/span><\/span>, \"k3\"<\/span><\/span>, 123)
r.hincrby<\/span>(\"hash1\", \"k3\", amount=-1)
print(r.hgetall<\/span>(\"hash1\"))
r.hincrby<\/span>(\"hash1\", \"k4\"<\/span>, amount=1) # \u4e0d\u5b58\u5728\u7684\u8bdd\uff0cvalue\u9ed8\u8ba4\u5c31\u662f1
<\/span> print(r.hgetall<\/span>(\"hash1\"))
<\/span><\/code><\/pre>\n<\/p><\/div>\n

10\u3001\u81ea\u589e\u81ea\u51cf\u6d6e\u70b9\u6570(\u5c06key\u5bf9\u5e94\u7684value--\u6d6e\u70b9\u6570 \u81ea\u589e1.0\u6216\u80052.0\uff0c\u6216\u8005\u522b\u7684\u6d6e\u70b9\u6570 \u8d1f\u6570\u5c31\u662f\u81ea\u51cf)<\/h3>\n
\n
 hincrbyfloat(name, key, amount=1.0) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u81ea\u589ename\u5bf9\u5e94\u7684hash\u4e2d\u7684\u6307\u5b9akey\u7684\u503c\uff0c\u4e0d\u5b58\u5728\u5219\u521b\u5efakey=amount\uff0c\u53c2\u6570\uff1a\n <\/div>\n

name - redis\u4e2d\u7684name<\/span>
\n key - hash\u5bf9\u5e94\u7684key<\/span>
\n amount\uff0c\u81ea\u589e\u6570\uff08\u6d6e\u70b9\u6570\uff09<\/span> <\/p>\n

\n \u81ea\u589e name \u5bf9\u5e94\u7684 hash \u4e2d\u7684\u6307\u5b9a key \u7684\u503c\uff0c\u4e0d\u5b58\u5728\u5219\u521b\u5efa key=amount\u3002\n <\/div>\n
\n
 r.hset<\/span>(\"hash1\"<\/span><\/span><\/span><\/span><\/span>, \"k5\"<\/span><\/span>, \"1.0\"<\/span>)
r.hincrbyfloat<\/span>(\"hash1\", \"k5\", amount=-1.0) # \u5df2\u7ecf\u5b58\u5728\uff0c\u9012\u51cf-1.0
<\/span> print(r.hgetall<\/span>(\"hash1\"))
r.hincrbyfloat<\/span>(\"hash1\", \"k6\"<\/span>, amount=-1.0) # \u4e0d\u5b58\u5728\uff0cvalue\u521d\u59cb\u503c\u662f-1.0 \u6bcf\u6b21\u9012\u51cf1.0
<\/span> print(r.hgetall<\/span>(\"hash1\"))
<\/span><\/code><\/pre>\n<\/p><\/div>\n

11\u3001\u53d6\u503c\u67e5\u770b--\u5206\u7247\u8bfb\u53d6<\/h3>\n
\n
 hscan(name, cursor=0, match=None, count=None) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u589e\u91cf\u5f0f\u8fed\u4ee3\u83b7\u53d6\uff0c\u5bf9\u4e8e\u6570\u636e\u5927\u7684\u6570\u636e\u975e\u5e38\u6709\u7528\uff0chscan\u53ef\u4ee5\u5b9e\u73b0\u5206\u7247\u7684\u83b7\u53d6\u6570\u636e\uff0c\u5e76\u975e\u4e00\u6b21\u6027\u5c06\u6570\u636e\u5168\u90e8\u83b7\u53d6\u5b8c\uff0c\u4ece\u800c\u653e\u7f6e\u5185\u5b58\u88ab\u6491\u7206\uff0c\u53c2\u6570\uff1a\n <\/div>\n

name - redis\u7684name<\/span>
\n cursor - \u6e38\u6807\uff08\u57fa\u4e8e\u6e38\u6807\u5206\u6279\u53d6\u83b7\u53d6\u6570\u636e\uff09<\/span>
\n match - \u5339\u914d\u6307\u5b9akey\uff0c\u9ed8\u8ba4None \u8868\u793a\u6240\u6709\u7684key<\/span>
\n count - \u6bcf\u6b21\u5206\u7247\u6700\u5c11\u83b7\u53d6\u4e2a\u6570\uff0c\u9ed8\u8ba4None\u8868\u793a\u91c7\u7528Redis\u7684\u9ed8\u8ba4\u5206\u7247\u4e2a\u6570<\/span> <\/p>\n

\n \u5982\uff1a\n <\/div>\n
\n
 \u7b2c\u4e00\u6b21\uff1acursor1, data1 = r.hscan<\/span>('xx'<\/span><\/span>, cursor=0, match=None, count=None)
\u7b2c\u4e8c\u6b21\uff1acursor2, data1 = r.hscan<\/span>('xx', cursor=cursor1, match=None, count=None)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u76f4\u5230\u8fd4\u56de\u503ccursor\u7684\u503c\u4e3a0\u65f6\uff0c\u8868\u793a\u6570\u636e\u5df2\u7ecf\u901a\u8fc7\u5206\u7247\u83b7\u53d6\u5b8c\u6bd5\n <\/div>\n
\n
print(r.hscan(\"hash1\"))<\/span><\/code><\/pre>\n<\/p><\/div>\n

12\u3001hscan_iter(name, match=None, count=None)<\/h3>\n
\n \u5229\u7528yield\u5c01\u88c5hscan\u521b\u5efa\u751f\u6210\u5668\uff0c\u5b9e\u73b0\u5206\u6279\u53bbredis\u4e2d\u83b7\u53d6\u6570\u636e\uff0c\u53c2\u6570\uff1a\n <\/div>\n

match - \u5339\u914d\u6307\u5b9akey\uff0c\u9ed8\u8ba4None \u8868\u793a\u6240\u6709\u7684key<\/span>
\n count - \u6bcf\u6b21\u5206\u7247\u6700\u5c11\u83b7\u53d6\u4e2a\u6570\uff0c\u9ed8\u8ba4None\u8868\u793a\u91c7\u7528Redis\u7684\u9ed8\u8ba4\u5206\u7247\u4e2a\u6570<\/span> <\/p>\n

\n
 for <\/span><\/span>item in <\/span>r.hscan_iter<\/span>('hash1'<\/span>):
    print(item)
print(r.hscan_iter<\/span>(\"hash1\"<\/span>)) # \u751f\u6210\u5668\u5185\u5b58\u5730\u5740
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n

redis\u57fa\u672c\u547d\u4ee4 list<\/h2>\n
\n 1.\u589e\u52a0\uff08\u7c7b\u4f3c\u4e8elist\u7684append\uff0c\u53ea\u662f\u8fd9\u91cc\u662f\u4ece\u5de6\u8fb9\u65b0\u589e\u52a0\uff09--\u6ca1\u6709\u5c31\u65b0\u5efa\n <\/div>\n
\n
lpush(name,values)<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728name\u5bf9\u5e94\u7684list\u4e2d\u6dfb\u52a0\u5143\u7d20\uff0c\u6bcf\u4e2a\u65b0\u7684\u5143\u7d20\u90fd\u6dfb\u52a0\u5230\u5217\u8868\u7684\u6700\u5de6\u8fb9\n <\/div>\n
\n
 import <\/span>redis
import <\/span>time
pool = redis.ConnectionPool<\/span>(host='localhost'<\/span>, port=6379, decode_responses=True)
r = redis.Redis<\/span>(connection_pool=pool)
r.lpush<\/span>(\"list1\"<\/span>, 11, 22, 33)
print(r.lrange<\/span>('list1'<\/span>, 0, -1))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4fdd\u5b58\u987a\u5e8f\u4e3a: 33,22,11,\u6269\u5c55\uff1a\n <\/div>\n
\n
 r.rpush<\/span>(\"list2\"<\/span><\/span><\/span>, 11, 22, 33) # \u8868\u793a\u4ece\u53f3\u5411\u5de6\u64cd\u4f5c
<\/span> print(r.llen<\/span>(\"list2\")) # \u5217\u8868\u957f\u5ea6
<\/span> print(r.lrange<\/span>(\"list2\", 0, 3)) # \u5207\u7247\u53d6\u51fa\u503c\uff0c\u8303\u56f4\u662f\u7d22\u5f15\u53f70-3
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 2\u3001\u589e\u52a0\uff08\u4ece\u53f3\u8fb9\u589e\u52a0\uff09--\u6ca1\u6709\u5c31\u65b0\u5efa\n <\/div>\n
\n
 r.rpush<\/span>(\"list2\"<\/span><\/span><\/span>, 44, 55, 66) # \u5728\u5217\u8868\u7684\u53f3\u8fb9\uff0c\u4f9d\u6b21\u6dfb\u52a044,55,66
<\/span> print(r.llen<\/span>(\"list2\")) # \u5217\u8868\u957f\u5ea6
<\/span> print(r.lrange<\/span>(\"list2\", 0, -1)) # \u5207\u7247\u53d6\u51fa\u503c\uff0c\u8303\u56f4\u662f\u7d22\u5f15\u53f70\u5230-1(\u6700\u540e\u4e00\u4e2a\u5143\u7d20)
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 3.\u5f80\u5df2\u7ecf\u6709\u7684name\u7684\u5217\u8868\u7684\u5de6\u8fb9\u6dfb\u52a0\u5143\u7d20\uff0c\u6ca1\u6709\u7684\u8bdd\u65e0\u6cd5\u521b\u5efa\n <\/div>\n
\n
 lpushx(name,value)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728name\u5bf9\u5e94\u7684list\u4e2d\u6dfb\u52a0\u5143\u7d20\uff0c\u53ea\u6709name\u5df2\u7ecf\u5b58\u5728\u65f6\uff0c\u503c\u6dfb\u52a0\u5230\u5217\u8868\u7684\u6700\u5de6\u8fb9\n <\/div>\n
\n
 r.lpushx<\/span>(\"list10\"<\/span><\/span><\/span>, 10) # \u8fd9\u91cclist10\u4e0d\u5b58\u5728
<\/span> print(r.llen<\/span>(\"list10\")) # 0
<\/span> print(r.lrange<\/span>(\"list10\", 0, -1)) # []
<\/span> r.lpushx<\/span>(\"list2\"<\/span><\/span><\/span><\/span>, 77) # \u8fd9\u91cc\"list2\"\u4e4b\u524d\u5df2\u7ecf\u5b58\u5728\uff0c\u5f80\u5217\u8868\u6700\u5de6\u8fb9\u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20\uff0c\u4e00\u6b21\u53ea\u80fd\u6dfb\u52a0\u4e00\u4e2a
<\/span> print(r.llen<\/span>(\"list2\")) # \u5217\u8868\u957f\u5ea6
<\/span> print(r.lrange<\/span>(\"list2\", 0, -1)) # \u5207\u7247\u53d6\u51fa\u503c\uff0c\u8303\u56f4\u662f\u7d22\u5f15\u53f70\u5230-1(\u6700\u540e\u4e00\u4e2a\u5143\u7d20
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 4.\u5f80\u5df2\u7ecf\u6709\u7684name\u7684\u5217\u8868\u7684\u53f3\u8fb9\u6dfb\u52a0\u5143\u7d20\uff0c\u6ca1\u6709\u7684\u8bdd\u65e0\u6cd5\u521b\u5efa\n <\/div>\n
\n
 r.rpushx<\/span>(\"list2\"<\/span><\/span><\/span>, 99) # \u8fd9\u91cc\"foo_list1\"<\/span>\u4e4b\u524d\u5df2\u7ecf\u5b58\u5728\uff0c\u5f80\u5217\u8868\u6700\u53f3\u8fb9\u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20\uff0c\u4e00\u6b21\u53ea\u80fd\u6dfb\u52a0\u4e00\u4e2a
<\/span> print(r.llen<\/span>(\"list2\")) # \u5217\u8868\u957f\u5ea6
<\/span> print(r.lrange<\/span>(\"list2\", 0, -1)) # \u5207\u7247\u53d6\u51fa\u503c\uff0c\u8303\u56f4\u662f\u7d22\u5f15\u53f70\u5230-1(\u6700\u540e\u4e00\u4e2a\u5143\u7d20)
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 5.\u65b0\u589e\uff08\u56fa\u5b9a\u7d22\u5f15\u53f7\u4f4d\u7f6e\u63d2\u5165\u5143\u7d20\uff09\n <\/div>\n
\n
 linsert(name, where, refvalue, value))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728name\u5bf9\u5e94\u7684\u5217\u8868\u7684\u67d0\u4e00\u4e2a\u503c\u524d\u6216\u540e\u63d2\u5165\u4e00\u4e2a\u65b0\u503c,\u53c2\u6570\uff1a\n <\/div>\n

name - redis\u7684name<\/span>
\n where - BEFORE\u6216AFTER<\/span>
\n refvalue - \u6807\u6746\u503c\uff0c\u5373\uff1a\u5728\u5b83\u524d\u540e\u63d2\u5165\u6570\u636e<\/span>
\n value - \u8981\u63d2\u5165\u7684\u6570\u636e<\/span> <\/p>\n

\n
 r.linsert<\/span>(\"list2\"<\/span><\/span>, \"before\"<\/span>, \"11\"<\/span><\/span>, \"00\"<\/span><\/span>) # \u5f80\u5217\u8868\u4e2d\u5de6\u8fb9\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u5143\u7d20\"11\"\u524d\u63d2\u5165\u5143\u7d20\"00\"
<\/span> print(r.lrange<\/span>(\"list2\", 0, -1)) # \u5207\u7247\u53d6\u51fa\u503c\uff0c\u8303\u56f4\u662f\u7d22\u5f15\u53f70-\u6700\u540e\u4e00\u4e2a\u5143\u7d20
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 6.\u4fee\u6539\uff08\u6307\u5b9a\u7d22\u5f15\u53f7\u8fdb\u884c\u4fee\u6539\uff09\n <\/div>\n
\n
 r.lset<\/span>(name, index, value)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5bf9name\u5bf9\u5e94\u7684list\u4e2d\u7684\u67d0\u4e00\u4e2a\u7d22\u5f15\u4f4d\u7f6e\u91cd\u65b0\u8d4b\u503c,\u53c2\u6570\uff1a\n <\/div>\n

name - redis\u7684name<\/span>
\n index - list\u7684\u7d22\u5f15\u4f4d\u7f6e<\/span>
\n value - \u8981\u8bbe\u7f6e\u7684\u503c<\/span> <\/p>\n

\n
 r.lset<\/span>(\"list2\"<\/span><\/span>, 0, -11) # \u628a\u7d22\u5f15\u53f7\u662f0\u7684\u5143\u7d20\u4fee\u6539\u6210-11
<\/span> print(r.lrange<\/span>(\"list2\", 0, -1))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 7.\u5220\u9664\uff08\u6307\u5b9a\u503c\u8fdb\u884c\u5220\u9664\uff09\n <\/div>\n
\n
 r.lrem<\/span>(name, value, num)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728name\u5bf9\u5e94\u7684list\u4e2d\u5220\u9664\u6307\u5b9a\u7684\u503c\uff0c\u53c2\u6570\uff1a\n <\/div>\n

name - redis\u7684name<\/span>
\n value - \u8981\u5220\u9664\u7684\u503c<\/span>
\n num - num=0\uff0c\u5220\u9664\u5217\u8868\u4e2d\u6240\u6709\u7684\u6307\u5b9a\u503c\uff1b<\/span>
\n num=2 - \u4ece\u524d\u5230\u540e\uff0c\u5220\u96642\u4e2a, num=1,\u4ece\u524d\u5230\u540e\uff0c\u5220\u9664\u5de6\u8fb9\u7b2c1\u4e2a<\/span>
\n num=-2 - \u4ece\u540e\u5411\u524d\uff0c\u5220\u96642\u4e2a<\/span> <\/p>\n

\n
 r.lrem<\/span>(\"list2\"<\/span><\/span><\/span><\/span><\/span><\/span>, \"11\"<\/span><\/span>, 1) # \u5c06\u5217\u8868\u4e2d\u5de6\u8fb9\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\"11\"\u5220\u9664
<\/span> print(r.lrange<\/span>(\"list2\", 0, -1))
r.lrem<\/span>(\"list2\", \"99\"<\/span><\/span>, -1) # \u5c06\u5217\u8868\u4e2d\u53f3\u8fb9\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\"99\"\u5220\u9664
<\/span> print(r.lrange<\/span>(\"list2\", 0, -1))
r.lrem<\/span>(\"list2\", \"22\"<\/span><\/span>, 0) # \u5c06\u5217\u8868\u4e2d\u6240\u6709\u7684\"22\"\u5220\u9664
<\/span> print(r.lrange<\/span>(\"list2\", 0, -1))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 8.\u5220\u9664\u5e76\u8fd4\u56de\n <\/div>\n
\n
 lpop(name)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728name\u5bf9\u5e94\u7684\u5217\u8868\u7684\u5de6\u4fa7\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u5e76\u5728\u5217\u8868\u4e2d\u79fb\u9664\uff0c\u8fd4\u56de\u503c\u5219\u662f\u7b2c\u4e00\u4e2a\u5143\u7d20,rpop(name) \u8868\u793a\u4ece\u53f3\u5411\u5de6\u64cd\u4f5c\n <\/div>\n
\n
 r.lpop<\/span>(\"list2\"<\/span><\/span><\/span><\/span>) # \u5220\u9664\u5217\u8868\u6700\u5de6\u8fb9\u7684\u5143\u7d20\uff0c\u5e76\u4e14\u8fd4\u56de\u5220\u9664\u7684\u5143\u7d20
<\/span> print(r.lrange<\/span>(\"list2\", 0, -1))
r.rpop<\/span>(\"list2\") # \u5220\u9664\u5217\u8868\u6700\u53f3\u8fb9\u7684\u5143\u7d20\uff0c\u5e76\u4e14\u8fd4\u56de\u5220\u9664\u7684\u5143\u7d20
<\/span> print(r.lrange<\/span>(\"list2\", 0, -1))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 9.\u5220\u9664\u7d22\u5f15\u4e4b\u5916\u7684\u503c\n <\/div>\n
\n
 ltrim(name, start, end)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728name\u5bf9\u5e94\u7684\u5217\u8868\u4e2d\u79fb\u9664\u6ca1\u6709\u5728start-end\u7d22\u5f15\u4e4b\u95f4\u7684\u503c,\u53c2\u6570\uff1a\n <\/div>\n

name - redis\u7684name<\/span>
\n start - \u7d22\u5f15\u7684\u8d77\u59cb\u4f4d\u7f6e<\/span>
\n end - \u7d22\u5f15\u7ed3\u675f\u4f4d\u7f6e<\/span> <\/p>\n

\n
 r.ltrim<\/span>(\"list2\"<\/span><\/span>, 0, 2) # \u5220\u9664\u7d22\u5f15\u53f7\u662f0-2\u4e4b\u5916\u7684\u5143\u7d20\uff0c\u503c\u4fdd\u7559\u7d22\u5f15\u53f7\u662f0-2\u7684\u5143\u7d20
<\/span> print(r.lrange<\/span>(\"list2\", 0, -1))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728name\u5bf9\u5e94\u7684\u5217\u8868\u4e2d\u6839\u636e\u7d22\u5f15\u83b7\u53d6\u5217\u8868\u5143\u7d20\n <\/div>\n
\n
print(r.lindex(\"list2\", 0)) # \u53d6\u51fa\u7d22\u5f15\u53f7\u662f0\u7684\u503c<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 10.\u53d6\u503c\uff08\u6839\u636e\u7d22\u5f15\u53f7\u53d6\u503c\uff09\n <\/div>\n
\n
rpoplpush(src, dst)<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 11.\u79fb\u52a8 \u5143\u7d20\u4ece\u4e00\u4e2a\u5217\u8868\u79fb\u52a8\u5230\u53e6\u5916\u4e00\u4e2a\u5217\u8868\n <\/div>\n
\n \u4ece\u4e00\u4e2a\u5217\u8868\u53d6\u51fa\u6700\u53f3\u8fb9\u7684\u5143\u7d20\uff0c\u540c\u65f6\u5c06\u5176\u6dfb\u52a0\u81f3\u53e6\u4e00\u4e2a\u5217\u8868\u7684\u6700\u5de6\u8fb9\uff0c\u53c2\u6570\uff1a\n <\/div>\n

src - \u8981\u53d6\u6570\u636e\u7684\u5217\u8868\u7684 name<\/span>
\n dst - \u8981\u6dfb\u52a0\u6570\u636e\u7684\u5217\u8868\u7684 name<\/span> <\/p>\n

\n
 r.rpoplpush<\/span>(\"list1\"<\/span>, \"list2\"<\/span><\/span>)
print(r.lrange<\/span>(\"list2\", 0, -1))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 12.\u79fb\u52a8 \u5143\u7d20\u4ece\u4e00\u4e2a\u5217\u8868\u79fb\u52a8\u5230\u53e6\u5916\u4e00\u4e2a\u5217\u8868 \u53ef\u4ee5\u8bbe\u7f6e\u8d85\u65f6\n <\/div>\n
\n
 brpoplpush(src, dst, timeout=0)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4ece\u4e00\u4e2a\u5217\u8868\u7684\u53f3\u4fa7\u79fb\u9664\u4e00\u4e2a\u5143\u7d20\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u53e6\u4e00\u4e2a\u5217\u8868\u7684\u5de6\u4fa7\uff0c\u53c2\u6570\uff1a\n <\/div>\n

src - \u53d6\u51fa\u5e76\u8981\u79fb\u9664\u5143\u7d20\u7684\u5217\u8868\u5bf9\u5e94\u7684name<\/span>
\n dst - \u8981\u63d2\u5165\u5143\u7d20\u7684\u5217\u8868\u5bf9\u5e94\u7684name<\/span>
\n timeout - \u5f53src\u5bf9\u5e94\u7684\u5217\u8868\u4e2d\u6ca1\u6709\u6570\u636e\u65f6\uff0c\u963b\u585e\u7b49\u5f85\u5176\u6709\u6570\u636e\u7684\u8d85\u65f6\u65f6\u95f4\uff08\u79d2\uff09\uff0c0 \u8868\u793a\u6c38\u8fdc\u963b\u585e<\/span> <\/p>\n

\n
 r.brpoplpush<\/span>(\"list1\"<\/span>, \"list2\"<\/span><\/span>, timeout=2)
print(r.lrange<\/span>(\"list2\", 0, -1))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 13.\u4e00\u6b21\u79fb\u9664\u591a\u4e2a\u5217\u8868\n <\/div>\n
\n
blpop(keys, timeout)<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5c06\u591a\u4e2a\u5217\u8868\u6392\u5217\uff0c\u6309\u7167\u4ece\u5de6\u5230\u53f3\u53bbpop\u5bf9\u5e94\u5217\u8868\u7684\u5143\u7d20\uff0c\u53c2\u6570\uff1a\n <\/div>\n

keys - redis\u7684name\u7684\u96c6\u5408<\/span>
\n timeout - \u8d85\u65f6\u65f6\u95f4\uff0c\u5f53\u5143\u7d20\u6240\u6709\u5217\u8868\u7684\u5143\u7d20\u83b7\u53d6\u5b8c\u4e4b\u540e\uff0c\u963b\u585e\u7b49\u5f85\u5217\u8868\u5185\u6709\u6570\u636e\u7684\u65f6\u95f4\uff08\u79d2\uff09, 0 \u8868\u793a\u6c38\u8fdc\u963b\u585e<\/span> <\/p>\n

\n r.brpop(keys, timeout) \u540c blpop\uff0c\u5c06\u591a\u4e2a\u5217\u8868\u6392\u5217,\u6309\u7167\u4ece\u53f3\u50cf\u5de6\u53bb\u79fb\u9664\u5404\u4e2a\u5217\u8868\u5185\u7684\u5143\u7d20\n <\/div>\n
\n
 r.lpush<\/span>(\"list10\"<\/span><\/span><\/span>, 3, 4, 5)
r.lpush<\/span>(\"list11\"<\/span><\/span><\/span>, 3, 4, 5)
while True<\/span>:
    r.blpop<\/span>([\"list10\", \"list11\"], timeout=2)
    print(r.lrange<\/span>(\"list10\", 0, -1), r.lrange<\/span>(\"list11\", 0, -1))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 14.\u81ea\u5b9a\u4e49\u589e\u91cf\u8fed\u4ee3\n <\/div>\n
\n \u7531\u4e8eredis\u7c7b\u5e93\u4e2d\u6ca1\u6709\u63d0\u4f9b\u5bf9\u5217\u8868\u5143\u7d20\u7684\u589e\u91cf\u8fed\u4ee3\uff0c\u5982\u679c\u60f3\u8981\u5faa\u73afname\u5bf9\u5e94\u7684\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u83b7\u53d6name\u5bf9\u5e94\u7684\u6240\u6709\u5217\u8868\u3002\u4f46\u662f\uff0c\u5982\u679c\u5217\u8868\u975e\u5e38\u5927\uff0c\u90a3\u4e48\u5c31\u6709\u53ef\u80fd\u5728\u7b2c\u4e00\u6b65\u65f6\u5c31\u5c06\u7a0b\u5e8f\u7684\u5185\u5bb9\u6491\u7206\uff0c\u6240\u6709\u6709\u5fc5\u8981\u81ea\u5b9a\u4e49\u4e00\u4e2a\u589e\u91cf\u8fed\u4ee3\u7684\u529f\u80fd\uff1a\n <\/div>\n
\n
 def <\/span>list_iter(name):
    \"\"<\/span><\/span>\"
    \u81ea\u5b9a\u4e49redis\u5217\u8868\u589e\u91cf\u8fed\u4ee3
    :param name: redis\u4e2d\u7684name\uff0c\u5373\uff1a\u8fed\u4ee3name\u5bf9\u5e94\u7684\u5217\u8868
    :return: yield <\/span>\u8fd4\u56de \u5217\u8868\u5143\u7d20
    \"\"\"
    list_count = r.llen<\/span>(name)
    for <\/span><\/span>index in <\/span>range(list_count):
        yield <\/span>r.lindex<\/span>(name, index)
# \u4f7f\u7528
<\/span> for <\/span><\/span>item in <\/span>list_iter('list2'<\/span>): # \u904d\u5386\u8fd9\u4e2a\u5217\u8868
<\/span>     print(item)
<\/span><\/code><\/pre>\n<\/p><\/div>\n

Redis\u57fa\u672c\u547d\u4ee4 set<\/h2>\n
\n 1.\u65b0\u589e<\/strong>\n <\/div>\n
\n
 sadd(name,values) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n name - \u5bf9\u5e94\u7684\u96c6\u5408\u4e2d\u6dfb\u52a0\u5143\u7d20\n <\/div>\n
\n
 r.sadd<\/span>(\"set1\"<\/span><\/span><\/span>, 33, 44, 55, 66)  # \u5f80\u96c6\u5408\u4e2d\u6dfb\u52a0\u5143\u7d20
<\/span> print(r.scard<\/span>(\"set1\"))  # \u96c6\u5408\u7684\u957f\u5ea6\u662f4
<\/span> print(r.smembers<\/span>(\"set1\"))   # \u83b7\u53d6\u96c6\u5408\u4e2d\u6240\u6709\u7684\u6210\u5458
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 2.\u83b7\u53d6\u5143\u7d20\u4e2a\u6570 \u7c7b\u4f3c\u4e8elen<\/strong>\n <\/div>\n
\n
 scard(name) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u83b7\u53d6name\u5bf9\u5e94\u7684\u96c6\u5408\u4e2d\u5143\u7d20\u4e2a\u6570\n <\/div>\n
\n
 print(r.scard<\/span>(\"set1\"<\/span>)) # \u96c6\u5408\u7684\u957f\u5ea6\u662f4<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 3.\u83b7\u53d6\u96c6\u5408\u4e2d\u6240\u6709\u7684\u6210\u5458<\/strong>\n <\/div>\n
\n
 smembers(name) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u83b7\u53d6name\u5bf9\u5e94\u7684\u96c6\u5408\u7684\u6240\u6709\u6210\u5458\n <\/div>\n
\n
 print(r.smembers<\/span>(\"set1\"<\/span>)) # \u83b7\u53d6\u96c6\u5408\u4e2d\u6240\u6709\u7684\u6210\u5458<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u83b7\u53d6\u96c6\u5408\u4e2d\u6240\u6709\u7684\u6210\u5458--\u5143\u7ec4\u5f62\u5f0f\n <\/div>\n
\n
 sscan(name, cursor=0, match=None, count=None) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5b9e\u4f8b\uff1a\n <\/div>\n
\n
 print(r.sscan<\/span>(\"set1\"<\/span>)) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u83b7\u53d6\u96c6\u5408\u4e2d\u6240\u6709\u7684\u6210\u5458--\u8fed\u4ee3\u5668\u7684\u65b9\u5f0f\n <\/div>\n
\n
 sscan_iter(name, match=None, count=None) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u540c\u5b57\u7b26\u4e32\u7684\u64cd\u4f5c\uff0c\u7528\u4e8e\u589e\u91cf\u8fed\u4ee3\u5206\u6279\u83b7\u53d6\u5143\u7d20\uff0c\u907f\u514d\u5185\u5b58\u6d88\u8017\u592a\u5927\n <\/div>\n
\n
 for <\/span><\/span>i in <\/span>r.sscan_iter<\/span>(\"set1\"<\/span>):
    print(i) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 4.\u5dee\u96c6<\/strong>\n <\/div>\n
\n
 sdiff(keys, *args) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728\u7b2c\u4e00\u4e2aname\u5bf9\u5e94\u7684\u96c6\u5408\u4e2d\u4e14\u4e0d\u5728\u5176\u4ed6name\u5bf9\u5e94\u7684\u96c6\u5408\u7684\u5143\u7d20\u96c6\u5408\n <\/div>\n
\n
 r.sadd<\/span>(\"set2\"<\/span><\/span><\/span><\/span>, 11, 22, 33)
print(r.smembers<\/span>(\"set1\"<\/span><\/span><\/span>))   # \u83b7\u53d6\u96c6\u5408\u4e2d\u6240\u6709\u7684\u6210\u5458
<\/span> print(r.smembers<\/span>(\"set2\"))
print(r.sdiff<\/span>(\"set1\", \"set2\"))   # \u5728\u96c6\u5408set1\u4f46\u662f\u4e0d\u5728\u96c6\u5408set2\u4e2d
<\/span> print(r.sdiff<\/span>(\"set2\", \"set1\"))   # \u5728\u96c6\u5408set2\u4f46\u662f\u4e0d\u5728\u96c6\u5408set1\u4e2d
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 5.\u5dee\u96c6--\u5dee\u96c6\u5b58\u5728\u4e00\u4e2a\u65b0\u7684\u96c6\u5408\u4e2d<\/strong>\n <\/div>\n
\n
 sdiffstore(dest, keys, *args) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u83b7\u53d6\u7b2c\u4e00\u4e2aname\u5bf9\u5e94\u7684\u96c6\u5408\u4e2d\u4e14\u4e0d\u5728\u5176\u4ed6name\u5bf9\u5e94\u7684\u96c6\u5408\uff0c\u518d\u5c06\u5176\u65b0\u52a0\u5165\u5230dest\u5bf9\u5e94\u7684\u96c6\u5408\u4e2d\n <\/div>\n
\n
 r.sdiffstore<\/span>(\"set3\"<\/span><\/span>, \"set1\"<\/span>, \"set2\"<\/span>)    # \u5728\u96c6\u5408set1\u4f46\u662f\u4e0d\u5728\u96c6\u5408set2\u4e2d
<\/span> print(r.smembers<\/span>(\"set3\"))   # \u83b7\u53d6\u96c6\u54083\u4e2d\u6240\u6709\u7684\u6210\u5458
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 6.\u4ea4\u96c6<\/strong>\n <\/div>\n
\n
 sinter(keys, *args) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u83b7\u53d6\u591a\u4e00\u4e2aname\u5bf9\u5e94\u96c6\u5408\u7684\u4ea4\u96c6\n <\/div>\n
\n
 print(r.sinter<\/span>(\"set1\"<\/span>, \"set2\"<\/span>)) # \u53d62\u4e2a\u96c6\u5408\u7684\u4ea4\u96c6<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 7.\u4ea4\u96c6--\u4ea4\u96c6\u5b58\u5728\u4e00\u4e2a\u65b0\u7684\u96c6\u5408\u4e2d<\/strong>\n <\/div>\n
\n
 sinterstore(dest, keys, *args) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u83b7\u53d6\u591a\u4e00\u4e2aname\u5bf9\u5e94\u96c6\u5408\u7684\u5e76\u96c6\uff0c\u518d\u5c06\u5176\u52a0\u5165\u5230dest\u5bf9\u5e94\u7684\u96c6\u5408\u4e2d\n <\/div>\n
\n
 print(r.sinterstore<\/span>(\"set3\"<\/span><\/span>, \"set1\"<\/span>, \"set2\"<\/span>)) # \u53d62\u4e2a\u96c6\u5408\u7684\u4ea4\u96c6
<\/span> print(r.smembers<\/span>(\"set3\")) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5e76\u96c6\n <\/div>\n
\n
 sunion(keys, *args) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u83b7\u53d6\u591a\u4e2aname\u5bf9\u5e94\u7684\u96c6\u5408\u7684\u5e76\u96c6\n <\/div>\n
\n
 print(r.sunion<\/span>(\"set1\"<\/span>, \"set2\"<\/span>)) # \u53d62\u4e2a\u96c6\u5408\u7684\u5e76\u96c6<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5e76\u96c6--\u5e76\u96c6\u5b58\u5728\u4e00\u4e2a\u65b0\u7684\u96c6\u5408\n <\/div>\n
\n
 sunionstore(dest,keys, *args) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u83b7\u53d6\u591a\u4e00\u4e2aname\u5bf9\u5e94\u7684\u96c6\u5408\u7684\u5e76\u96c6\uff0c\u5e76\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230dest\u5bf9\u5e94\u7684\u96c6\u5408\u4e2d\n <\/div>\n
\n
 print(r.sunionstore<\/span>(\"set3\"<\/span><\/span>, \"set1\"<\/span>, \"set2\"<\/span>)) # \u53d62\u4e2a\u96c6\u5408\u7684\u5e76\u96c6
<\/span> print(r.smembers<\/span>(\"set3\")) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 8.\u5224\u65ad\u662f\u5426\u662f\u96c6\u5408\u7684\u6210\u5458 \u7c7b\u4f3cin<\/strong>\n <\/div>\n
\n
 sismember(name, value) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u68c0\u67e5value\u662f\u5426\u662fname\u5bf9\u5e94\u7684\u96c6\u5408\u7684\u6210\u5458\uff0c\u7ed3\u679c\u4e3aTrue\u548cFalse\n <\/div>\n
\n
 print(r.sismember<\/span>(\"set1\"<\/span><\/span>, 33)) # 33\u662f\u96c6\u5408\u7684\u6210\u5458
<\/span> print(r.sismember<\/span>(\"set1\", 23)) # 23\u4e0d\u662f\u96c6\u5408\u7684\u6210\u5458<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 9.\u79fb\u52a8<\/strong>\n <\/div>\n
\n
 smove(src, dst, value) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5c06\u67d0\u4e2a\u6210\u5458\u4ece\u4e00\u4e2a\u96c6\u5408\u4e2d\u79fb\u52a8\u5230\u53e6\u5916\u4e00\u4e2a\u96c6\u5408\n <\/div>\n
\n
 r.smove<\/span>(\"set1\"<\/span><\/span>, \"set2\"<\/span><\/span>, 44)
print(r.smembers<\/span>(\"set1\"))
print(r.smembers<\/span>(\"set2\")) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 10.\u5220\u9664--\u968f\u673a\u5220\u9664\u5e76\u4e14\u8fd4\u56de\u88ab\u5220\u9664\u503c<\/strong>\n <\/div>\n
\n
 spop(name) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4ece\u96c6\u5408\u79fb\u9664\u4e00\u4e2a\u6210\u5458\uff0c\u5e76\u5c06\u5176\u8fd4\u56de,\u8bf4\u660e\u4e00\u4e0b\uff0c\u96c6\u5408\u662f\u65e0\u5e8f\u7684\uff0c\u6240\u6709\u662f\u968f\u673a\u5220\u9664\u7684\n <\/div>\n
\n
 print(r.spop<\/span>(\"set2\"<\/span><\/span>)) # \u8fd9\u4e2a\u5220\u9664\u7684\u503c\u662f\u968f\u673a\u5220\u9664\u7684\uff0c\u96c6\u5408\u662f\u65e0\u5e8f\u7684
<\/span> print(r.smembers<\/span>(\"set2\")) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 11.\u5220\u9664--\u6307\u5b9a\u503c\u5220\u9664<\/strong>\n <\/div>\n
\n
 srem(name, values) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728name\u5bf9\u5e94\u7684\u96c6\u5408\u4e2d\u5220\u9664\u67d0\u4e9b\u503c\n <\/div>\n
\n
 print(r.srem<\/span>(\"set2\"<\/span><\/span>, 11)) # \u4ece\u96c6\u5408\u4e2d\u5220\u9664\u6307\u5b9a\u503c 11
<\/span> print(r.smembers<\/span>(\"set2\")) <\/span><\/code><\/pre>\n<\/p><\/div>\n

7\u3001redis\u57fa\u672c\u547d\u4ee4 \u6709\u5e8fset<\/h2>\n
\n Set\u64cd\u4f5c\uff0cSet\u96c6\u5408\u5c31\u662f\u4e0d\u5141\u8bb8\u91cd\u590d\u7684\u5217\u8868\uff0c\u672c\u8eab\u662f\u65e0\u5e8f\u7684\u3002\n <\/div>\n
\n \u6709\u5e8f\u96c6\u5408\uff0c\u5728\u96c6\u5408\u7684\u57fa\u7840\u4e0a\uff0c\u4e3a\u6bcf\u5143\u7d20\u6392\u5e8f\uff1b\u5143\u7d20\u7684\u6392\u5e8f\u9700\u8981\u6839\u636e\u53e6\u5916\u4e00\u4e2a\u503c\u6765\u8fdb\u884c\u6bd4\u8f83\uff0c\u6240\u4ee5\uff0c\u5bf9\u4e8e\u6709\u5e8f\u96c6\u5408\uff0c\u6bcf\u4e00\u4e2a\u5143\u7d20\u6709\u4e24\u4e2a\u503c\uff0c\u5373\uff1a\u503c\u548c\u5206\u6570\uff0c\u5206\u6570\u4e13\u95e8\u7528\u6765\u505a\u6392\u5e8f\u3002\n <\/div>\n
\n 1.\u65b0\u589e<\/strong>\n <\/div>\n
\n
 zadd(name, *args, **kwargs) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728name\u5bf9\u5e94\u7684\u6709\u5e8f\u96c6\u5408\u4e2d\u6dfb\u52a0\u5143\u7d20\n <\/div>\n
\n \u5982\uff1a\n <\/div>\n
\n
 import <\/span>redis
import <\/span>time
pool = redis.ConnectionPool<\/span>(host='localhost'<\/span>, port=6379, decode_responses=True)
r = redis.Redis<\/span>(connection_pool=pool)
r.zadd<\/span>(\"zset1\"<\/span><\/span><\/span>, n1=11, n2=22)
r.zadd<\/span>(\"zset2\"<\/span><\/span><\/span>, 'm1'<\/span>, 22, 'm2'<\/span>, 44)
print(r.zcard<\/span>(\"zset1\")) # \u96c6\u5408\u957f\u5ea6
<\/span><\/span> print(r.zcard<\/span>(\"zset2\"))
print(r.zrange<\/span>(\"zset1\", 0, -1))   # \u83b7\u53d6\u6709\u5e8f\u96c6\u5408\u4e2d\u6240\u6709\u5143\u7d20
<\/span> print(r.zrange<\/span>(\"zset2\", 0, -1, withscores=True))   # \u83b7\u53d6\u6709\u5e8f\u96c6\u5408\u4e2d\u6240\u6709\u5143\u7d20\u548c\u5206\u6570
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 2.\u83b7\u53d6\u6709\u5e8f\u96c6\u5408\u5143\u7d20\u4e2a\u6570 \u7c7b\u4f3c\u4e8elen<\/strong>\n <\/div>\n
\n
 zcard(name) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u83b7\u53d6name\u5bf9\u5e94\u7684\u6709\u5e8f\u96c6\u5408\u5143\u7d20\u7684\u6570\u91cf\n <\/div>\n
\n
 print(r.zcard<\/span>(\"zset1\"<\/span>)) # \u96c6\u5408\u957f\u5ea6<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 3.\u83b7\u53d6\u6709\u5e8f\u96c6\u5408\u7684\u6240\u6709\u5143\u7d20<\/strong>\n <\/div>\n
\n
 r.zrange<\/span>( name, start, end, desc=False<\/span>, withscores=False<\/span>, score_cast_func=float) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6309\u7167\u7d22\u5f15\u8303\u56f4\u83b7\u53d6name\u5bf9\u5e94\u7684\u6709\u5e8f\u96c6\u5408\u7684\u5143\u7d20\n <\/div>\n
\n \u53c2\u6570\uff1a\n <\/div>\n

name - redis\u7684name<\/span>
\n start - \u6709\u5e8f\u96c6\u5408\u7d22\u5f15\u8d77\u59cb\u4f4d\u7f6e\uff08\u975e\u5206\u6570\uff09<\/span>
\n end - \u6709\u5e8f\u96c6\u5408\u7d22\u5f15\u7ed3\u675f\u4f4d\u7f6e\uff08\u975e\u5206\u6570\uff09<\/span>
\n desc - \u6392\u5e8f\u89c4\u5219\uff0c\u9ed8\u8ba4\u6309\u7167\u5206\u6570\u4ece\u5c0f\u5230\u5927\u6392\u5e8f<\/span>
\n withscores - \u662f\u5426\u83b7\u53d6\u5143\u7d20\u7684\u5206\u6570\uff0c\u9ed8\u8ba4\u53ea\u83b7\u53d6\u5143\u7d20\u7684\u503c<\/span>
\n score_cast_func - \u5bf9\u5206\u6570\u8fdb\u884c\u6570\u636e\u8f6c\u6362\u7684\u51fd\u6570<\/span> <\/p>\n

\n 3-1 \u4ece\u5927\u5230\u5c0f\u6392\u5e8f(\u540czrange\uff0c\u96c6\u5408\u662f\u4ece\u5927\u5230\u5c0f\u6392\u5e8f\u7684)\n <\/div>\n
\n
 zrevrange(name, start, end, withscores=False<\/span>, score_cast_func=float) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5b9e\u4f8b\n <\/div>\n
\n
 print(r.zrevrange<\/span>(\"zset1\"<\/span><\/span>, 0, -1)) # \u53ea\u83b7\u53d6\u5143\u7d20\uff0c\u4e0d\u663e\u793a\u5206\u6570
<\/span> print(r.zrevrange<\/span>(\"zset1\", 0, -1, withscores=True)) # \u83b7\u53d6\u6709\u5e8f\u96c6\u5408\u4e2d\u6240\u6709\u5143\u7d20\u548c\u5206\u6570,\u5206\u6570\u5012\u5e8f<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 3-2 \u6309\u7167\u5206\u6570\u8303\u56f4\u83b7\u53d6name\u5bf9\u5e94\u7684\u6709\u5e8f\u96c6\u5408\u7684\u5143\u7d20\n <\/div>\n
\n zrangebyscore(name, min, max, start=None, num=None, withscores=False, score_cast_func=float)\n <\/div>\n
\n \u5b9e\u4f8b\n <\/div>\n
\n
 for <\/span><\/span>i in <\/span>range(1, 30):
   element = 'n'<\/span> + str(i)
   r.zadd<\/span>(\"zset3\"<\/span><\/span><\/span>, element, i)
print(r.zrangebyscore<\/span>(\"zset3\", 15, 25)) # # \u5728\u5206\u6570\u662f15-25\u4e4b\u95f4\uff0c\u53d6\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20
<\/span> print(r.zrangebyscore<\/span>(\"zset3\", 12, 22, withscores=True))    # \u5728\u5206\u6570\u662f12-22\u4e4b\u95f4\uff0c\u53d6\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\uff08\u5e26\u5206\u6570\uff09
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 3-3 \u6309\u7167\u5206\u6570\u8303\u56f4\u83b7\u53d6\u6709\u5e8f\u96c6\u5408\u7684\u5143\u7d20\u5e76\u6392\u5e8f\uff08\u9ed8\u8ba4\u4ece\u5927\u5230\u5c0f\u6392\u5e8f\uff09\n <\/div>\n
\n
 zrevrangebyscore(name, max, min, start=None, num=None, withscores=False<\/span>, score_cast_func=float) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5b9e\u4f8b\n <\/div>\n
\n
 print(r.zrevrangebyscore<\/span>(\"zset3\"<\/span>, 22, 11, withscores=True)) # \u5728\u5206\u6570\u662f22-11\u4e4b\u95f4\uff0c\u53d6\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20 \u6309\u7167<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n

\u5206\u6570\u5012\u5e8f <\/p>\n

\n 3-4 \u83b7\u53d6\u6240\u6709\u5143\u7d20--\u9ed8\u8ba4\u6309\u7167\u5206\u6570\u987a\u5e8f\u6392\u5e8f\n <\/div>\n
\n
 zscan(name, cursor=0, match=None, count=None, score_cast_func=float) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5b9e\u4f8b\n <\/div>\n
\n
 print(r.zscan<\/span>(\"zset3\"<\/span>)) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 3-5 \u83b7\u53d6\u6240\u6709\u5143\u7d20--\u8fed\u4ee3\u5668\n <\/div>\n
\n
 zscan_iter(name, match=None, count=None,score_cast_func=float) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5b9e\u4f8b\n <\/div>\n
\n
 for <\/span><\/span>i in <\/span>r.zscan_iter<\/span>(\"zset3\"<\/span>): # \u904d\u5386\u8fed\u4ee3\u5668
<\/span>     print(i) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 4.zcount(name, min, max)<\/strong>\n <\/div>\n
\n \u83b7\u53d6name\u5bf9\u5e94\u7684\u6709\u5e8f\u96c6\u5408\u4e2d\u5206\u6570 \u5728 [min,max] \u4e4b\u95f4\u7684\u4e2a\u6570\n <\/div>\n
\n
 print(r.zrange<\/span>(\"zset3\"<\/span><\/span>, 0, -1, withscores=True))
print(r.zcount<\/span>(\"zset3\", 11, 22)) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 5.\u81ea\u589e<\/strong>\n <\/div>\n
\n
 zincrby(name, value, amount) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u81ea\u589ename\u5bf9\u5e94\u7684\u6709\u5e8f\u96c6\u5408\u7684 name \u5bf9\u5e94\u7684\u5206\u6570\n <\/div>\n
\n
 r.zincrby<\/span>(\"zset3\"<\/span><\/span>, \"n2\"<\/span>, amount=2) # \u6bcf\u6b21\u5c06n2\u7684\u5206\u6570\u81ea\u589e2
<\/span> print(r.zrange<\/span>(\"zset3\", 0, -1, withscores=True)) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 6.\u83b7\u53d6\u503c\u7684\u7d22\u5f15\u53f7<\/strong>\n <\/div>\n
\n
 zrank(name, value) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u83b7\u53d6\u67d0\u4e2a\u503c\u5728 name\u5bf9\u5e94\u7684\u6709\u5e8f\u96c6\u5408\u4e2d\u7684\u7d22\u5f15\uff08\u4ece 0 \u5f00\u59cb\uff09\n <\/div>\n
\n \u66f4\u591a\uff1a\n <\/div>\n
\n zrevrank(name, value)\uff0c\u4ece\u5927\u5230\u5c0f\u6392\u5e8f\u3002\n <\/div>\n
\n
 print(r.zrank<\/span>(\"zset3\"<\/span><\/span><\/span>, \"n1\"<\/span><\/span>))   # n1\u7684\u7d22\u5f15\u53f7\u662f0 \u8fd9\u91cc\u6309\u7167\u5206\u6570\u987a\u5e8f\uff08\u4ece\u5c0f\u5230\u5927\uff09
<\/span> print(r.zrank<\/span>(\"zset3\", \"n6\"<\/span>))   # n6\u7684\u7d22\u5f15\u53f7\u662f1
<\/span> print(r.zrevrank<\/span>(\"zset3\", \"n1\"))    # n1\u7684\u7d22\u5f15\u53f7\u662f29 \u8fd9\u91cc\u5b89\u7167\u5206\u6570\u5012\u5e8f\uff08\u4ece\u5927\u5230\u5c0f\uff09
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 7.\u5220\u9664--\u6307\u5b9a\u503c\u5220\u9664<\/strong>\n <\/div>\n
\n
 zrem(name, values) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5220\u9664name\u5bf9\u5e94\u7684\u6709\u5e8f\u96c6\u5408\u4e2d\u503c\u662fvalues\u7684\u6210\u5458\n <\/div>\n
\n
 r.zrem<\/span>(\"zset3\"<\/span><\/span>, \"n3\"<\/span>) # \u5220\u9664\u6709\u5e8f\u96c6\u5408\u4e2d\u7684\u5143\u7d20n3 \u5220\u9664\u5355\u4e2a
<\/span> print(r.zrange<\/span>(\"zset3\", 0, -1)) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 8.\u5220\u9664--\u6839\u636e\u6392\u884c\u8303\u56f4\u5220\u9664\uff0c\u6309\u7167\u7d22\u5f15\u53f7\u6765\u5220\u9664<\/strong>\n <\/div>\n
\n
 zremrangebyrank(name, min, max) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6839\u636e\u6392\u884c\u8303\u56f4\u5220\u9664\n <\/div>\n
\n
 r.zremrangebyrank<\/span>(\"zset3\"<\/span><\/span>, 0, 1) # \u5220\u9664\u6709\u5e8f\u96c6\u5408\u4e2d\u7684\u7d22\u5f15\u53f7\u662f0, 1\u7684\u5143\u7d20
<\/span> print(r.zrange<\/span>(\"zset3\", 0, -1)) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 9.\u5220\u9664--\u6839\u636e\u5206\u6570\u8303\u56f4\u5220\u9664<\/strong>\n <\/div>\n
\n
 zremrangebyscore(name, min, max) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6839\u636e\u5206\u6570\u8303\u56f4\u5220\u9664\n <\/div>\n
\n
 r.zremrangebyscore<\/span>(\"zset3\"<\/span><\/span>, 11, 22) # \u5220\u9664\u6709\u5e8f\u96c6\u5408\u4e2d\u7684\u5206\u6570\u662f11-22\u7684\u5143\u7d20
<\/span> print(r.zrange<\/span>(\"zset3\", 0, -1)) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 10.\u83b7\u53d6\u503c\u5bf9\u5e94\u7684\u5206\u6570<\/strong>\n <\/div>\n
\n
 zscore(name, value) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u83b7\u53d6name\u5bf9\u5e94\u6709\u5e8f\u96c6\u5408\u4e2d value \u5bf9\u5e94\u7684\u5206\u6570\n <\/div>\n
\n
 print(r.zscore<\/span>(\"zset3\"<\/span>, \"n27\"<\/span>)) # \u83b7\u53d6\u5143\u7d20n27\u5bf9\u5e94\u7684\u5206\u657027<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n

8\u3001\u5176\u4ed6\u5e38\u7528\u64cd\u4f5c<\/h2>\n
\n 1.\u5220\u9664<\/strong>\n <\/div>\n
\n
 delete(*names) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6839\u636e\u5220\u9664redis\u4e2d\u7684\u4efb\u610f\u6570\u636e\u7c7b\u578b\uff08string\u3001hash\u3001list\u3001set\u3001\u6709\u5e8fset\uff09\n <\/div>\n
\n
 r.delete<\/span>(\"gender\"<\/span>) # \u5220\u9664key\u4e3agender\u7684\u952e\u503c\u5bf9<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 2.\u68c0\u67e5\u540d\u5b57\u662f\u5426\u5b58\u5728<\/strong>\n <\/div>\n
\n
 exists(name) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u68c0\u6d4bredis\u7684name\u662f\u5426\u5b58\u5728\uff0c\u5b58\u5728\u5c31\u662fTrue\uff0cFalse \u4e0d\u5b58\u5728\n <\/div>\n
\n
 print(r.exists<\/span>(\"zset1\"<\/span>)) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 3.\u6a21\u7cca\u5339\u914d<\/strong>\n <\/div>\n
\n
 keys(pattern=''<\/span>) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6839\u636e\u6a21\u578b\u83b7\u53d6redis\u7684name\n <\/div>\n
\n \u66f4\u591a\uff1a\n <\/div>\n

KEYS * \u5339\u914d\u6570\u636e\u5e93\u4e2d\u6240\u6709 key \u3002<\/span>
\n KEYS h?llo \u5339\u914d hello \uff0c hallo \u548c hxllo \u7b49\u3002<\/span>
\n KEYS hllo \u5339\u914d hllo \u548c heeeeello \u7b49\u3002<\/span>
\n KEYS h[ae]llo \u5339\u914d hello \u548c hallo \uff0c\u4f46\u4e0d\u5339\u914d hillo<\/span> <\/p>\n

\n
 print(r.keys<\/span>(\"foo*\"<\/span>)) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 4.\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4<\/strong>\n <\/div>\n
\n
 expire(name ,time) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4e3a\u67d0\u4e2aredis\u7684\u67d0\u4e2aname\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\n <\/div>\n
\n
 r.lpush<\/span>(\"list5\"<\/span><\/span><\/span><\/span>, 11, 22)
r.expire<\/span>(\"list5\", time=3)
print(r.lrange<\/span>(\"list5\", 0, -1))
time.sleep<\/span>(3)
print(r.lrange<\/span>(\"list5\", 0, -1)) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 5.\u91cd\u547d\u540d<\/strong>\n <\/div>\n
\n
 rename(src, dst) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5bf9redis\u7684name\u91cd\u547d\u540d\n <\/div>\n
\n
 r.lpush<\/span>(\"list5\"<\/span><\/span>, 11, 22)
r.rename<\/span>(\"list5\", \"list5-1\"<\/span>) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 6.\u968f\u673a\u83b7\u53d6name<\/strong>\n <\/div>\n
\n
 randomkey() <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u968f\u673a\u83b7\u53d6\u4e00\u4e2aredis\u7684name\uff08\u4e0d\u5220\u9664\uff09\n <\/div>\n
\n
 print(r.randomkey<\/span>()) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 7.\u83b7\u53d6\u7c7b\u578b<\/strong>\n <\/div>\n
\n
 type(name) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u83b7\u53d6name\u5bf9\u5e94\u503c\u7684\u7c7b\u578b\n <\/div>\n
\n
 print(r.type<\/span>(\"set1\"<\/span>))
print(r.type<\/span>(\"hash2\"<\/span>)) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 8.\u67e5\u770b\u6240\u6709\u5143\u7d20<\/strong>\n <\/div>\n
\n
 scan(cursor=0, match=None, count=None)
print(r.hscan<\/span>(\"hash2\"<\/span>))
print(r.sscan<\/span>(\"set3\"<\/span><\/span>))
print(r.zscan<\/span>(\"zset2\"<\/span>))
print(r.getrange<\/span>(\"foo1\"<\/span>, 0, -1))
print(r.lrange<\/span>(\"list2\"<\/span>, 0, -1))
print(r.smembers<\/span>(\"set3\"))
print(r.zrange<\/span>(\"zset3\"<\/span>, 0, -1))
print(r.hgetall<\/span>(\"hash1\"<\/span>))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n 9.\u67e5\u770b\u6240\u6709\u5143\u7d20--\u8fed\u4ee3\u5668<\/strong>\n <\/div>\n
\n
 scan_iter(match=None, count=None)
for <\/span><\/span>i in <\/span>r.hscan_iter<\/span>(\"hash1\"<\/span>):
    print(i)
for <\/span><\/span>i in <\/span>r.sscan_iter<\/span>(\"set3\"<\/span>):
    print(i)
for <\/span><\/span>i in <\/span>r.zscan_iter<\/span>(\"zset3\"<\/span>):
    print(i)
<\/span><\/code><\/pre>\n<\/p><\/div>\n

other \u65b9\u6cd5<\/h3>\n
\n
 print(r.get<\/span>('name'<\/span>))    # \u67e5\u8be2key\u4e3aname\u7684\u503c
<\/span> r.delete<\/span>(\"gender\"<\/span>)  # \u5220\u9664key\u4e3agender\u7684\u952e\u503c\u5bf9
<\/span> print(r.keys<\/span>()) # \u67e5\u8be2\u6240\u6709\u7684Key
<\/span> print(r.dbsize<\/span>())   # \u5f53\u524dredis\u5305\u542b\u591a\u5c11\u6761\u6570\u636e
<\/span> r.save<\/span>()    # \u6267\u884c\"\u68c0\u67e5\u70b9\"<\/span>\u64cd\u4f5c\uff0c\u5c06\u6570\u636e\u5199\u56de\u78c1\u76d8\u3002\u4fdd\u5b58\u65f6\u963b\u585e
<\/span> # r.flushdb<\/span>()        # \u6e05\u7a7ar\u4e2d\u7684\u6240\u6709\u6570\u636e
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n

\u7ba1\u9053\uff08pipeline\uff09<\/h3>\n
\n redis\u9ed8\u8ba4\u5728\u6267\u884c\u6bcf\u6b21\u8bf7\u6c42\u90fd\u4f1a\u521b\u5efa\uff08\u8fde\u63a5\u6c60\u7533\u8bf7\u8fde\u63a5\uff09\u548c\u65ad\u5f00\uff08\u5f52\u8fd8\u8fde\u63a5\u6c60\uff09\u4e00\u6b21\u8fde\u63a5\u64cd\u4f5c\uff0c\u5982\u679c\u60f3\u8981\u5728\u4e00\u6b21\u8bf7\u6c42\u4e2d\u6307\u5b9a\u591a\u4e2a\u547d\u4ee4\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528pipline\u5b9e\u73b0\u4e00\u6b21\u8bf7\u6c42\u6307\u5b9a\u591a\u4e2a\u547d\u4ee4\uff0c\u5e76\u4e14\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4e00\u6b21pipline \u662f\u539f\u5b50\u6027\u64cd\u4f5c\u3002\n <\/div>\n
\n \u7ba1\u9053\uff08pipeline\uff09\u662fredis\u5728\u63d0\u4f9b\u5355\u4e2a\u8bf7\u6c42\u4e2d\u7f13\u51b2\u591a\u6761\u670d\u52a1\u5668\u547d\u4ee4\u7684\u57fa\u7c7b\u7684\u5b50\u7c7b\u3002\u5b83\u901a\u8fc7\u51cf\u5c11\u670d\u52a1\u5668-\u5ba2\u6237\u7aef\u4e4b\u95f4\u53cd\u590d\u7684TCP\u6570\u636e\u5e93\u5305\uff0c\u4ece\u800c\u5927\u5927\u63d0\u9ad8\u4e86\u6267\u884c\u6279\u91cf\u547d\u4ee4\u7684\u529f\u80fd\u3002\n <\/div>\n
\n
 import <\/span>redis
import <\/span>time
pool = redis.ConnectionPool<\/span>(host='localhost'<\/span>, port=6379, decode_responses=True)
r = redis.Redis<\/span>(connection_pool=pool)
pipe = r.pipeline<\/span>() # \u521b\u5efa\u4e00\u4e2a\u7ba1\u9053
<\/span> pipe.set<\/span>('name'<\/span>, 'jack'<\/span>)
pipe.set<\/span>('role'<\/span>, 'sb'<\/span>)
pipe.sadd<\/span>('faz'<\/span>, 'baz'<\/span>)
pipe.incr<\/span>('num'<\/span>)     # \u5982\u679cnum\u4e0d\u5b58\u5728\u5219vaule\u4e3a1\uff0c\u5982\u679c\u5b58\u5728\uff0c\u5219value\u81ea\u589e1
<\/span> pipe.execute<\/span>()
print(r.get<\/span>(\"name\"<\/span>))
print(r.get<\/span>(\"role\"<\/span>))
print(r.get<\/span>(\"num\"<\/span>))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u7ba1\u9053\u7684\u547d\u4ee4\u53ef\u4ee5\u5199\u5728\u4e00\u8d77\uff0c\u5982\uff1a\n <\/div>\n
\n
 pipe.set<\/span>('hello'<\/span>, 'redis'<\/span>).sadd<\/span>('faz'<\/span>, 'baz'<\/span>).incr<\/span>('num'<\/span>).execute<\/span>()
print(r.get<\/span>(\"name\"<\/span>))
print(r.get<\/span>(\"role\"<\/span>))
print(r.get<\/span>(\"num\"<\/span>))
<\/span><\/code><\/pre>\n<\/p><\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python rediszh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[],"class_list":["post-1057","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1057"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1057"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1057\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}