{"id":201598,"date":"2025-05-10T11:18:10","date_gmt":"2025-05-10T03:18:10","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201598\/"},"modified":"2025-05-10T11:18:10","modified_gmt":"2025-05-10T03:18:10","slug":"python%e4%b8%ad%e4%bd%bf%e7%94%a8redis%e7%94%a8%e6%b3%95%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201598\/","title":{"rendered":"python\u4e2d\u4f7f\u7528redis\u7528\u6cd5\u8be6\u89e3"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>python\u4e2d\u4f7f\u7528redis\u7528\u6cd5\u8be6\u89e3<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929golang\u5b66\u4e60\u7f51\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300apython\u4e2d\u4f7f\u7528redis\u7528\u6cd5\u8be6\u89e3\u300b\uff0c\u804a\u804apythonredis\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p>Redis\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u57fa\u4e8e\u5185\u5b58\u4e5f\u53ef\u6301\u4e45\u5316\u7684Key-Value\u6570\u636e\u5e93\uff0c\u91c7\u7528ANSI C\u8bed\u8a00\u7f16\u5199\u3002\u5b83\u62e5\u6709\u4e30\u5bcc\u7684\u6570\u636e\u7ed3\u6784\uff0c\u62e5\u6709\u4e8b\u52a1\u529f\u80fd\uff0c\u4fdd\u8bc1\u547d\u4ee4\u7684\u539f\u5b50\u6027\u3002\u7531\u4e8e\u662f\u5185\u5b58\u6570\u636e\u5e93\uff0c\u8bfb\u5199\u975e\u5e38\u9ad8\u901f\uff0c\u53ef\u8fbe10w\/s\u7684\u8bc4\u7387\uff0c\u6240\u4ee5\u4e00\u822c\u5e94\u7528\u4e8e\u6570\u636e\u53d8\u5316\u5feb\u3001\u5b9e\u65f6\u901a\u8baf\u3001\u7f13\u5b58\u7b49\u3002\u4f46\u5185\u5b58\u6570\u636e\u5e93\u901a\u5e38\u8981\u8003\u8651\u673a\u5668\u7684\u5185\u5b58\u5927\u5c0f\u3002<\/p>\n<p>redis\u652f\u6301\u5b58\u50a8\u7684value\u7c7b\u578b\u6709\u5f88\u591a\u79cd\uff0c\u5982string(\u5b57\u7b26\u4e32)\u3001list(\u94fe\u8868)\u3001set(\u96c6\u5408)\u3001zset(\u6709\u5e8f\u96c6\u5408)\u548chash(\u54c8\u5e0c\u7c7b\u578b)\u3002<\/p>\n<p>\u4e3a\u4e86\u4fdd\u8bc1\u6548\u7387\uff0c\u6570\u636e\u90fd\u662f\u7f13\u5b58\u5728\u5185\u5b58\u4e2d\u3002\u53ef\u4ee5\u5468\u671f\u6027\u7684\u628a\u66f4\u65b0\u7684\u6570\u636e\u5199\u5165\u78c1\u76d8\u6216\u8005\u628a\u4fee\u6539\u64cd\u4f5c\u5199\u5165\u8ffd\u52a0\u6587\u4ef6\u3002Redis\u63d0\u4f9b\u4e86Python\uff0cRuby\uff0cJava\uff0cPhp\u7684\u5ba2\u6237\u7aef\uff0c\u4f7f\u7528\u5f88\u65b9\u4fbf\uff0c\u672c\u6587\u4e3b\u8981\u4ee5Python\u6765\u8bb2\u89e3Redis\u7684\u64cd\u4f5c\u3002<\/p>\n<h2>\u5b89\u88c5redis<\/h2>\n<pre>pip install redis\n<\/pre>\n<p>\u8fde\u63a5redis\uff0c\u52a0\u4e0adecode_responses=True\uff0c\u5199\u5165\u7684\u952e\u503c\u5bf9\u4e2d\u7684value\u4e3astr\u7c7b\u578b\uff0c\u4e0d\u52a0\u8fd9\u4e2a\u53c2\u6570\u5199\u5165\u7684\u5219\u4e3a\u5b57\u8282\u7c7b\u578b\u3002<\/p>\n<pre>import redis   # \u5bfc\u5165redis\u6a21\u5757\uff0c\u901a\u8fc7python\u64cd\u4f5credis \u4e5f\u53ef\u4ee5\u76f4\u63a5\u5728redis\u4e3b\u673a\u7684\u670d\u52a1\u7aef\u64cd\u4f5c\u7f13\u5b58\u6570\u636e\u5e93\n\nr = redis.Redis(host='localhost', port=6379, decode_responses=True)   # host\u662fredis\u4e3b\u673a\uff0c\u9700\u8981redis\u670d\u52a1\u7aef\u548c\u5ba2\u6237\u7aef\u90fd\u542f\u52a8 redis\u9ed8\u8ba4\u7aef\u53e3\u662f6379\nr.set('name', 'junxi')  # key\u662f\"foo\" value\u662f\"bar\" \u5c06\u952e\u503c\u5bf9\u5b58\u5165redis\u7f13\u5b58\nprint(r['name'])\nprint(r.get('name'))  # \u53d6\u51fa\u952ename\u5bf9\u5e94\u7684\u503c\nprint(type(r.get('name')))\n<\/pre>\n<h2>\u8fde\u63a5\u6c60<\/h2>\n<p>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<\/p>\n<p>\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<\/p>\n<pre>import redis &nbsp; &nbsp;# \u5bfc\u5165redis \u6a21\u5757\n\npool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)\nr = redis.Redis(host='localhost', port=6379, decode_responses=True) &nbsp;\nr.set('name', 'runoob') &nbsp;# \u8bbe\u7f6e name \u5bf9\u5e94\u7684\u503c\nprint(r.get('name')) &nbsp;# \u53d6\u51fa\u952e name \u5bf9\u5e94\u7684\u503c<\/pre>\n<h2>redis\u7684String\u7c7b\u578b\u57fa\u672c\u547d\u4ee4\u793a\u4f8b<\/h2>\n<p>set(name, value, ex=None, px=None, nx=False, xx=False)<\/p>\n<p>\u5728 Redis \u4e2d\u8bbe\u7f6e\u503c\uff0c\u9ed8\u8ba4\uff0c\u4e0d\u5b58\u5728\u5219\u521b\u5efa\uff0c\u5b58\u5728\u5219\u4fee\u6539\u3002<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>ex &#8211; \u8fc7\u671f\u65f6\u95f4\uff08\u79d2\uff09<\/li>\n<li>px &#8211; \u8fc7\u671f\u65f6\u95f4\uff08\u6beb\u79d2\uff09<\/li>\n<li>nx &#8211; \u5982\u679c\u8bbe\u7f6e\u4e3aTrue\uff0c\u5219\u53ea\u6709name\u4e0d\u5b58\u5728\u65f6\uff0c\u5f53\u524dset\u64cd\u4f5c\u624d\u6267\u884c<\/li>\n<li>xx &#8211; \u5982\u679c\u8bbe\u7f6e\u4e3aTrue\uff0c\u5219\u53ea\u6709name\u5b58\u5728\u65f6\uff0c\u5f53\u524dset\u64cd\u4f5c\u624d\u6267\u884c<\/li>\n<\/ul>\n<h3>1.ex &#8211; \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<\/h3>\n<pre>import redis\n\npool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)\nr = redis.Redis(connection_pool=pool)\nr.set('food', 'mutton', ex=3) &nbsp; &nbsp;# key\u662f\"food\" value\u662f\"mutton\" \u5c06\u952e\u503c\u5bf9\u5b58\u5165redis\u7f13\u5b58\nprint(r.get('food')) &nbsp;# mutton \u53d6\u51fa\u952efood\u5bf9\u5e94\u7684\u503c<\/pre>\n<h3>2.px &#8211; \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<\/h3>\n<pre>import redis\n\npool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)\nr = redis.Redis(connection_pool=pool)\nr.set('food', 'beef', px=3)\nprint(r.get('food'))<\/pre>\n<h3>3.nx &#8211; \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<\/h3>\n<pre>import redis\n\npool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)\nr = redis.Redis(connection_pool=pool)\nprint(r.set('fruit', 'watermelon', nx=True)) &nbsp; &nbsp;# True--\u4e0d\u5b58\u5728\n# \u5982\u679c\u952efruit\u4e0d\u5b58\u5728\uff0c\u90a3\u4e48\u8f93\u51fa\u662fTrue\uff1b\u5982\u679c\u952efruit\u5df2\u7ecf\u5b58\u5728\uff0c\u8f93\u51fa\u662fNone<\/pre>\n<h3>4.xx &#8211; \u5982\u679c\u8bbe\u7f6e\u4e3aTrue\uff0c\u5219\u53ea\u6709name\u5b58\u5728\u65f6\uff0c\u5f53\u524dset\u64cd\u4f5c\u624d\u6267\u884c \uff08\u4fee\u6539\uff09<\/h3>\n<pre>print((r.set('fruit', 'watermelon', xx=True))) &nbsp; # True--\u5df2\u7ecf\u5b58\u5728\n# \u5982\u679c\u952efruit\u5df2\u7ecf\u5b58\u5728\uff0c\u90a3\u4e48\u8f93\u51fa\u662fTrue\uff1b\u5982\u679c\u952efruit\u4e0d\u5b58\u5728\uff0c\u8f93\u51fa\u662fNone<\/pre>\n<h3>5.setnx(name, value)<\/h3>\n<p>\u8bbe\u7f6e\u503c\uff0c\u53ea\u6709name\u4e0d\u5b58\u5728\u65f6\uff0c\u6267\u884c\u8bbe\u7f6e\u64cd\u4f5c\uff08\u6dfb\u52a0\uff09<\/p>\n<pre>print(r.setnx('fruit1', 'banana'))  # fruit1\u4e0d\u5b58\u5728\uff0c\u8f93\u51fa\u4e3aTrue<\/pre>\n<h3>6.setex(name, time, value)<\/h3>\n<p>\u8bbe\u7f6e\u503c<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<p>time &#8211; \u8fc7\u671f\u65f6\u95f4\uff08\u6570\u5b57\u79d2 \u6216 timedelta\u5bf9\u8c61\uff09<\/p>\n<pre>import redis\nimport time\n\npool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)\nr = redis.Redis(connection_pool=pool)\nr.setex(\"fruit2\", 5, \"orange\")\ntime.sleep(5)\nprint(r.get('fruit2')) &nbsp;# 5\u79d2\u540e\uff0c\u53d6\u503c\u5c31\u4eceorange\u53d8\u6210None<\/pre>\n<h3>7.psetex(name, time_ms, value)<\/h3>\n<p>\u8bbe\u7f6e\u503c<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<p>time_ms &#8211; \u8fc7\u671f\u65f6\u95f4\uff08\u6570\u5b57\u6beb\u79d2 \u6216 timedelta\u5bf9\u8c61\uff09<\/p>\n<pre>r.psetex(\"fruit3\", 5000, \"apple\")\ntime.sleep(5)\nprint(r.get('fruit3')) &nbsp;# 5000\u6beb\u79d2\u540e\uff0c\u53d6\u503c\u5c31\u4eceapple\u53d8\u6210None<\/pre>\n<h3>8.mset(*args, **kwargs)<\/h3>\n<p>\u6279\u91cf\u8bbe\u7f6e\u503c<\/p>\n<p>\u5982\uff1a<\/p>\n<pre>r.mget({'k1': 'v1', 'k2': 'v2'})\nr.mset(k1=\"v1\", k2=\"v2\") # \u8fd9\u91cck1 \u548ck2 \u4e0d\u80fd\u5e26\u5f15\u53f7\uff0c\u4e00\u6b21\u8bbe\u7f6e\u591a\u4e2a\u952e\u503c\u5bf9\nprint(r.mget(\"k1\", \"k2\")) &nbsp; # \u4e00\u6b21\u53d6\u51fa\u591a\u4e2a\u952e\u5bf9\u5e94\u7684\u503c\nprint(r.mget(\"k1\"))<\/pre>\n<h3>9.mget(keys, *args)<\/h3>\n<p>\u6279\u91cf\u83b7\u53d6<\/p>\n<p>\u5982\uff1a<\/p>\n<pre>print(r.mget('k1', 'k2'))\nprint(r.mget(['k1', 'k2']))\nprint(r.mget(\"fruit\", \"fruit1\", \"fruit2\", \"k1\", \"k2\")) &nbsp;# \u5c06\u76ee\u524dredis\u7f13\u5b58\u4e2d\u7684\u952e\u5bf9\u5e94\u7684\u503c\u6279\u91cf\u53d6\u51fa\u6765<\/pre>\n<h3>10.getset(name, value)<\/h3>\n<p>\u8bbe\u7f6e\u65b0\u503c\u5e76\u83b7\u53d6\u539f\u6765\u7684\u503c<\/p>\n<pre>print(r.getset(\"food\", \"barbecue\"))  # \u8bbe\u7f6e\u7684\u65b0\u503c\u662fbarbecue \u8bbe\u7f6e\u524d\u7684\u503c\u662fbeef<\/pre>\n<h3>11.getrange(key, start, end)<\/h3>\n<p>\u83b7\u53d6\u5b50\u5e8f\u5217\uff08\u6839\u636e\u5b57\u8282\u83b7\u53d6\uff0c\u975e\u5b57\u7b26\uff09<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>name &#8211; Redis \u7684 name<\/li>\n<li>start &#8211; \u8d77\u59cb\u4f4d\u7f6e\uff08\u5b57\u8282\uff09<\/li>\n<li>end &#8211; \u7ed3\u675f\u4f4d\u7f6e\uff08\u5b57\u8282\uff09<\/li>\n<\/ul>\n<p>\u5982\uff1a &#8220;\u541b\u60dc\u5927\u5927&#8221; \uff0c0-3\u8868\u793a &#8220;\u541b&#8221;<\/p>\n<pre>r.set(\"cn_name\", \"\u541b\u60dc\u5927\u5927\") # \u6c49\u5b57\nprint(r.getrange(\"cn_name\", 0, 2)) &nbsp; # \u53d6\u7d22\u5f15\u53f7\u662f0-2 \u524d3\u4f4d\u7684\u5b57\u8282 \u541b \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\nprint(r.getrange(\"cn_name\", 0, -1)) &nbsp;# \u53d6\u6240\u6709\u7684\u5b57\u8282 \u541b\u60dc\u5927\u5927 \u5207\u7247\u64cd\u4f5c\nr.set(\"en_name\",\"junxi\") # \u5b57\u6bcd\nprint(r.getrange(\"en_name\", 0, 2)) &nbsp;# \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\nprint(r.getrange(\"en_name\", 0, -1)) # \u53d6\u6240\u6709\u7684\u5b57\u8282 junxi \u5207\u7247\u64cd\u4f5c<\/pre>\n<h3>12.setrange(name, offset, value)<\/h3>\n<p>\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<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>offset &#8211; \u5b57\u7b26\u4e32\u7684\u7d22\u5f15\uff0c\u5b57\u8282\uff08\u4e00\u4e2a\u6c49\u5b57\u4e09\u4e2a\u5b57\u8282\uff09<\/li>\n<li>value &#8211; \u8981\u8bbe\u7f6e\u7684\u503c<\/li>\n<\/ul>\n<pre>r.setrange(\"en_name\", 1, \"ccc\")\nprint(r.get(\"en_name\")) &nbsp; &nbsp;# jccci \u539f\u59cb\u503c\u662fjunxi \u4ece\u7d22\u5f15\u53f7\u662f1\u5f00\u59cb\u66ff\u6362\u6210ccc \u53d8\u6210 jccci<\/pre>\n<h3>13.setbit(name, offset, value)<\/h3>\n<p>\u5bf9 name \u5bf9\u5e94\u503c\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u7684\u4f4d\u8fdb\u884c\u64cd\u4f5c<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>name &#8211; redis\u7684name<\/li>\n<li>offset &#8211; \u4f4d\u7684\u7d22\u5f15\uff08\u5c06\u503c\u53d8\u6362\u6210\u4e8c\u8fdb\u5236\u540e\u518d\u8fdb\u884c\u7d22\u5f15\uff09<\/li>\n<li>value &#8211; \u503c\u53ea\u80fd\u662f 1 \u6216 0<\/li>\n<\/ul>\n<p>\u6ce8\uff1a\u5982\u679c\u5728Redis\u4e2d\u6709\u4e00\u4e2a\u5bf9\u5e94\uff1a n1 = &#8220;foo&#8221;\uff0c<\/p>\n<p>\u90a3\u4e48\u5b57\u7b26\u4e32foo\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u4e3a\uff1a01100110 01101111 01101111<\/p>\n<p>\u6240\u4ee5\uff0c\u5982\u679c\u6267\u884c setbit(&#8216;n1&#8217;, 7, 1)\uff0c\u5219\u5c31\u4f1a\u5c06\u7b2c7\u4f4d\u8bbe\u7f6e\u4e3a1\uff0c<\/p>\n<p>\u90a3\u4e48\u6700\u7ec8\u4e8c\u8fdb\u5236\u5219\u53d8\u6210 01100111 01101111 01101111\uff0c\u5373\uff1a&#8221;goo&#8221;<\/p>\n<p>\u6269\u5c55\uff0c\u8f6c\u6362\u4e8c\u8fdb\u5236\u8868\u793a\uff1a<\/p>\n<pre>source = \"\u9648\u601d\u7ef4\"\nsource = \"foo\"\nfor i in source:\nnum = ord(i)\nprint bin(num).replace('b','')<\/pre>\n<p><strong>\u7279\u522b\u7684\uff0c\u5982\u679csource\u662f\u6c49\u5b57 &#8220;\u9648\u601d\u7ef4&#8221;\u600e\u4e48\u529e\uff1f<\/strong><\/p>\n<p>\u7b54\uff1a\u5bf9\u4e8eutf-8\uff0c\u6bcf\u4e00\u4e2a\u6c49\u5b57\u5360 3 \u4e2a\u5b57\u8282\uff0c\u90a3\u4e48 &#8220;\u9648\u601d\u7ef4&#8221; \u5219\u6709 9\u4e2a\u5b57\u8282 \u5bf9\u4e8e\u6c49\u5b57\uff0cfor\u5faa\u73af\u65f6\u5019\u4f1a\u6309\u7167 \u5b57\u8282 \u8fed\u4ee3\uff0c\u90a3\u4e48\u5728\u8fed\u4ee3\u65f6\uff0c\u5c06\u6bcf\u4e00\u4e2a\u5b57\u8282\u8f6c\u6362 \u5341\u8fdb\u5236\u6570\uff0c\u7136\u540e\u518d\u5c06\u5341\u8fdb\u5236\u6570\u8f6c\u6362\u6210\u4e8c\u8fdb\u5236 11100110 10101101 10100110 11100110 10110010 10011011 11101001 10111101 10010000<\/p>\n<h3>14.getbit(name, offset)<\/h3>\n<p>\u83b7\u53d6name\u5bf9\u5e94\u7684\u503c\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u4e2d\u7684\u67d0\u4f4d\u7684\u503c \uff080\u62161\uff09<\/p>\n<pre>print(r.getbit(\"foo1\", 0)) # 0 foo1 \u5bf9\u5e94\u7684\u4e8c\u8fdb\u5236 4\u4e2a\u5b57\u8282 32\u4f4d \u7b2c0\u4f4d\u662f0\u8fd8\u662f1<\/pre>\n<h3>15.bitcount(key, start=None, end=None)<\/h3>\n<p>\u83b7\u53d6name\u5bf9\u5e94\u7684\u503c\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u4e2d 1 \u7684\u4e2a\u6570<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>key &#8211; Redis\u7684name<\/li>\n<li>start &#8211; \u5b57\u8282\u8d77\u59cb\u4f4d\u7f6e<\/li>\n<li>end &#8211; \u5b57\u8282\u7ed3\u675f\u4f4d\u7f6e<\/li>\n<\/ul>\n<pre>print(r.get(\"foo\")) &nbsp;# goo1 01100111\nprint(r.bitcount(\"foo\",0,1)) &nbsp;# 11 \u8868\u793a\u524d2\u4e2a\u5b57\u8282\u4e2d\uff0c1\u51fa\u73b0\u7684\u4e2a\u6570<\/pre>\n<h3>16.bitop(operation, dest, *keys)<\/h3>\n<p>\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<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>operation &#8211; AND\uff08\u5e76\uff09 \u3001 OR\uff08\u6216\uff09 \u3001 NOT\uff08\u975e\uff09 \u3001 XOR\uff08\u5f02\u6216\uff09<\/li>\n<li>dest &#8211; \u65b0\u7684Redis\u7684name<\/li>\n<li>*keys &#8211; \u8981\u67e5\u627e\u7684Redis\u7684name<\/li>\n<\/ul>\n<p>\u5982\uff1a<\/p>\n<pre>bitop(\"AND\", 'new_name', 'n1', 'n2', 'n3')<\/pre>\n<p>\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<\/p>\n<pre>r.set(\"foo\",\"1\") &nbsp;# 0110001\nr.set(\"foo1\",\"2\") &nbsp;# 0110010\nprint(r.mget(\"foo\",\"foo1\")) &nbsp;# ['goo1', 'baaanew']\nprint(r.bitop(\"AND\",\"new\",\"foo\",\"foo1\")) &nbsp;# \"new\" 0 0110000\nprint(r.mget(\"foo\",\"foo1\",\"new\"))\n\nsource = \"12\"\nfor i in source:\nnum = ord(i)\nprint(num) &nbsp;# \u6253\u5370\u6bcf\u4e2a\u5b57\u6bcd\u5b57\u7b26\u6216\u8005\u6c49\u5b57\u5b57\u7b26\u5bf9\u5e94\u7684ascii\u7801\u503c f-102-0b100111-01100111\nprint(bin(num)) &nbsp;# \u6253\u5370\u6bcf\u4e2a10\u8fdb\u5236ascii\u7801\u503c\u8f6c\u6362\u6210\u4e8c\u8fdb\u5236\u7684\u503c 0b1100110\uff080b\u8868\u793a\u4e8c\u8fdb\u5236\uff09\nprint bin(num).replace('b','') &nbsp;# \u5c06\u4e8c\u8fdb\u52360b1100110\u66ff\u6362\u621001100110<\/pre>\n<h3>17.strlen(name)<\/h3>\n<p>\u8fd4\u56dename\u5bf9\u5e94\u503c\u7684\u5b57\u8282\u957f\u5ea6\uff08\u4e00\u4e2a\u6c49\u5b573\u4e2a\u5b57\u8282\uff09<\/p>\n<pre>print(r.strlen(\"foo\"))  # 4 'goo1'\u7684\u957f\u5ea6\u662f4<\/pre>\n<h3>18.incr(self, name, amount=1)<\/h3>\n<p>\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<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>name &#8211; Redis\u7684name<\/li>\n<li>amount &#8211; \u81ea\u589e\u6570\uff08\u5fc5\u987b\u662f\u6574\u6570\uff09<\/li>\n<\/ul>\n<p>\u6ce8\uff1a\u540c incrby<\/p>\n<pre>r.set(\"foo\", 123)\nprint(r.mget(\"foo\", \"foo1\", \"foo2\", \"k1\", \"k2\"))\nr.incr(\"foo\", amount=1)\nprint(r.mget(\"foo\", \"foo1\", \"foo2\", \"k1\", \"k2\"))<\/pre>\n<p><strong>\u5e94\u7528\u573a\u666f \u2013 \u9875\u9762\u70b9\u51fb\u6570<\/strong><\/p>\n<p>\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<\/p>\n<p>\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<\/p>\n<pre>r.set(\"visit:12306:totals\", 34634)\nprint(r.get(\"visit:12306:totals\"))<\/pre>\n<p>\u6bcf\u5f53\u6709\u4e00\u4e2a\u9875\u9762\u70b9\u51fb\uff0c\u5219\u4f7f\u7528INCR\u589e\u52a0\u70b9\u51fb\u6570\u5373\u53ef\u3002<\/p>\n<pre>r.incr(\"visit:12306:totals\")\nr.incr(\"visit:12306:totals\")<\/pre>\n<p>\u9875\u9762\u8f7d\u5165\u7684\u65f6\u5019\u5219\u53ef\u76f4\u63a5\u83b7\u53d6\u8fd9\u4e2a\u503c<\/p>\n<pre>print(r.get(\"visit:12306:totals\"))<\/pre>\n<h3>19.incrbyfloat(self, name, amount=1.0)<\/h3>\n<p>\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<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>name &#8211; Redis\u7684name<\/li>\n<li>amount &#8211; \u81ea\u589e\u6570\uff08\u6d6e\u70b9\u578b\uff09<\/li>\n<\/ul>\n<pre>r.set(\"foo1\", \"123.0\")\nr.set(\"foo2\", \"221.0\")\nprint(r.mget(\"foo1\", \"foo2\"))\nr.incrbyfloat(\"foo1\", amount=2.0)\nr.incrbyfloat(\"foo2\", amount=3.0)\nprint(r.mget(\"foo1\", \"foo2\"))<\/pre>\n<h3>20.decr(self, name, amount=1)<\/h3>\n<p>\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<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>name &#8211; Redis\u7684name<\/li>\n<li>amount &#8211; \u81ea\u51cf\u6570\uff08\u6574\u6570)<\/li>\n<\/ul>\n<pre>r.decr(\"foo4\", amount=3) # \u9012\u51cf3\nr.decr(\"foo1\", amount=1) # \u9012\u51cf1\nprint(r.mget(\"foo1\", \"foo4\"))<\/pre>\n<h3>21.append(key, value)<\/h3>\n<p>\u5728redis name\u5bf9\u5e94\u7684\u503c\u540e\u9762\u8ffd\u52a0\u5185\u5bb9<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>key &#8211; redis\u7684name<\/li>\n<li>value &#8211; \u8981\u8ffd\u52a0\u7684\u5b57\u7b26\u4e32<\/li>\n<\/ul>\n<pre>r.append(\"name\", \"haha\") &nbsp; &nbsp;# \u5728name\u5bf9\u5e94\u7684\u503cjunxi\u540e\u9762\u8ffd\u52a0\u5b57\u7b26\u4e32haha\nprint(r.mget(\"name\"))<\/pre>\n<h2>redis\u7684hash\u7c7b\u578b\u57fa\u672c\u547d\u4ee4\u793a\u4f8b<\/h2>\n<h3>1\u3001\u5355\u4e2a\u589e\u52a0&#8211;\u4fee\u6539(\u5355\u4e2a\u53d6\u51fa)&#8211;\u6ca1\u6709\u5c31\u65b0\u589e\uff0c\u6709\u7684\u8bdd\u5c31\u4fee\u6539<\/h3>\n<pre>hset(name, key, value)<\/pre>\n<p>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<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>name &#8211; redis\u7684name<\/li>\n<li>key &#8211; name\u5bf9\u5e94\u7684hash\u4e2d\u7684key<\/li>\n<li>value &#8211; name\u5bf9\u5e94\u7684hash\u4e2d\u7684value<\/li>\n<\/ul>\n<p>\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<\/p>\n<pre>import redis\nimport time\n\npool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)\nr = redis.Redis(connection_pool=pool)\n\nr.hset(\"hash1\", \"k1\", \"v1\")\nr.hset(\"hash1\", \"k2\", \"v2\")\nprint(r.hkeys(\"hash1\")) # \u53d6hash\u4e2d\u6240\u6709\u7684key\nprint(r.hget(\"hash1\", \"k1\")) &nbsp; &nbsp;# \u5355\u4e2a\u53d6hash\u7684key\u5bf9\u5e94\u7684\u503c\nprint(r.hmget(\"hash1\", \"k1\", \"k2\")) # \u591a\u4e2a\u53d6hash\u7684key\u5bf9\u5e94\u7684\u503c\nr.hsetnx(\"hash1\", \"k2\", \"v3\") # \u53ea\u80fd\u65b0\u5efa\nprint(r.hget(\"hash1\", \"k2\"))<\/pre>\n<h3>2\u3001\u6279\u91cf\u589e\u52a0\uff08\u53d6\u51fa\uff09<\/h3>\n<pre>hmset(name, mapping)<\/pre>\n<p>\u5728name\u5bf9\u5e94\u7684hash\u4e2d\u6279\u91cf\u8bbe\u7f6e\u952e\u503c\u5bf9<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>name &#8211; redis\u7684name<\/li>\n<li>mapping &#8211; \u5b57\u5178\uff0c\u5982\uff1a{&#8216;k1&#8242;:&#8217;v1&#8217;, &#8216;k2&#8217;: &#8216;v2&#8217;}<\/li>\n<\/ul>\n<p>\u5982\uff1a<\/p>\n<pre>r.hmset(\"hash2\", {\"k2\": \"v2\", \"k3\": \"v3\"})\nhget(name,key)\n<\/pre>\n<p>\u5728name\u5bf9\u5e94\u7684hash\u4e2d\u83b7\u53d6\u6839\u636ekey\u83b7\u53d6value<\/p>\n<pre>hmget(name, keys, *args)<\/pre>\n<p>\u5728name\u5bf9\u5e94\u7684hash\u4e2d\u83b7\u53d6\u591a\u4e2akey\u7684\u503c<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>name &#8211; reids\u5bf9\u5e94\u7684name<\/li>\n<li>keys &#8211; \u8981\u83b7\u53d6key\u96c6\u5408\uff0c\u5982\uff1a[&#8216;k1&#8217;, &#8216;k2&#8217;, &#8216;k3&#8217;]<\/li>\n<li>*args &#8211; \u8981\u83b7\u53d6\u7684key\uff0c\u5982\uff1ak1,k2,k3<\/li>\n<\/ul>\n<p>\u5982\uff1a<\/p>\n<pre>print(r.hget(\"hash2\", \"k2\")) &nbsp;# \u5355\u4e2a\u53d6\u51fa\"hash2\"\u7684key-k2\u5bf9\u5e94\u7684value\nprint(r.hmget(\"hash2\", \"k2\", \"k3\")) &nbsp;# \u6279\u91cf\u53d6\u51fa\"hash2\"\u7684key-k2 k3\u5bf9\u5e94\u7684value --\u65b9\u5f0f1\nprint(r.hmget(\"hash2\", [\"k2\", \"k3\"])) &nbsp;# \u6279\u91cf\u53d6\u51fa\"hash2\"\u7684key-k2 k3\u5bf9\u5e94\u7684value --\u65b9\u5f0f2\n<\/pre>\n<h3>3\u3001\u53d6\u51fa\u6240\u6709\u7684\u952e\u503c\u5bf9<\/h3>\n<pre>hgetall(name)<\/pre>\n<p>\u83b7\u53d6name\u5bf9\u5e94hash\u7684\u6240\u6709\u952e\u503c<\/p>\n<pre>print(r.hgetall(\"hash1\"))<\/pre>\n<h3>4\u3001\u5f97\u5230\u6240\u6709\u952e\u503c\u5bf9\u7684\u683c\u5f0f hash\u957f\u5ea6<\/h3>\n<pre>hlen(name)<\/pre>\n<p>\u83b7\u53d6name\u5bf9\u5e94\u7684hash\u4e2d\u952e\u503c\u5bf9\u7684\u4e2a\u6570<\/p>\n<pre>print(r.hlen(\"hash1\"))<\/pre>\n<h3>5\u3001\u5f97\u5230\u6240\u6709\u7684keys\uff08\u7c7b\u4f3c\u5b57\u5178\u7684\u53d6\u6240\u6709keys\uff09<\/h3>\n<pre>hkeys(name)<\/pre>\n<p>\u83b7\u53d6name\u5bf9\u5e94\u7684hash\u4e2d\u6240\u6709\u7684key\u7684\u503c<\/p>\n<pre>print(r.hkeys(\"hash1\"))<\/pre>\n<h3>6\u3001\u5f97\u5230\u6240\u6709\u7684value\uff08\u7c7b\u4f3c\u5b57\u5178\u7684\u53d6\u6240\u6709value\uff09<\/h3>\n<pre>hvals(name)<\/pre>\n<p>\u83b7\u53d6name\u5bf9\u5e94\u7684hash\u4e2d\u6240\u6709\u7684value\u7684\u503c<\/p>\n<pre>print(r.hvals(\"hash1\"))<\/pre>\n<h3>7\u3001\u5224\u65ad\u6210\u5458\u662f\u5426\u5b58\u5728\uff08\u7c7b\u4f3c\u5b57\u5178\u7684in\uff09<\/h3>\n<pre>hexists(name, key)<\/pre>\n<p>\u68c0\u67e5 name \u5bf9\u5e94\u7684 hash \u662f\u5426\u5b58\u5728\u5f53\u524d\u4f20\u5165\u7684 key<\/p>\n<pre>print(r.hexists(\"hash1\", \"k4\")) &nbsp;# False \u4e0d\u5b58\u5728\nprint(r.hexists(\"hash1\", \"k1\")) &nbsp;# True \u5b58\u5728\n<\/pre>\n<h3>8\u3001\u5220\u9664\u952e\u503c\u5bf9<\/h3>\n<pre>hdel(name,*keys)<\/pre>\n<p>\u5c06name\u5bf9\u5e94\u7684hash\u4e2d\u6307\u5b9akey\u7684\u952e\u503c\u5bf9\u5220\u9664<\/p>\n<pre>print(r.hgetall(\"hash1\"))\nr.hset(\"hash1\", \"k2\", \"v222\") &nbsp; # \u4fee\u6539\u5df2\u6709\u7684key k2\nr.hset(\"hash1\", \"k11\", \"v1\") &nbsp; # \u65b0\u589e\u952e\u503c\u5bf9 k11\nr.hdel(\"hash1\", \"k1\") &nbsp; &nbsp;# \u5220\u9664\u4e00\u4e2a\u952e\u503c\u5bf9\nprint(r.hgetall(\"hash1\"))\n<\/pre>\n<h3>9\u3001\u81ea\u589e\u81ea\u51cf\u6574\u6570(\u5c06key\u5bf9\u5e94\u7684value&#8211;\u6574\u6570 \u81ea\u589e1\u6216\u80052\uff0c\u6216\u8005\u522b\u7684\u6574\u6570 \u8d1f\u6570\u5c31\u662f\u81ea\u51cf)<\/h3>\n<pre>hincrby(name, key, amount=1)<\/pre>\n<p>\u81ea\u589ename\u5bf9\u5e94\u7684hash\u4e2d\u7684\u6307\u5b9akey\u7684\u503c\uff0c\u4e0d\u5b58\u5728\u5219\u521b\u5efakey=amount<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>name &#8211; redis\u4e2d\u7684name<\/li>\n<li>key &#8211; hash\u5bf9\u5e94\u7684key<\/li>\n<li>amount &#8211; \u81ea\u589e\u6570\uff08\u6574\u6570\uff09<\/li>\n<\/ul>\n<pre>r.hset(\"hash1\", \"k3\", 123)\nr.hincrby(\"hash1\", \"k3\", amount=-1)\nprint(r.hgetall(\"hash1\"))\nr.hincrby(\"hash1\", \"k4\", amount=1) &nbsp;# \u4e0d\u5b58\u5728\u7684\u8bdd\uff0cvalue\u9ed8\u8ba4\u5c31\u662f1\nprint(r.hgetall(\"hash1\"))\n<\/pre>\n<h3>10\u3001\u81ea\u589e\u81ea\u51cf\u6d6e\u70b9\u6570(\u5c06key\u5bf9\u5e94\u7684value&#8211;\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<pre>hincrbyfloat(name, key, amount=1.0)<\/pre>\n<p>\u81ea\u589ename\u5bf9\u5e94\u7684hash\u4e2d\u7684\u6307\u5b9akey\u7684\u503c\uff0c\u4e0d\u5b58\u5728\u5219\u521b\u5efakey=amount<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>name &#8211; redis\u4e2d\u7684name<\/li>\n<li>key &#8211; hash\u5bf9\u5e94\u7684key<\/li>\n<li>amount\uff0c\u81ea\u589e\u6570\uff08\u6d6e\u70b9\u6570\uff09<\/li>\n<\/ul>\n<p>\u81ea\u589e name \u5bf9\u5e94\u7684 hash \u4e2d\u7684\u6307\u5b9a key \u7684\u503c\uff0c\u4e0d\u5b58\u5728\u5219\u521b\u5efa key=amount\u3002<\/p>\n<pre>r.hset(\"hash1\", \"k5\", \"1.0\")\nr.hincrbyfloat(\"hash1\", \"k5\", amount=-1.0) &nbsp; &nbsp;# \u5df2\u7ecf\u5b58\u5728\uff0c\u9012\u51cf-1.0\nprint(r.hgetall(\"hash1\"))\nr.hincrbyfloat(\"hash1\", \"k6\", amount=-1.0) &nbsp; &nbsp;# \u4e0d\u5b58\u5728\uff0cvalue\u521d\u59cb\u503c\u662f-1.0 \u6bcf\u6b21\u9012\u51cf1.0\nprint(r.hgetall(\"hash1\"))\n<\/pre>\n<h3>11\u3001\u53d6\u503c\u67e5\u770b&#8211;\u5206\u7247\u8bfb\u53d6<\/h3>\n<pre>hscan(name, cursor=0, match=None, count=None)<\/pre>\n<p>\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<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>name &#8211; redis\u7684name<\/li>\n<li>cursor &#8211; \u6e38\u6807\uff08\u57fa\u4e8e\u6e38\u6807\u5206\u6279\u53d6\u83b7\u53d6\u6570\u636e\uff09<\/li>\n<li>match &#8211; \u5339\u914d\u6307\u5b9akey\uff0c\u9ed8\u8ba4None \u8868\u793a\u6240\u6709\u7684key<\/li>\n<li>count &#8211; \u6bcf\u6b21\u5206\u7247\u6700\u5c11\u83b7\u53d6\u4e2a\u6570\uff0c\u9ed8\u8ba4None\u8868\u793a\u91c7\u7528Redis\u7684\u9ed8\u8ba4\u5206\u7247\u4e2a\u6570<\/li>\n<\/ul>\n<p>\u5982\uff1a<\/p>\n<pre>\u7b2c\u4e00\u6b21\uff1acursor1, data1 = r.hscan('xx', cursor=0, match=None, count=None)\n\u7b2c\u4e8c\u6b21\uff1acursor2, data1 = r.hscan('xx', cursor=cursor1, match=None, count=None)\n...<\/pre>\n<p>\u76f4\u5230\u8fd4\u56de\u503ccursor\u7684\u503c\u4e3a0\u65f6\uff0c\u8868\u793a\u6570\u636e\u5df2\u7ecf\u901a\u8fc7\u5206\u7247\u83b7\u53d6\u5b8c\u6bd5<\/p>\n<pre>print(r.hscan(\"hash1\"))<\/pre>\n<h3>12\u3001hscan_iter(name, match=None, count=None)<\/h3>\n<p>\u5229\u7528yield\u5c01\u88c5hscan\u521b\u5efa\u751f\u6210\u5668\uff0c\u5b9e\u73b0\u5206\u6279\u53bbredis\u4e2d\u83b7\u53d6\u6570\u636e<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>match &#8211; \u5339\u914d\u6307\u5b9akey\uff0c\u9ed8\u8ba4None \u8868\u793a\u6240\u6709\u7684key<\/li>\n<li>count &#8211; \u6bcf\u6b21\u5206\u7247\u6700\u5c11\u83b7\u53d6\u4e2a\u6570\uff0c\u9ed8\u8ba4None\u8868\u793a\u91c7\u7528Redis\u7684\u9ed8\u8ba4\u5206\u7247\u4e2a\u6570<\/li>\n<\/ul>\n<p>\u5982\uff1a<\/p>\n<pre>for item in r.hscan_iter('hash1'):\n&nbsp; &nbsp; print(item)\nprint(r.hscan_iter(\"hash1\")) &nbsp; &nbsp;# \u751f\u6210\u5668\u5185\u5b58\u5730\u5740<\/pre>\n<h2>redis\u7684list\u7c7b\u578b\u57fa\u672c\u547d\u4ee4\u793a\u4f8b<\/h2>\n<h3>1.\u589e\u52a0\uff08\u7c7b\u4f3c\u4e8elist\u7684append\uff0c\u53ea\u662f\u8fd9\u91cc\u662f\u4ece\u5de6\u8fb9\u65b0\u589e\u52a0\uff09&#8211;\u6ca1\u6709\u5c31\u65b0\u5efa<\/h3>\n<pre>lpush(name,values)<\/pre>\n<p>\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<\/p>\n<p>\u5982\uff1a<\/p>\n<pre>import redis\nimport time\n\npool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)\nr = redis.Redis(connection_pool=pool)\n\nr.lpush(\"list1\", 11, 22, 33)\nprint(r.lrange('list1', 0, -1))<\/pre>\n<p>\u4fdd\u5b58\u987a\u5e8f\u4e3a: 33,22,11<\/p>\n<p>\u6269\u5c55\uff1a<\/p>\n<pre>r.rpush(\"list2\", 11, 22, 33)  # \u8868\u793a\u4ece\u53f3\u5411\u5de6\u64cd\u4f5c\nprint(r.llen(\"list2\"))  # \u5217\u8868\u957f\u5ea6\nprint(r.lrange(\"list2\", 0, 3))  # \u5207\u7247\u53d6\u51fa\u503c\uff0c\u8303\u56f4\u662f\u7d22\u5f15\u53f70-3<\/pre>\n<h3><strong>2.\u589e\u52a0\uff08\u4ece\u53f3\u8fb9\u589e\u52a0\uff09&#8211;\u6ca1\u6709\u5c31\u65b0\u5efa<\/strong><\/h3>\n<pre>r.rpush(\"list2\", 44, 55, 66) &nbsp; &nbsp;# \u5728\u5217\u8868\u7684\u53f3\u8fb9\uff0c\u4f9d\u6b21\u6dfb\u52a044,55,66\nprint(r.llen(\"list2\")) &nbsp;# \u5217\u8868\u957f\u5ea6\nprint(r.lrange(\"list2\", 0, -1)) # \u5207\u7247\u53d6\u51fa\u503c\uff0c\u8303\u56f4\u662f\u7d22\u5f15\u53f70\u5230-1(\u6700\u540e\u4e00\u4e2a\u5143\u7d20)<\/pre>\n<h3><strong>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<\/strong><\/h3>\n<pre>lpushx(name,value)<\/pre>\n<p>\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<\/p>\n<pre>r.lpushx(\"list10\", 10) &nbsp; # \u8fd9\u91cclist10\u4e0d\u5b58\u5728\nprint(r.llen(\"list10\")) &nbsp;# 0\nprint(r.lrange(\"list10\", 0, -1)) &nbsp;# []\nr.lpushx(\"list2\", 77) &nbsp; # \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\nprint(r.llen(\"list2\")) &nbsp;# \u5217\u8868\u957f\u5ea6\nprint(r.lrange(\"list2\", 0, -1)) # \u5207\u7247\u53d6\u51fa\u503c\uff0c\u8303\u56f4\u662f\u7d22\u5f15\u53f70\u5230-1(\u6700\u540e\u4e00\u4e2a\u5143\u7d20<\/pre>\n<h3><strong>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<\/strong><\/h3>\n<pre>r.rpushx(\"list2\", 99) &nbsp; # \u8fd9\u91cc\"foo_list1\"\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\nprint(r.llen(\"list2\")) &nbsp;# \u5217\u8868\u957f\u5ea6\nprint(r.lrange(\"list2\", 0, -1)) # \u5207\u7247\u53d6\u51fa\u503c\uff0c\u8303\u56f4\u662f\u7d22\u5f15\u53f70\u5230-1(\u6700\u540e\u4e00\u4e2a\u5143\u7d20)<\/pre>\n<h3><strong>5.\u65b0\u589e\uff08\u56fa\u5b9a\u7d22\u5f15\u53f7\u4f4d\u7f6e\u63d2\u5165\u5143\u7d20\uff09<\/strong><\/h3>\n<pre>linsert(name, where, refvalue, value))<\/pre>\n<p>\u5728name\u5bf9\u5e94\u7684\u5217\u8868\u7684\u67d0\u4e00\u4e2a\u503c\u524d\u6216\u540e\u63d2\u5165\u4e00\u4e2a\u65b0\u503c<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>name &#8211; redis\u7684name<\/li>\n<li>where &#8211; BEFORE\u6216AFTER<\/li>\n<li>refvalue &#8211; \u6807\u6746\u503c\uff0c\u5373\uff1a\u5728\u5b83\u524d\u540e\u63d2\u5165\u6570\u636e<\/li>\n<li>value &#8211; \u8981\u63d2\u5165\u7684\u6570\u636e<\/li>\n<\/ul>\n<pre>r.linsert(\"list2\", \"before\", \"11\", \"00\") &nbsp; # \u5f80\u5217\u8868\u4e2d\u5de6\u8fb9\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u5143\u7d20\"11\"\u524d\u63d2\u5165\u5143\u7d20\"00\"\nprint(r.lrange(\"list2\", 0, -1)) &nbsp; # \u5207\u7247\u53d6\u51fa\u503c\uff0c\u8303\u56f4\u662f\u7d22\u5f15\u53f70-\u6700\u540e\u4e00\u4e2a\u5143\u7d20<\/pre>\n<h3><strong>6.\u4fee\u6539\uff08\u6307\u5b9a\u7d22\u5f15\u53f7\u8fdb\u884c\u4fee\u6539\uff09<\/strong><\/h3>\n<pre>r.lset(name, index, value)<\/pre>\n<p>\u5bf9name\u5bf9\u5e94\u7684list\u4e2d\u7684\u67d0\u4e00\u4e2a\u7d22\u5f15\u4f4d\u7f6e\u91cd\u65b0\u8d4b\u503c<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>name &#8211; redis\u7684name<\/li>\n<li>index &#8211; list\u7684\u7d22\u5f15\u4f4d\u7f6e<\/li>\n<li>value &#8211; \u8981\u8bbe\u7f6e\u7684\u503c<\/li>\n<\/ul>\n<pre>r.lset(\"list2\", 0, -11) &nbsp; &nbsp;# \u628a\u7d22\u5f15\u53f7\u662f0\u7684\u5143\u7d20\u4fee\u6539\u6210-11\nprint(r.lrange(\"list2\", 0, -1))<\/pre>\n<h3><strong>7.\u5220\u9664\uff08\u6307\u5b9a\u503c\u8fdb\u884c\u5220\u9664\uff09<\/strong><\/h3>\n<pre>r.lrem(name, value, num)<\/pre>\n<p>\u5728name\u5bf9\u5e94\u7684list\u4e2d\u5220\u9664\u6307\u5b9a\u7684\u503c<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>name &#8211; redis\u7684name<\/li>\n<li>value &#8211; \u8981\u5220\u9664\u7684\u503c<\/li>\n<li>num &#8211; num=0\uff0c\u5220\u9664\u5217\u8868\u4e2d\u6240\u6709\u7684\u6307\u5b9a\u503c\uff1b<\/li>\n<li>num=2 &#8211; \u4ece\u524d\u5230\u540e\uff0c\u5220\u96642\u4e2a, num=1,\u4ece\u524d\u5230\u540e\uff0c\u5220\u9664\u5de6\u8fb9\u7b2c1\u4e2a<\/li>\n<li>num=-2 &#8211; \u4ece\u540e\u5411\u524d\uff0c\u5220\u96642\u4e2a<\/li>\n<\/ul>\n<pre>r.lrem(\"list2\", \"11\", 1) &nbsp; &nbsp;# \u5c06\u5217\u8868\u4e2d\u5de6\u8fb9\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\"11\"\u5220\u9664\nprint(r.lrange(\"list2\", 0, -1))\nr.lrem(\"list2\", \"99\", -1) &nbsp; &nbsp;# \u5c06\u5217\u8868\u4e2d\u53f3\u8fb9\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\"99\"\u5220\u9664\nprint(r.lrange(\"list2\", 0, -1))\nr.lrem(\"list2\", \"22\", 0) &nbsp; &nbsp;# \u5c06\u5217\u8868\u4e2d\u6240\u6709\u7684\"22\"\u5220\u9664\nprint(r.lrange(\"list2\", 0, -1))<\/pre>\n<p><strong>8.\u5220\u9664\u5e76\u8fd4\u56de<\/strong><\/p>\n<pre>lpop(name)<\/pre>\n<p>\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<\/p>\n<p>rpop(name) \u8868\u793a\u4ece\u53f3\u5411\u5de6\u64cd\u4f5c<\/p>\n<pre>r.lpop(\"list2\") &nbsp; &nbsp;# \u5220\u9664\u5217\u8868\u6700\u5de6\u8fb9\u7684\u5143\u7d20\uff0c\u5e76\u4e14\u8fd4\u56de\u5220\u9664\u7684\u5143\u7d20\nprint(r.lrange(\"list2\", 0, -1))\nr.rpop(\"list2\") &nbsp; &nbsp;# \u5220\u9664\u5217\u8868\u6700\u53f3\u8fb9\u7684\u5143\u7d20\uff0c\u5e76\u4e14\u8fd4\u56de\u5220\u9664\u7684\u5143\u7d20\nprint(r.lrange(\"list2\", 0, -1))<\/pre>\n<h3><strong>9.\u5220\u9664\u7d22\u5f15\u4e4b\u5916\u7684\u503c<\/strong><\/h3>\n<pre>ltrim(name, start, end)<\/pre>\n<p>\u5728name\u5bf9\u5e94\u7684\u5217\u8868\u4e2d\u79fb\u9664\u6ca1\u6709\u5728start-end\u7d22\u5f15\u4e4b\u95f4\u7684\u503c<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>name &#8211; redis\u7684name<\/li>\n<li>start &#8211; \u7d22\u5f15\u7684\u8d77\u59cb\u4f4d\u7f6e<\/li>\n<li>end &#8211; \u7d22\u5f15\u7ed3\u675f\u4f4d\u7f6e<\/li>\n<\/ul>\n<pre>r.ltrim(\"list2\", 0, 2) &nbsp; &nbsp;# \u5220\u9664\u7d22\u5f15\u53f7\u662f0-2\u4e4b\u5916\u7684\u5143\u7d20\uff0c\u503c\u4fdd\u7559\u7d22\u5f15\u53f7\u662f0-2\u7684\u5143\u7d20\nprint(r.lrange(\"list2\", 0, -1))<\/pre>\n<h3><strong>10.\u53d6\u503c\uff08\u6839\u636e\u7d22\u5f15\u53f7\u53d6\u503c\uff09<\/strong><\/h3>\n<pre>lindex(name, index)<\/pre>\n<p>\u5728name\u5bf9\u5e94\u7684\u5217\u8868\u4e2d\u6839\u636e\u7d22\u5f15\u83b7\u53d6\u5217\u8868\u5143\u7d20<\/p>\n<pre>print(r.lindex(\"list2\", 0))  # \u53d6\u51fa\u7d22\u5f15\u53f7\u662f0\u7684\u503c<\/pre>\n<h3><strong>11.\u79fb\u52a8 \u5143\u7d20\u4ece\u4e00\u4e2a\u5217\u8868\u79fb\u52a8\u5230\u53e6\u5916\u4e00\u4e2a\u5217\u8868<\/strong><\/h3>\n<pre>rpoplpush(src, dst)<\/pre>\n<p>\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<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>src &#8211; \u8981\u53d6\u6570\u636e\u7684\u5217\u8868\u7684 name<\/li>\n<li>dst &#8211; \u8981\u6dfb\u52a0\u6570\u636e\u7684\u5217\u8868\u7684 name<\/li>\n<\/ul>\n<pre>r.rpoplpush(\"list1\", \"list2\")\nprint(r.lrange(\"list2\", 0, -1))<\/pre>\n<h3><strong>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<\/strong><\/h3>\n<pre>brpoplpush(src, dst, timeout=0)<\/pre>\n<p>\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<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>src &#8211; \u53d6\u51fa\u5e76\u8981\u79fb\u9664\u5143\u7d20\u7684\u5217\u8868\u5bf9\u5e94\u7684name<\/li>\n<li>dst &#8211; \u8981\u63d2\u5165\u5143\u7d20\u7684\u5217\u8868\u5bf9\u5e94\u7684name<\/li>\n<li>timeout &#8211; \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<\/li>\n<\/ul>\n<pre>r.brpoplpush(\"list1\", \"list2\", timeout=2)\nprint(r.lrange(\"list2\", 0, -1))<\/pre>\n<h3><strong>13.\u4e00\u6b21\u79fb\u9664\u591a\u4e2a\u5217\u8868<\/strong><\/h3>\n<pre>blpop(keys, timeout)<\/pre>\n<p>\u5c06\u591a\u4e2a\u5217\u8868\u6392\u5217\uff0c\u6309\u7167\u4ece\u5de6\u5230\u53f3\u53bbpop\u5bf9\u5e94\u5217\u8868\u7684\u5143\u7d20<\/p>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>keys &#8211; redis\u7684name\u7684\u96c6\u5408<\/li>\n<li>timeout &#8211; \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<\/li>\n<\/ul>\n<p>\u66f4\u591a\uff1a<\/p>\n<p>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<\/p>\n<pre>r.lpush(\"list10\", 3, 4, 5)\nr.lpush(\"list11\", 3, 4, 5)\nwhile True:\n&nbsp; &nbsp; r.blpop([\"list10\", \"list11\"], timeout=2)\n&nbsp; &nbsp; print(r.lrange(\"list10\", 0, -1), r.lrange(\"list11\", 0, -1))<\/pre>\n<h3><strong>14.\u81ea\u5b9a\u4e49\u589e\u91cf\u8fed\u4ee3<\/strong><\/h3>\n<p>\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<\/p>\n<p>\u5faa\u73af\u5217\u8868<\/p>\n<p>\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<\/p>\n<pre>def list_iter(name):\n&nbsp; &nbsp; \"\"\"\n&nbsp; &nbsp; \u81ea\u5b9a\u4e49redis\u5217\u8868\u589e\u91cf\u8fed\u4ee3\n&nbsp; &nbsp; :param name: redis\u4e2d\u7684name\uff0c\u5373\uff1a\u8fed\u4ee3name\u5bf9\u5e94\u7684\u5217\u8868\n&nbsp; &nbsp; :return: yield \u8fd4\u56de \u5217\u8868\u5143\u7d20\n&nbsp; &nbsp; \"\"\"\n&nbsp; &nbsp; list_count = r.llen(name)\n&nbsp; &nbsp; for index in range(list_count):\n&nbsp; &nbsp; &nbsp; &nbsp; yield r.lindex(name, index)\n\n# \u4f7f\u7528\nfor item in list_iter('list2'): # \u904d\u5386\u8fd9\u4e2a\u5217\u8868\n&nbsp; &nbsp; print(item)<\/pre>\n<h2>redis\u7684set\u7c7b\u578b\u57fa\u672c\u547d\u4ee4\u793a\u4f8b<\/h2>\n<p>Set\u96c6\u5408\u5c31\u662f\u4e0d\u5141\u8bb8\u91cd\u590d\u7684\u5217\u8868<\/p>\n<h3>1.sadd\u3001smembers\u548cscard<\/h3>\n<p>sadd(key,values)\uff0c\u7ed9key\u5bf9\u5e94\u7684\u96c6\u5408\u4e2d\u6dfb\u52a0\u5143\u7d20<\/p>\n<p>smembers(key)\uff0c\u83b7\u53d6key\u5bf9\u5e94\u7684\u96c6\u5408\u7684\u6240\u6709\u6210\u5458<\/p>\n<p>scard(key)\uff0c\u83b7\u53d6key\u5bf9\u5e94\u7684\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u4e2a\u6570<\/p>\n<pre>r.sadd(\"key12\",\"a1\")\nr.sadd(\"key12\",\"a1\",\"a1\")\n\nprint(r.smembers(\"key12\")) #\u8f93\u51fa\uff1a{'a1'}print(r.scard(\"key12\")) #\u8f93\u51fa\uff1a1<\/pre>\n<h3>2.sdiff \u548c sdiffstore<\/h3>\n<p>sdiff(keys, *args)\uff0c\u5728\u7b2c\u4e00\u4e2akey\u5bf9\u5e94\u7684\u96c6\u5408\u4e2d\u4e14\u4e0d\u5728\u5176\u4ed6key\u5bf9\u5e94\u7684\u96c6\u5408\u7684\u5143\u7d20\u96c6\u5408<\/p>\n<pre>r.sadd(\"key13\",\"a1\",\"a2\",\"a4\")\nr.sadd(\"key14\",\"a2\",\"a3\")\nr.sadd(\"key15\",\"a1\",\"a2\",\"a3\")\nprint(r.sdiff(\"key13\",\"key14\",\"key15\"))#\u8f93\u51fa:{'a4'}<\/pre>\n<p>sdiffstore(newkey, keys, *args)\uff0c\u76f8\u5f53\u4e8e\u628asdiff\u83b7\u53d6\u7684\u503c\u52a0\u5165\u5230newkey\u5bf9\u5e94\u7684\u96c6\u5408\u4e2d<\/p>\n<pre>r.sdiffstore(\"key16\",\"key13\",\"key14\",\"key15\")\nprint(r.smembers(\"key16\")) #\u8f93\u51fa\uff1a{'a4'}<\/pre>\n<h3>3.sinter\u548c sinterstore<\/h3>\n<p>sinter(keys, *args)\uff0c\u83b7\u53d6\u591a\u4e2akey\u5bf9\u5e94\u96c6\u5408\u7684\u5e76\u96c6<\/p>\n<pre>r.sadd(\"key13\",\"a1\",\"a2\",\"a4\")\nr.sadd(\"key14\",\"a2\",\"a3\")\nr.sadd(\"key15\",\"a1\",\"a2\",\"a3\")\nprint(r.sinter(\"key13\",\"key14\",\"key15\"))#\u8f93\u51fa:{'a2'}<\/pre>\n<p>sinterstore(newkey, keys, *args)\uff0c\u83b7\u53d6\u591a\u4e2akey\u5bf9\u5e94\u96c6\u5408\u7684\u5e76\u96c6\uff0c\u518d\u8bb2\u5176\u52a0\u5165\u5230newkey\u5bf9\u5e94\u7684\u96c6\u5408\u4e2d<\/p>\n<pre>r.sinterstore(\"key17\",\"key13\",\"key14\",\"key15\")\nprint(r.smembers(\"key17\")) #\u8f93\u51fa\uff1a{'a2'}<\/pre>\n<h3>4.sismember\u3001smove\u548c spop<\/h3>\n<p>sismember(name, value)\uff0c\u68c0\u67e5value\u662f\u5426\u662fname\u5bf9\u5e94\u7684\u96c6\u5408\u5185\u7684\u5143\u7d20<\/p>\n<pre>r.sadd(\"key13\", \"a1\", \"a2\", \"a4\")\nprint(r.sismember(\"key13\",\"a1\")) # \u8f93\u51fa\uff1aTrueprint(r.sismember(\"key13\",\"a5\")) # \u8f93\u51fa\uff1aFalse<\/pre>\n<p>smove(src, dst, value)\uff0c\u5c06\u67d0\u4e2a\u5143\u7d20\u4ece\u4e00\u4e2a\u96c6\u5408\u4e2d\u79fb\u52a8\u5230\u53e6\u5916\u4e00\u4e2a\u96c6\u5408<\/p>\n<pre>r.sadd(\"key18\", \"a1\", \"a2\", \"a4\")\nr.sadd(\"key19\", \"a5\")\nr.smove(\"key18\", \"key19\", \"a2\")\nprint(r.smembers(\"key19\")) #\u8f93\u51fa\uff1a{'a2', 'a5'}<\/pre>\n<p>spop(name)\uff0c\u4ece\u96c6\u5408\u7684\u53f3\u4fa7\u79fb\u9664\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u5c06\u5176\u8fd4\u56de<\/p>\n<pre>print(r.spop(\"key19\")) #\u8f93\u51fa\uff1aa5print(r.smembers(\"key19\")) #\u8f93\u51fa\uff1a{'a2'}<\/pre>\n<h2>redis\u7684\u6709\u5e8fset\u7c7b\u578b\u57fa\u672c\u547d\u4ee4\u793a\u4f8b<\/h2>\n<p>\u6b64\u5916\u8fd8\u6709\u6709\u5e8f\u96c6\u5408\u64cd\u4f5c\uff1a\u5728\u96c6\u5408\u7684\u57fa\u7840\u4e0a\uff0c\u4e3a\u6bcf\u4e00\u4e2a\u5143\u7d20\u6392\u5e8f\u3002<\/p>\n<p>zadd(name, mapping)\uff0c\u5176\u4e2dname\u4e3a\u6709\u5e8f\u96c6\u5408\u540d\uff0cmapping\u4e3adict\u7c7b\u578b\u7684\u952e-\u503c\u5bf9<\/p>\n<p>zcard(name)\uff0c\u83b7\u53d6\u6709\u5e8f\u96c6\u5408\u5185\u5143\u7d20\u7684\u6570\u91cf<\/p>\n<p>zcount(name, min, max)\uff0c#\u83b7\u53d6\u6709\u5e8f\u96c6\u5408\u4e2d\u5206\u6570\u5728[min,max]\u4e4b\u95f4\u7684\u4e2a\u6570<\/p>\n<pre>r.zadd(\"key20\", {\"a\": 10, \"b\": 3, \"c\": 8})\nprint(r.zcard(\"key20\")) #\u8f93\u51fa\uff1a3print(r.zcount(\"key20\", 2, 5)) #\u8f93\u51fa\uff1a1print(r.zcount(\"key20\", 7, 15)) #\u8f93\u51fa\uff1a1<\/pre>\n<h2>redis\u5176\u4ed6\u5e38\u7528\u64cd\u4f5c\u547d\u4ee4<\/h2>\n<p>\u4ee5\u4e0a\u662f\u6309\u4e0d\u540c\u7c7b\u578b\u7684value\u8fdb\u884c\u5bf9\u5e94\u7684\u64cd\u4f5c\uff0credis\u6709\u4e00\u4e9b\u65e0\u89c6\u7c7b\u578b\u7684\u901a\u7528\u64cd\u4f5c\u65b9\u6cd5\u3002\uff08\u8fd9\u91cc\u5c31\u53ea\u662f\u5217\u51fa\u65b9\u6cd5\u5e76\u8bf4\u660e\uff0c\u5c31\u4e0d\u518d\u4e00\u4e00\u6f14\u793a\uff09<\/p>\n<p>delete(*names)\uff0c\u6839\u636ename\u5220\u9664redis\u4e2d\u7684\u4efb\u610f\u6570\u636e\u7c7b\u578b<\/p>\n<p>exists(name)\uff0c\u68c0\u6d4bredis\u7684name\u662f\u5426\u5b58\u5728<\/p>\n<p>keys(pattern=&#8217;*&#8217;)\uff0c\u6839\u636e* \uff1f\u7b49\u901a\u914d\u7b26\u5339\u914d\u83b7\u53d6redis\u7684name<\/p>\n<p>expire(name ,time)\uff0c\u4e3a\u67d0\u4e2aname\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4<\/p>\n<p>rename(src, dst)\uff0c\u91cd\u547d\u540d<\/p>\n<p>move(name, db))\uff0c\u5c06redis\u7684\u67d0\u4e2a\u503c\u79fb\u52a8\u5230\u6307\u5b9a\u7684db\u4e0b<\/p>\n<p>randomkey()\uff0c\u968f\u673a\u83b7\u53d6\u4e00\u4e2aredis\u7684name\uff08\u4e0d\u5220\u9664\uff09<\/p>\n<p>type(name)\uff0c\u83b7\u53d6name\u5bf9\u5e94\u503c\u7684\u7c7b\u578b<\/p>\n<h2>\u5c0f\u7ed3<\/h2>\n<p>\u4eca\u5929\u7684\u6280\u672f\u8bb2\u89e3\u6587\u7ae0\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u4e3b\u8981\u662f\u5c06\u4e86\u5982\u4f55\u901a\u8fc7python\u53bb\u8fde\u63a5Redis\uff0cRedis\u7684\u5b58\u50a8\u7ed3\u6784\u662fkey-value\u7c7b\u578b\uff0c\u672c\u6587\u6309value\u7c7b\u578b\u5206\u7c7b\u8bb2\u89e3\u4e86string\u5b57\u7b26\u4e32\u64cd\u4f5c\u3001list\u64cd\u4f5c\u3001hash\u64cd\u4f5c\u3001set\u64cd\u4f5c\u4ee5\u53ca\u901a\u7528\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>python\u4e2d\u4f7f\u7528redis\u7528\u6cd5&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201598","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201598"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201598\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}