{"id":5897,"date":"2024-08-27T19:01:01","date_gmt":"2024-08-27T11:01:01","guid":{"rendered":""},"modified":"2024-08-27T19:01:01","modified_gmt":"2024-08-27T11:01:01","slug":"redis\u547d\u4ee4\u8be6\u89e3_redis\u5e38\u7528\u547d\u4ee4\u5927\u5168","status":"publish","type":"post","link":"https:\/\/mushiming.com\/5897.html","title":{"rendered":"redis\u547d\u4ee4\u8be6\u89e3_redis\u5e38\u7528\u547d\u4ee4\u5927\u5168"},"content":{"rendered":"
\u8fd9\u8282\u6211\u4eec\u5b66\u4e60\u4f4d\u8fd0\u7b97\uff0c\u7b97\u662fredis\u6211\u8bb0\u4e0d\u4f4f\u7684\u90a3\u4e9b\u547d\u4ee4<\/span>\u7684\u5b8c\u7ed3<\/span>\uff0c\u540e\u7eed\u4f1a\u66f4\u65b0\u5176\u4ed6\u5185\u5bb9\u3002<\/strong><\/p>\n \u5b57\u7b26\u4e32\uff1a \u4ece frank\u5230grank\u7684\u8f6c\u6362\uff0c f \u7684ascii\u7801\u4e3a\u201c0110 0110\u201d\uff0cg\u7684ascii\u7801\u4e3a\u201c0110 0111\u201d\uff0c\u4ece\u5de6\u5230\u53f3\uff0c\u4ece0\u5f00\u59cb\uff0cf\u548cg\u7684\u5dee\u5f02\u5728\u4e8e\u6700\u540e\u76841\uff0c\u6240\u4ee5\u6211\u4eec\u4f7f\u7528setbit name 7 1 \uff0c\u90a3\u6211\u4eec\u60f3grank\u8f6c\u6362\u4e3agrcnk\u5462\uff1f\uff1f\uff1f \u4e5f\u5c31\u662fa\u53d8\u6210c<\/p>\n \u8fd922\u662f\u600e\u4e48\u7b97\u51fa\u6765\u7684\u5462\uff1f 22 = 2*8+6 a\u7684ascii\u7801\u4e3a\"0110 0001\" , c\u7684ascii\u7801\u4e3a\u201c0110 0011\u201d<\/p>\n gr\u4e3a2*8\u662f16\u4f4d\uff0c\u800ca\u548cc\u7684\u5dee\u5f02\u5728\u7b2c6\u4f4d\uff0c\u6240\u4ee5\u5f97\u51fa22<\/p>\n \u4e8c\u7ef4\u7684\u4f8b\u5b50\uff1a<\/p>\n \u6211\u4eec\u6709\u4e00\u4e2a10*10\u7684\u77e9\u9635\uff0c\u6c34\u5e73\u4e3ax\u5750\u6807\uff0c\u5782\u76f4\u4e3ay\u5750\u6807\uff0c\u5373(x,y)\u5219\u4e0a\u8ff0\u56fe\u4e2d1\u7684\u5750\u6807\u4e3a (4,0)\u3001 (4,1)\u3001(4,2) \u3001<\/span><\/strong>(5,2)\u3001 (5,3)\u3001(5,4)\u3001(5,5)\u3001(6,5) \uff0c\u800c\u5728\u4e00\u4e2abinary string\u4e2d\u7684\u504f\u79fb\u91cfoffset\u662f\u9700\u8981\u8fdb\u884c\u8f6c\u6362\uff1a<\/span><\/strong><\/p>\n offset = y * max_width + x \uff0c\u8fd9\u91cc\u7684max_width\u4e3a10<\/span><\/strong><\/p>\n \u6211\u4eec\u4f7f\u7528\u8fd9\u4e2a\u4e8c\u4f4d\u7684\u77e9\u9635\u505a\u4ec0\u4e48\u5462\uff1f \u6bd4\u5982\u6709\u4e00\u4e2a\u6e38\u620f\uff0c\u5728\u4e8c\u7ef4\u5e73\u9762\u4e0a\u901a\u8fc7\u4e0a\u4e0b\u5de6\u53f3\u952e\u8fdb\u884c\u884c\u8d70\uff0c\u6211\u4eec\u9700\u8981\u8bb0\u5f55\u73a9\u5bb6\u8d70\u7684\u8def\u5f84\u548c\u8d70\u7684\u6b65\u6570\uff0c\u90a3\u6211\u4eec\u5982\u4f55\u505a\u5462\uff1f \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528redis\u7684bitwise\u6765\u8fdb\u884c\u64cd\u4f5c\uff0c\u5047\u8bbe\u73a9\u5bb6id\u4e3a2 \uff0c\u5728\u7b2c3\u5173\u5361\u7684\u7b2c4\u4e2a\u5730\u56fe\uff0c\u90a3\u4e48 \u6211\u4eec\u8bb0\u5f55\u8fd9\u4e2akey\u4e3a<\/p>\n player:2:episode:3:map:4<\/span><\/strong><\/p>\n \u8fd9\u4e2akey\u6807\u8bc6\u4e86\u67d0\u4e2a\u73a9\u5bb6\u5728\u67d0\u5173\u5361\u7684\u67d0\u4e2a\u5730\u56fe\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/strong><\/span><\/p>\n \u6bcf\u5f53\u73a9\u5bb6\u8d70\u4e86\u4e00\u6b65\uff0c\u6211\u4eec\u7528setbit key offset 1\uff0c\u800cbitcount\u6765\u8ba1\u7b97\u8fd9\u4e2a\u73a9\u5bb6\u8d70\u4e86\u591a\u5c11\u6b65\u3002<\/strong><\/span><\/p>\n \u6bcf\u6b21\u4eceredis\u4e2d\u53d6\u51fa\u73a9\u5bb6\u7684\u4fe1\u606f\uff0c\u5c31\u63a8\u7b97\u51fa\u4ed6\u7684\u8def\u5f84\u548c\u6b65\u6570\u3002<\/strong><\/span><\/p>\n 0. setbit <\/span><\/strong>: \u5b57\u7b26\u4e32\u662f\u4ece\u5de6\u5230\u53f3\uff0c\u4ece0\u5f00\u59cb\uff0c\u4e8c\u7ef4\u5750\u6807\u5219\u662f\u4ece\u5de6\u5230\u53f3\uff0c\u4ece\u4e0b\u5411\u4e0a\uff0c\u4ece0\u5f00\u59cb<\/p>\n 1. getbit<\/strong><\/span> \uff1a \u83b7\u53d6\u67d0\u4e00\u4e2a\u504f\u79fb\u91cf\u7684\u503c<\/p>\n 2. bitcount <\/span><\/strong>: \u83b7\u53d6\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u76841\u7684\u4e2a\u6570<\/p>\n 3. bitop <\/strong><\/span>\uff1a \u5bf9\u4e00\u4e2a\u6216\u591a\u4e2a\u4e8c\u8fdb\u5236\u4f4d\u4e32\u6267\u884c\u5305\u62ec \u5e76and\uff0c\u6216or\uff0c\u5f02\u6216xor\uff0c\u975enot\uff0c\u5e76\u5c06\u7ed3\u679c\u8ba1\u7b97\u5f97\u51fa\u4fdd\u5b58\u5728dest-key\u952e\u4e2d<\/p>\n 4. bitpos <\/span><\/strong>: \u83b7\u53d6\u7b2c\u4e00\u4e2a 1\u62160 \u7684\u504f\u79fb\u91cf\uff0credis version 7.0\u7248\u672c\uff0c\u683c\u5f0f\uff1a bitpos key bit start end byte|bit \uff0c 7.0\u7248\u672c\u4ee5\u4e0b\u4e3a\uff1a bitpos key bit start end<\/p>\n \u8fd4\u56de\u503c\uff1a \u5f53\u6709\u4e00\u4e2a\u51680\u7684\u5b57\u7b26\u4e32\uff0c\u5bfb\u627e1\u5219\u8fd4\u56de -1<\/p>\n \u5f53\u6709\u4e00\u4e2a\u51681\u7684\u5b57\u7b26\u4e32\uff0c\u5bfb\u627e0\u5219\u8fd4\u56de\u5b57\u7b26\u4e32\u957f\u5ea6\uff0c\u4e3a\u4ec0\u4e48\u8fd4\u56de\u5b57\u7b26\u4e32\u957f\u5ea6\uff1f \u6bd4\u5982\u51681\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u4e3a9\uff0c\u7d22\u5f15\u4e3a0-8\uff0c\u6240\u4ee5\u4ece\u5de6\u5230\u53f3\u7b97\uff0c0\u7684\u7d22\u5f15\u662f9\uff0c\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u662f\u5b57\u7b26\u4e32\u957f\u5ea6\u3002<\/p>\n \u4ece\u6839\u672c\u4e0a\u8bf4\uff0c\u5982\u679c\u4f60\u5bfb\u627e0\u4e14\u6ca1\u6709\u6307\u5b9a\u8303\u56f4\u6216\u8005\u53ea\u6709start\u53c2\u6570\uff0c\u90a3\u4e48\u4f1a\u8003\u8651\u5728\u5b57\u7b26\u4e32\u7684\u53f3\u4fa7\u6dfb\u52a00.<\/p>\n \u7136\u800c\uff0c\u5982\u679c\u4f60\u5bfb\u627e0\u4e14\u6307\u5b9a\u4e86\u8303\u56f4\u5982start\u548cend\u3002\u5982\u679c\u5728\u6307\u5b9a\u7684\u8303\u56f4\u5185\u6ca1\u6709\u627e\u52300\uff0c\u5219\u8fd4\u56de-1<\/p>\n 5. bitfield<\/strong> <\/strong>: <\/span>\u8fd9\u4e2a\u547d\u4ee4\u5176\u5b9e\u5c31\u662f\u6309\u7167\u4f60\u7684\u9700\u6c42\u6765\u5206\u914d\u7684\uff0c\u53ef\u5c06\u591a\u4e2a\u5b57\u6bb5\u5b58\u50a8\u5728\u4e00\u4e2akey\u4e2d\uff0c\u5373\u4e00\u4e2abitmap\u4e2d\uff0c\u63d0\u5347\u4e86\u5185\u5b58\u7684\u4f7f\u7528\u6548\u7387\uff0c\u7279\u522b\u662f\u53ef\u7528\u4e8e\u5b9e\u65f6\u7684\u5206\u6790\u3002\u4f8b\u5982\u4f60\u9700\u8981\u4e00\u4e2a\u5e26\u7b26\u53f7\u65705\u4f4d\uff0c\u4f4d\u7f6e\u5728\u7b2c100\u4f4d\u5230104\u4f4d\uff0c i5 \u4ee3\u8868\u662f5\u4f4d\u6709\u7b26\u53f7\u6574\u578b\u6570\u5b57\uff0c100\u4ee3\u8868offset\u5373\u504f\u79fb\u91cf(\u8d77\u59cb\u4f4d\u7f6e)\uff0c15\u4e3a\u4f60\u6240\u8bbe\u5b9a\u7684\u503c\uff0c15\u7684\u4e8c\u8fdb\u5236\u503c\u4e3a(01111)\uff0c\u6ce8\u610f\u8fd9\u662f\u8865\u7801\u7684\u503c\uff0c\u4e00\u5b9a\u6ce8\u610f<\/strong><\/span>\u3002\u53ef\u53c2\u8003\u539f\u7801\uff0c\u53cd\u7801\u548c\u8865\u7801\uff08\u590d\u4e60\uff09<\/p>\n \u8ba1\u7b97\u673a\u6ca1\u6709\u51cf\u6cd5\u90fd\u662f\u52a0\u6cd5\uff0c\u8865\u7801\u51fa\u73b0\u4e0d\u4ec5\u4ec5\u4fee\u590d\u4e860\u7684\u7b26\u53f7\u4ee5\u53ca\u5b58\u5728\u4e24\u4e2a\u7f16\u7801\u7684\u95ee\u9898, \u800c\u4e14\u8fd8\u80fd\u591f\u591a\u8868\u793a\u4e00\u4e2a\u6700\u4f4e\u6570\uff0c\u5373 5\u4f4d\u5e26\u7b26\u53f7\u6570\uff08-16\uff0c+15\uff09<\/p>\n \u7b2c\u4e8c\u79cdoffset\u5b9a\u4f4d\u7684\u65b9\u5f0f\u662f\uff1a\u4f7f\u7528\u201c#\u201d\uff0c\u4ece0\u5f00\u59cb\uff0c\u4ece\u5de6\u5230\u53f3\uff0c\u8fd9\u6837\u7684\u597d\u5904\u662f\u4e0d\u7528\u624b\u52a8\u7684\u8ba1\u7b97\u4f4d\u6570\u5e76\u4e14\u4e24\u4e2a\u53d8\u91cf\u662f\u7d27\u7d27\u6328\u7740\u7684\u3002<\/strong><\/span><\/p>\n \u8fd8\u6709\u4e00\u4e2a\u662f\u5bf9\u6ea2\u51fa\u7684\u63a7\u5236\uff0c\u5206\u4e3a\u4e09\u79cd\u63a7\u5236\u6ea2\u51fa\u7684\u65b9\u5f0f\u5373wrap\uff0csat\uff0cfail\uff0c\u9ed8\u8ba4\u4e3awrap<\/strong><\/span><\/p>\n wrap\u662f\u5faa\u73af\uff0c\u5373\u5230\u4e86\u6700\u5927\u503c\u518d\u52a01\uff0c\u5219\u4e3a\u6700\u5c0f\u503c\uff0c\u4f9d\u6b21\u5faa\u73af\u3002<\/strong><\/span><\/p>\n sat\u662f\u56fa\u5b9a\uff0c\u5373\u5230\u4e86\u6700\u5927\u503c\u5219\u4e0d\u518d\u53d8\u5316<\/strong><\/span><\/p>\n fail\u662f\u5931\u8d25\uff0c\u5373\u5230\u4e86\u6700\u5927\u503c\u518d\u52a01\u5219\u6b64\u64cd\u4f5c\u5931\u8d25\u3002<\/strong><\/span><\/p>\n \u683c\u5f0f\u4e3a\uff1a bitfield mykey overflow wrap incrby u2 #0 1<\/strong><\/span><\/p>\n bitfield\u6700\u4f73\u5b9e\u8df5\uff1a \u65f6\u5e8f\u8bb0\u5f55\u6570\u636e<\/strong><\/span> \uff0c\u6587\u4e2d\u662f\u8bb0\u5f55\u4e00\u5929\u4e4b\u5185\u6bcf\u5206\u949f\u7684\u6e29\u5ea6\uff0c\u901a\u8fc78\u4f4d\u65e0\u7b26\u53f7\u6570\u5b57\uff0c\u901a\u8fc7offset \u201c#\u201d\u8fdb\u884c\u5b9a\u4f4d\uff0c\u4e00\u5206\u949f\u7684\u6e29\u5ea6\u6240\u5360\u7528\u4e00\u4e2a\u5b57\u8282\uff0c\u4e00\u5929\u603b\u5171\u5360\u75281.44KB\u7684\u5927\u5c0f<\/p>\n 6. getrange<\/strong> <\/strong>: \u83b7\u53d6\u533a\u95f4\u5185\u7684\u503c<\/strong><\/span><\/p>\n 7. strlen \uff1a \u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u5355\u4f4d\u4e3a\u5b57\u8282<\/strong><\/span><\/p>\n \u5b8c\u6574\u793a\u4f8b\uff1a<\/p>\n0. \u57fa\u7840\u77e5\u8bc6(bitmap)<\/strong><\/span><\/h2>\n
127.0.0.1:6379> set name frank OK 127.0.0.1:6379> get name frank 127.0.0.1:6379> setbit name 7 1 0 127.0.0.1:6379> getbit name 7 1 127.0.0.1:6379> get name grank 127.0.0.1:6379> setbit name 22 1 0 127.0.0.1:6379> get name grcnk<\/code><\/pre>\n
\n\n
\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n<\/tr>\n \n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n<\/tr>\n \n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n<\/tr>\n \n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n<\/tr>\n \n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 1<\/span><\/strong><\/td>\n 1<\/span><\/strong><\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n<\/tr>\n \n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 1<\/strong><\/span><\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n<\/tr>\n \n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 1<\/strong><\/span><\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n<\/tr>\n \n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 1<\/strong><\/span><\/td>\n 1<\/strong><\/span><\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n<\/tr>\n \n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 1<\/strong><\/span><\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n<\/tr>\n \n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 1<\/strong><\/span><\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n 0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n \n\n
\n coordinate\u5750\u6807<\/td>\n offset\u504f\u79fb\u91cf<\/td>\n<\/tr>\n \n (4,0)<\/td>\n 4<\/td>\n<\/tr>\n \n (4,1)<\/td>\n 14<\/td>\n<\/tr>\n \n (4,2)<\/td>\n 24<\/td>\n<\/tr>\n \n (5,2)<\/td>\n 25<\/td>\n<\/tr>\n \n (5,3)<\/td>\n 35<\/td>\n<\/tr>\n \n (5,4)<\/td>\n 45<\/td>\n<\/tr>\n \n (5,5)<\/td>\n 55<\/td>\n<\/tr>\n \n (6,5)<\/td>\n 56<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n \u4e00. \u4f4d\u8fd0\u7b97<\/strong><\/span><\/h2>\n
127.0.0.1:6379> BITFIELD bit-ts SET u8 #59 23 SET u8 #719 25<\/code><\/pre>\n
127.0.0.1:6379> bitfield mykey set i5 100 15 0 127.0.0.1:6379> getbit mykey 100 0 127.0.0.1:6379> getbit mykey 101 1 127.0.0.1:6379> getbit mykey 102 1 127.0.0.1:6379> getbit mykey 103 1 127.0.0.1:6379> getbit mykey 104 1 # \u5f53\u5bf9\u5e26\u7b26\u53f7\u7684\u503c \u589e\u52a01\uff0c\u4f1a\u51fa\u73b0\u6ea2\u51fa\u7684\u60c5\u51b5\uff0c\u5373 -16 # \u4f46\u4e8c\u8fdb\u5236\u4f4d\u6570\u663e\u793a\u662f\u4e00\u81f4\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4 10000 \u8fd9\u662f-16\u7684\u8865\u7801\uff0c\u6240\u4ee5\u8fd9\u91cc\u7684\u503c\u624d\u4e3a-16 127.0.0.1:6379> bitfield mykey incrby i5 100 1 -16 127.0.0.1:6379> getbit mykey 100 1 127.0.0.1:6379> getbit mykey 101 0 127.0.0.1:6379> getbit mykey 102 0 127.0.0.1:6379> getbit mykey 103 0 127.0.0.1:6379> getbit mykey 104 0 127.0.0.1:6379> bitfield mykey2 set i5 100 -16 0 127.0.0.1:6379> getbit mykey2 100 1 127.0.0.1:6379> getbit mykey2 101 0 127.0.0.1:6379> getbit mykey2 102 0 127.0.0.1:6379> getbit mykey2 103 0 127.0.0.1:6379> getbit mykey2 104 0 127.0.0.1:6379> bitfield mystring set i8 #0 100 set i8 #1 200 0 0 127.0.0.1:6379> getbit mystring 0 0 127.0.0.1:6379> getbit mystring 1 1 127.0.0.1:6379> getbit mystring 2 1 127.0.0.1:6379> getbit mystring 3 0 127.0.0.1:6379> getbit mystring 4 0 127.0.0.1:6379> getbit mystring 5 1 127.0.0.1:6379> getbit mystring 6 0 127.0.0.1:6379> getbit mystring 7 0 127.0.0.1:6379> 127.0.0.1:6379> getbit mystring 8 1 127.0.0.1:6379> getbit mystring 9 1 127.0.0.1:6379> getbit mystring 10 0 127.0.0.1:6379> getbit mystring 11 0 127.0.0.1:6379> getbit mystring 12 1 127.0.0.1:6379> getbit mystring 13 0 127.0.0.1:6379> getbit mystring 14 0 127.0.0.1:6379> getbit mystring 15 0 127.0.0.1:6379> bitfield mystring get i8 #1 -56 ()\u662f\u8865\u7801\uff0c\u5b83\u7684\u503c\u4e3a-56\uff0c\u800c\u4e0d\u662f200\uff0c\u8fd9\u662f\u56e0\u4e3a\u6ea2\u51fa\u4e86<\/code><\/pre>\n
# 0. 127.0.0.1:6379> setbit mybinarystring 4 1 0 127.0.0.1:6379> setbit mybinarystring 14 1 0 127.0.0.1:6379> setbit mybinarystring 24 1 0 127.0.0.1:6379> setbit mybinarystring 25 1 0 127.0.0.1:6379> setbit mybinarystring 35 1 0 127.0.0.1:6379> setbit mybinarystring 45 1 0 127.0.0.1:6379> setbit mybinarystring 55 1 0 127.0.0.1:6379> setbit mybinarystring 56 1 0 # 1. 127.0.0.1:6379> getbit mybinarystring 24 1 # 2. 127.0.0.1:6379> bitcount mybinarystring 8 # 3. \u6211\u4eec\u76f4\u63a5copy mybinarystring\u7684\u503c 127.0.0.1:6379> copy mybinarystring mybinarystring2 1 127.0.0.1:6379> bitop and result mybinarystring mybinarystring2 8 127.0.0.1:6379> getbit result 4 1 127.0.0.1:6379> bitop or result2 mybinarystring mybinarystring2 8 127.0.0.1:6379> getbit result2 4 1 127.0.0.1:6379> bitop xor result3 mybinarystring mybinarystring2 8 127.0.0.1:6379> getbit result3 55 0 127.0.0.1:6379> getbit result3 56 0 127.0.0.1:6379> bitop not result4 mybinarystring 8 127.0.0.1:6379> getbit result4 45 0 127.0.0.1:6379> getbit result4 44 1 # 4. 127.0.0.1:6379> bitpos result4 1 0 127.0.0.1:6379> set mykey \"\\xff\\xf0\\x00\" OK 127.0.0.1:6379> get mykey \u2592\u2592 127.0.0.1:6379> bitpos mykey 0 12 127.0.0.1:6379> set mykey \"\\x00\\xff\\xf0\" OK 127.0.0.1:6379> bitpos mykey 1 0 8 127.0.0.1:6379> bitpos mykey 1 2 16 127.0.0.1:6379> bitpos mykey 1 3 -1 127.0.0.1:6379> bitpos mykey 1 1 8 127.0.0.1:6379> bitpos mykey 1 0 -1 8 127.0.0.1:6379> bitpos mykey 1 0 1 8 127.0.0.1:6379> bitpos mykey 1 0 0 -1 # \u6211\u7684redis\u7684\u7248\u672c\u662f6.0\uff0c\u6240\u4ee5\u4f1a\u51fa\u73b0\u8bed\u6cd5\u9519\u8bef 127.0.0.1:6379> bitpos mykey 1 0 1 byte ERR syntax error 127.0.0.1:6379> bitpos mykey 1 0 1 bit ERR syntax error # 5. bitfield # \u6709\u4e00\u4e2a\u5728\u7ebf\u6e38\u620f\uff0c\u9700\u8981\u8bb0\u5f55\u6bcf\u4e2a\u73a9\u5bb6\u7684\u91d1\u5e01\u6570\u548c\u6740\u654c\u6570\uff0c\u8fd9\u4e2a\u6e38\u620f\u662f\u8ba9\u4eba\u75f4\u8ff7\u7684\uff0c\u957f\u5ea6\u4e3a32\u4f4d\uff0c\u4f7f\u7528 bitfield \u53ef\u4ee5\u5b58\u50a8\u8fd9\u4e24\u4e2a\u5173\u952e\u4fe1\u606f\u3002 # \u6e38\u620f\u521d\u59cb\u5316\u4e3a\u6bcf\u4e2a\u73a9\u5bb6\u4e3a1000\u91d1\u5e01 127.0.0.1:6379> BITFIELD player:1:stats SET u32 #0 1000 1) (integer) 0 # \u5f53\u6740\u6389\u5996\u602a\u6551\u51fa\u4eba\u8d28\u540e\u5c06\u4f1a\u589e\u52a050\u91d1\u5e01\uff0c\u5e76\u589e\u52a0\u4e00\u4e2a\u5996\u602a\u6570\u91cf 127.0.0.1:6379> BITFIELD player:1:stats INCRBY u32 #0 50 INCRBY u32 #1 1 1) (integer) 1050 2) (integer) 1 # \u82b1\u8d39999\u91d1\u5e01\u4e70\u4e00\u4e2a\u88c5\u5907 127.0.0.1:6379> BITFIELD player:1:stats INCRBY u32 #0 -999 1) (integer) 51 # \u83b7\u53d6\u73a9\u5bb6\u7684\u4fe1\u606f 127.0.0.1:6379> BITFIELD player:1:stats GET u32 #0 GET u32 #1 1) (integer) 51 2) (integer) 1 # overflow # sat 127.0.0.1:6379> bitfield mykey overflow sat incrby u2 300 1 1) (integer) 1 127.0.0.1:6379> bitfield mykey overflow sat incrby u2 300 1 1) (integer) 2 127.0.0.1:6379> bitfield mykey overflow sat incrby u2 300 1 1) (integer) 3 127.0.0.1:6379> bitfield mykey overflow sat incrby u2 300 1 1) (integer) 3 127.0.0.1:6379> bitfield mykey overflow sat incrby u2 300 1 1) (integer) 3 # wrap\uff0c\u8fd9\u662f\u9ed8\u8ba4\u503c 127.0.0.1:6379> bitfield mykey overflow wrap incrby u2 400 1 1) (integer) 1 127.0.0.1:6379> 127.0.0.1:6379> bitfield mykey overflow wrap incrby u2 400 1 1) (integer) 2 127.0.0.1:6379> bitfield mykey overflow wrap incrby u2 400 1 1) (integer) 3 127.0.0.1:6379> bitfield mykey overflow wrap incrby u2 400 1 1) (integer) 0 127.0.0.1:6379> bitfield mykey overflow wrap incrby u2 400 1 1) (integer) 1 # 6. \u83b7\u53d6\u8fd9\u4e2akey\u7684\u533a\u95f4\u7684\u6570\u5b57 127.0.0.1:6379> getrange mystring 0 15 \"d\\xc8\" d \u4ee3\u8868\u7684ascii\u7801\uff0c\u5373 0110 0100 \\xc8 \u4ee3\u8868\u5341\u516d\u8fdb\u5236\uff0c\u5373 1100 1000 # 7. \u83b7\u53d6\u8fd9\u4e2akey\u7684\u957f\u5ea6\uff0c\u5355\u4f4d\u4e3a\u5b57\u8282 127.0.0.1:6379> strlen mystring 2<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"redis\u547d\u4ee4\u8be6\u89e3_redis\u5e38\u7528\u547d\u4ee4\u5927\u5168redis\u6211\u8bb0\u4e0d\u4f4f\u7684\u90a3\u4e9b\u547d\u4ee4\uff08\u5341\uff09\u4f4d\u8fd0\u7b97_bitfieldredis","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/5897"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=5897"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/5897\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=5897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=5897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=5897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}