{"id":201473,"date":"2025-05-10T12:05:37","date_gmt":"2025-05-10T04:05:37","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201473\/"},"modified":"2025-05-10T12:05:37","modified_gmt":"2025-05-10T04:05:37","slug":"%e5%a6%82%e4%bd%95%e4%bc%98%e9%9b%85%e5%9c%b0%e4%bd%bf%e7%94%a8redis%e4%b9%8b%e4%bd%8d%e5%9b%be%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201473\/","title":{"rendered":"\u5982\u4f55\u4f18\u96c5\u5730\u4f7f\u7528Redis\u4e4b\u4f4d\u56fe\u64cd\u4f5c"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4f18\u96c5\u5730\u4f7f\u7528Redis\u4e4b\u4f4d\u56fe\u64cd\u4f5c<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300a\u5982\u4f55\u4f18\u96c5\u5730\u4f7f\u7528Redis\u4e4b\u4f4d\u56fe\u64cd\u4f5c\u300b\uff0c\u4e3b\u8981\u5305\u62ec\u64cd\u4f5c\u3001Redis\u3001\u4f4d\u56fe\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<p>&nbsp;\u5728\u8fdb\u5165\u4eca\u5929\u7684\u4e3b\u9898\u524d\uff0c\u5148\u7b80\u5355\u5730\u89e3\u91ca\u4e0bRedis\u4e2d\u7684\u4f4d\u56fe\u5230\u5e95\u662f\u4ec0\u4e48\u3002Redis\u5b98\u65b9\u6587\u6863\u5bf9\u4e8e\u4f4d\u56fe\u7684\u4ecb\u7ecd\u5982\u4e0b\uff1a<\/p>\n<p>\u4f4d\u56fe\u4e0d\u662f\u4e00\u4e2a\u771f\u5b9e\u7684\u6570\u636e\u7c7b\u578b\uff0c\u800c\u662f\u5b9a\u4e49\u5728\u5b57\u7b26\u4e32\u7c7b\u578b\u4e0a\u7684\u9762\u5411\u4f4d\u7684\u64cd\u4f5c\u7684\u96c6\u5408\u3002\u7531\u4e8e\u5b57\u7b26\u4e32\u7c7b\u578b\u662f\u4e8c\u8fdb\u5236\u5b89\u5168\u7684\u4e8c\u8fdb\u5236\u5927\u5bf9\u8c61\uff0c\u5e76\u4e14***\u957f\u5ea6\u662f 512MB\uff0c\u9002\u5408\u4e8e\u8bbe\u7f6e 2^32\u4e2a\u4e0d\u540c\u7684\u4f4d\u3002<\/p>\n<p>\u4f4d\u64cd\u4f5c\u5206\u4e3a\u4e24\u7ec4\uff1a\u5e38\u91cf\u65f6\u95f4\u5355\u4e2a\u4f4d\u7684\u64cd\u4f5c\uff0c\u50cf\u8bbe\u7f6e\u4e00\u4e2a\u4f4d\u4e3a 1 \u6216\u8005 0\uff0c\u6216\u8005\u83b7\u53d6\u8be5\u4f4d\u7684\u503c\u3002\u5bf9\u4e00\u7ec4\u4f4d\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\u8ba1\u7b97\u6307\u5b9a\u8303\u56f4\u4f4d\u7684\u7f6e\u4f4d\u6570\u91cf\u3002<\/p>\n<p>\u4f4d\u56fe\u7684***\u4f18\u52bf\u662f\u6709\u65f6\u662f\u4e00\u79cd\u975e\u5e38\u663e\u8457\u7684\u8282\u7701\u7a7a\u95f4\u6765\u5b58\u50a8\u4fe1\u606f\u7684\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u5728\u4e00\u4e2a\u7cfb\u7edf\u4e2d\uff0c\u4e0d\u540c\u7528\u6237\u7531\u9012\u589e\u7684\u7528\u6237 ID \u6765\u8868\u793a\uff0c\u53ef\u4ee5\u4f7f\u7528 512MB \u7684\u5185\u5b58\u6765\u8868\u793a 400 \u4e07\u7528\u6237\u7684\u5355\u4e2a\u4f4d\u4fe1\u606f\uff08\u4f8b\u5982\u4ed6\u4eec\u662f\u5426\u9700\u8981\u63a5\u6536\u4fe1\u4ef6\uff09\u3002&nbsp;<\/p>\n<\/p>\n<p>\u7b80\u800c\u8a00\u4e4b\uff0c\u4f4d\u56fe\u64cd\u4f5c\u662f\u7528\u6765\u64cd\u4f5c\u6bd4\u7279\u4f4d\u7684\uff0c\u5176\u4f18\u70b9\u662f\u8282\u7701\u5185\u5b58\u7a7a\u95f4\u3002\u4e3a\u4ec0\u4e48\u53ef\u4ee5\u8282\u7701\u5185\u5b58\u7a7a\u95f4\u5462\uff1f\u5047\u5982\u6211\u4eec\u9700\u8981\u5b58\u50a8100\u4e07\u4e2a\u7528\u6237\u7684\u767b\u5f55\u72b6\u6001\uff0c\u4f7f\u7528\u4f4d\u56fe\u7684\u8bdd\u6700\u5c11\u53ea\u9700\u8981100\u4e07\u4e2a\u6bd4\u7279\u4f4d\uff08\u6bd4\u7279\u4f4d1\u8868\u793a\u767b\u5f55\uff0c\u6bd4\u7279\u4f4d0\u8868\u793a\u672a\u767b\u5f55\uff09\u5c31\u53ef\u4ee5\u5b58\u50a8\u4e86\uff0c\u800c\u5982\u679c\u4ee5\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f\u5b58\u50a8\uff0c\u6bd4\u5982\u8bf4\u4ee5userId\u4e3akey\uff0c\u662f\u5426\u767b\u5f55\uff08\u5b57\u7b26\u4e32\u201c1\u201d\u8868\u793a\u767b\u5f55\uff0c\u5b57\u7b26\u4e32\u201c0\u201d\u8868\u793a\u672a\u767b\u5f55\uff09\u4e3avalue\u8fdb\u884c\u5b58\u50a8\u7684\u8bdd\uff0c\u5c31\u9700\u8981\u5b58\u50a8100\u4e07\u4e2a\u5b57\u7b26\u4e32\u4e86\uff0c\u76f8\u6bd4\u4e4b\u4e0b\u4f7f\u7528\u4f4d\u56fe\u5b58\u50a8\u5360\u7528\u7684\u7a7a\u95f4\u8981\u5c0f\u5f97\u591a\uff0c\u8fd9\u5c31\u662f\u4f4d\u56fe\u5b58\u50a8\u7684\u4f18\u52bf\u3002<\/p>\n<p>\u4f4d\u56fe\u5e38\u7528\u64cd\u4f5c<\/p>\n<p>\u4f4d\u56fe\u7684\u5e38\u7528\u64cd\u4f5c\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>setbit<\/li>\n<\/ul>\n<p>\u8bbe\u7f6e\u7279\u5b9akey\u5bf9\u5e94\u7684\u6bd4\u7279\u4f4d\u7684\u503c\u3002<\/p>\n<ul>\n<li>getbit<\/li>\n<\/ul>\n<p>\u83b7\u53d6\u7279\u5b9akey\u5bf9\u5e94\u7684\u6bd4\u7279\u4f4d\u7684\u503c\u3002<\/p>\n<ul>\n<li>bitcount<\/li>\n<\/ul>\n<p>\u7edf\u8ba1\u7ed9\u5b9akey\u5bf9\u5e94\u7684\u5b57\u7b26\u4e32\u6bd4\u7279\u4f4d\u4e3a1\u7684\u6570\u91cf\u3002<\/p>\n<p><strong>\u4f7f\u7528\u4f4d\u56fe\u5b58\u50a8\u7528\u6237\u767b\u5f55\u72b6\u6001<\/strong><\/p>\n<p>\u4f4d\u56fe\u7684\u5e38\u89c1\u5e94\u7528\u662f\u7528\u6765\u5b58\u50a8\u72b6\u6001\u503c\uff0c\u6bd4\u5982\u5b58\u50a8\u7528\u6237\u7684\u767b\u5f55\u72b6\u6001\u3002<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u73b0\u5728\u6709\u4e00\u4e2a\u9700\u6c42\uff0c\u9700\u8981\u8bb0\u5f55\u7528\u6237\u6ce8\u518c\u4ee5\u6765\u6bcf\u5929\u7684\u767b\u5f55\u72b6\u6001\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u53ef\u4ee5\u4ee5\u7528\u6237id\u4e3akey\uff0c\u7136\u540e\u4ee5\u65e5\u671f\u6216\u8005\u65e5\u671f\u7684\u504f\u79fb\u91cf\u4f5c\u4e3a\u4e0b\u6807\uff0c\u5c06\u767b\u5f55\u72b6\u6001\u5b58\u50a8\u5230\u5bf9\u5e94\u7684\u6bd4\u7279\u4f4d\u4e2d\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u83b7\u53d6\u7528\u6237\u67d0\u4e00\u5929\u7684\u767b\u5f55\u72b6\u6001\u4e86\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u770b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<pre>public&nbsp;class&nbsp;UserLoginStatusService&nbsp;{&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;static&nbsp;final&nbsp;String&nbsp;host=\"111.111.111.111\";&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;static&nbsp;final&nbsp;int&nbsp;port=6379;&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;static&nbsp;final&nbsp;Jedis&nbsp;jedis=new&nbsp;Jedis(host,port);&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/\u4e0b\u6587\u9700\u8981\u4f7f\u7528\u65e5\u671f\u7684\u504f\u79fb\u91cf\u4f5c\u4e3aredis\u4f4d\u56fe\u7684offset\uff0c&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/\u8fd9\u91cc\u4f7f\u7528\u4e86Java&nbsp;8\u7684\u65b0\u65e5\u671fAPI&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;static&nbsp;final&nbsp;LocalDate&nbsp;beginDate=LocalDate.of(2018,1,1);&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;{&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jedis.connect();&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;setLoginStatus(String&nbsp;userId,&nbsp;LocalDate&nbsp;date,boolean&nbsp;isLogin){&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;offset&nbsp;=&nbsp;getDateDuration(beginDate,&nbsp;date);&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jedis.setbit(userId,offset,isLogin);&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;boolean&nbsp;getLoginStatus(String&nbsp;userId,LocalDate&nbsp;date){&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;offset&nbsp;=&nbsp;getDateDuration(beginDate,&nbsp;date);&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;jedis.getbit(userId,offset);&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;long&nbsp;getDateDuration(LocalDate&nbsp;start&nbsp;,LocalDate&nbsp;end){&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;start.until(end,&nbsp;ChronoUnit.DAYS);&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args)&nbsp;{&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UserLoginStatusService&nbsp;userLoginStatusService=new&nbsp;UserLoginStatusService();&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;userId=\"user_1\";&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LocalDate&nbsp;today&nbsp;=&nbsp;LocalDate.now();&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userLoginStatusService.setLoginStatus(userId,today,true);&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;boolean&nbsp;todayLoginStatus&nbsp;=&nbsp;userLoginStatusService.getLoginStatus(userId,&nbsp;today);&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(String.format(\"The&nbsp;loginStatus&nbsp;of&nbsp;%s&nbsp;in&nbsp;%s&nbsp;is&nbsp;%s\",userId,today,todayLoginStatus));&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LocalDate&nbsp;yesterday&nbsp;=&nbsp;LocalDate.now().minusDays(1);&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;boolean&nbsp;yesterdayLoginStatus&nbsp;=&nbsp;userLoginStatusService.getLoginStatus(userId,&nbsp;yesterday);&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(String.format(\"The&nbsp;loginStatus&nbsp;of&nbsp;%s&nbsp;in&nbsp;%s&nbsp;is&nbsp;%s\",userId,yesterday,yesterdayLoginStatus));&nbsp;\r\n<\/pre>\n<p>\u4ee3\u7801\u4e0d\u590d\u6742\uff0c\u6211\u4eec\u5728main\u65b9\u6cd5\u4e2d\u8bbe\u7f6e\u5f53\u5929\u7684\u767b\u5f55\u72b6\u6001\u4e3atrue\uff0c\u7136\u540e\u5206\u522b\u67e5\u51fa\u5f53\u5929\u7684\u767b\u5f55\u72b6\u6001\u548c\u6628\u5929\u7684\u767b\u5f55\u72b6\u6001\uff0c\u7531\u4e8eredis\u4f4d\u56fe\u7684\u6bd4\u7279\u4f4d\u9ed8\u8ba4\u662f0\uff0c\u6240\u4ee5\u8be5\u4ee3\u7801\u7684\u6b63\u786e\u8f93\u51fa\u5e94\u8be5\u662f\u4eca\u5929\u5df2\u767b\u5f55\uff0c\u6628\u5929\u672a\u767b\u5f55\uff0c\u6211\u4eec\u8fd0\u884c\u4e00\u6b21\u770b\u770b\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p>\u4ece\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u6765\u770b\uff0cRedis\u7684\u4f4d\u56fe\u786e\u5b9e\u6ee1\u8db3\u4e86\u6211\u4eec\u7684\u9700\u6c42\uff0c\u4e14\u517c\u6709\u8282\u7701\u5b58\u50a8\u7a7a\u95f4\u7684\u4f18\u70b9\u3002<\/p>\n<p><strong>\u4f7f\u7528\u4f4d\u56fe\u7edf\u8ba1\u767b\u5f55\u5929\u6570<\/strong><\/p>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u6709\u4e00\u4e2a\u65b0\u9700\u6c42\uff0c\u5c31\u662f\u7edf\u8ba1\u67d0\u4e2a\u7528\u6237\u6ce8\u518c\u540e\u524d10\u5929\u7684\u767b\u5f55\u5929\u6570\uff0cRedis\u4e2d\u6709\u4e2abitcount\u547d\u4ee4\uff0c\u53ef\u4ee5\u7edf\u8ba1\u67d0\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u6bd4\u7279\u4f4d\u4e3a1\u7684\u6570\u91cf\uff0c\u5176\u8fd8\u67092\u4e2a\u53c2\u6570start\u548cend\uff0c\u8868\u793a\u8981\u7edf\u8ba1\u7684\u8303\u56f4\uff0c\u548b\u4e00\u770b\u597d\u50cf\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u6211\u4eec\u8fd9\u4e2a\u9700\u6c42\uff0c\u4f46\u662f\u8fd9\u91cc\u6709\u4e00\u4e2a\u5751\u9700\u8981\u6ce8\u610f\u4e0b\uff0cbitcount\u547d\u4ee4\u7684start\u548cend\u53c2\u6570\u6307\u7684\u662f\u5b57\u8282\u7684\u7d22\u5f15\uff0c\u800c\u4e0d\u662f\u6bd4\u7279\u4f4d\u7684\u7d22\u5f15\uff0c\u800c\u6211\u4eec\u5982\u679c\u8981\u4f7f\u7528\u4f4d\u56fe\u6765\u7edf\u8ba1\u67d0\u4e2a\u7528\u6237\u6ce8\u518c\u540e\u524d10\u5929\u7684\u767b\u5f55\u5929\u6570\u7684\u8bdd\uff0c\u9700\u8981\u7edf\u8ba1\u7684\u662f\u6bd4\u7279\u4f4d\u7d22\u5f15\u4ece0\u52309\u7684\u6bd4\u7279\u503c\u4e3a1\u7684\u6570\u91cf\uff0c\u6240\u4ee5\u76f4\u63a5\u4f7f\u7528bitcount\u547d\u4ee4\u663e\u7136\u662f\u65e0\u6cd5\u6ee1\u8db3\u8981\u6c42\u7684\u3002\u90a3\u4e48\u5047\u5982\u8bf4\u6211\u4eec\u4e00\u5b9a\u8981\u7528\u4f4d\u56fe\u6765\u5b58\u50a8\u767b\u5f55\u72b6\u6001\u5462\uff0c\u5e94\u8be5\u548b\u529e\u5462\uff1f\u5176\u5b9e\u529e\u6cd5\u8fd8\u662f\u6709\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u5148\u62ff\u5230\u6bd4\u7279\u4f4d\u7d22\u5f15\u4ece0\u52309\u6240\u5728\u7684\u5b57\u8282\u6570\u7ec4\uff0c\u518d\u5c06\u8be5\u5b57\u8282\u6570\u7ec4\u89e3\u6790\u6210\u4e8c\u8fdb\u5236\u5f62\u5f0f\uff0c\u8fdb\u800c\u7edf\u8ba1\u51fa\u6bd4\u7279\u4f4d\u7d22\u5f15\u4ece0\u52309\u6bd4\u7279\u503c\u4e3a1\u7684\u6570\u91cf\u3002<\/p>\n<p>\u8981\u62ff\u5230\u6bd4\u7279\u4f4d\u7d22\u5f15\u6240\u5728\u7684\u5b57\u8282\u5728\u5b57\u8282\u6570\u7ec4\u4e2d\u7684\u4e0b\u6807\u6bd4\u8f83\u7b80\u5355\uff0c\u53ea\u8981\u5c06\u6bd4\u7279\u4f4d\u7d22\u5f15\u9664\u4ee58\uff08\u4e00\u4e2a\u5b57\u8282\u5305\u542b8\u4e2a\u6bd4\u7279\u4f4d\uff09\u518d\u5411\u4e0b\u53d6\u6574\u5c31\u884c\u4e86\u3002\u63a5\u4e0b\u6765\u5c31\u662f\u4f7f\u7528redis\u7684getrange\u547d\u4ee4\u6765\u622a\u53d6\u5b57\u8282\u6570\u7ec4\u4e86\u3002<\/p>\n<p>\u62ff\u5230\u4e86\u5b57\u8282\u6570\u7ec4\uff0c\u63a5\u4e0b\u6765\u5c31\u662f\u89e3\u6790\u5b57\u8282\u6570\u7ec4\uff0c\u7edf\u8ba1\u5176\u4e2d\u6bd4\u7279\u503c\u4e3a1\u7684\u6570\u91cf\u4e86\u3002\u6211\u4eec\u5148\u4ece\u6700\u7b80\u5355\u7684\u5355\u4e2a\u5b57\u8282\u8bf4\u8d77\uff0c\u5047\u8bbe\u4e00\u4e2a\u5b57\u8282\u7684\u5404\u4e2a\u6bd4\u7279\u4f4d\u7684\u503c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p>\u6211\u4eec\u8bbe\u6bd4\u7279\u4f4d\u7d22\u5f15\u4e3aindex\uff0c\u5047\u5982\u6211\u4eec\u8981\u8ba1\u7b97\u6bd4\u7279\u4f4d\u4e3a7\u7684\u6bd4\u7279\u503c\uff0c\u53ea\u9700\u8981\u5c06\u539f\u503c\u76f4\u63a5\u8ddf1\u8fdb\u884c\u4e0e\u8fd0\u7b97\u5c31\u884c\u4e86\u3002\u8981\u8ba1\u7b97\u6bd4\u7279\u4f4d\u4e3a6\u7684\u6bd4\u7279\u503c\uff0c\u53ea\u9700\u8981\u5c06\u539f\u503c\u53f3\u79fb1\u4f4d\uff0c\u518d\u8ddf1\u8fdb\u884c\u4e0e\u8fd0\u7b97\u3002\u4ee5\u6b64\u7c7b\u63a8\uff0c\u8981\u8ba1\u7b97\u7b2cindex\u4f4d\u7684\u6bd4\u7279\u503c\uff0c\u53ea\u9700\u8981\u5148\u53f3\u79fb(7-index)\u4f4d\uff0c\u518d\u8ddf1\u8fdb\u884c\u4e0e\u8fd0\u7b97\u5373\u53ef\u3002<\/p>\n<p>\u53ea\u8981\u80fd\u591f\u7edf\u8ba1\u51fa\u622a\u53d6\u51fa\u6765\u7684\u7684\u5b57\u8282\u6570\u7ec4\u4e2d\u6bd4\u7279\u4f4d\u7684\u503c\u4e3a1\u7684\u6570\u91cf\uff0c\u63a5\u4e0b\u6765\u518d\u51cf\u53bb\u4e0d\u5305\u542b\u5728\u5bf9\u5e94\u6bd4\u7279\u7d22\u5f15\u4e2d\u7684\u6bd4\u7279\u503c\u4e3a1\u7684\u6570\u91cf\uff0c\u5373\u53ef\u7edf\u8ba1\u51fa\u7ed9\u5b9a\u7684\u6bd4\u7279\u7d22\u5f15\u8303\u56f4\u5185\u6bd4\u7279\u503c\u4e3a1\u7684\u6570\u91cf\u3002<\/p>\n<p>\u8fd9\u4e48\u8bf4\u6709\u70b9\u62d7\u53e3\uff0c\u6211\u4eec\u4ee5\u4e0a\u8ff0\u4f8b\u5b50\u4e3a\u4f8b\u8fdb\u884c\u8bb2\u89e3\u5427\u3002\u6211\u4eec\u8981\u7edf\u8ba1\u51fa\u7528\u6237\u6ce8\u518c\u540e\u524d10\u5929\u7684\u767b\u5f55\u5929\u6570\uff0c\u5982\u679c\u7528\u4f4d\u56fe\u5b58\u50a8\u7528\u6237\u767b\u5f55\u72b6\u6001\uff0c\u4f4d\u56fe\u4e2d\u7684\u7d22\u5f15\u4e3a\u6ce8\u518c\u5929\u6570\u7684\u8bdd\uff0c\u90a3\u4e48\u6211\u4eec\u9700\u8981\u7edf\u8ba1\u6bd4\u7279\u7d22\u5f15\u4ece0\u52309\u7684\u6bd4\u7279\u503c\u4e3a1\u7684\u6570\u91cf\uff0c\u624d\u80fd\u8ba1\u7b97\u51fa\u8be5\u7528\u6237\u6ce8\u518c\u540e\u524d10\u5929\u7684\u767b\u5f55\u5929\u6570\u3002<\/p>\n<p>\u6211\u4eec\u5148\u8ba1\u7b97\u51fa\u6bd4\u7279\u7d22\u5f15\u4ece0\u52309\u5305\u542b\u5728\u54ea\u4e00\u6bb5\u5b57\u8282\u6570\u7ec4\u4e2d\uff0c\u524d\u9762\u8bf4\u4e86\uff0c\u53ea\u9700\u8981\u5c06\u5bf9\u5e94\u7684\u7d22\u5f15\u9664\u4ee58\uff0c\u518d\u5411\u4e0b\u53d6\u6574\u5c31\u884c\u4e86\u3002\u4ece\u800c\u53ef\u4ee5\u5f97\u77e5\u6bd4\u7279\u4f4d\u7d22\u5f15\u4ece0\u52309\u5bf9\u5e94\u7684\u662f\u4e0b\u6807\u4ece0\u52301\u7684\u5b57\u8282\u6570\u7ec4\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u4f7f\u7528getrange\u547d\u4ee4\u622a\u53d6\u8be5\u5b57\u8282\u6570\u7ec4\uff0c\u5047\u8bbe\u5176\u503c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<\/p>\n<p>\u5047\u8bbe\u6bd4\u7279\u7d22\u5f150\u52309\u5bf9\u5e94\u7684\u5b57\u8282\u6570\u7ec4\u7684\u6bd4\u7279\u503c\u60c5\u51b5\u5982\u4e0a\u6240\u793a\uff0c\u6211\u4eec\u9700\u8981\u7edf\u8ba1\u7684\u662f***\u4e2a\u5b57\u8282\uff08\u4e0b\u6807\u4e3a0\uff09\u4e2d\u76840\u52307\u4f4d\u4e2d\u6bd4\u7279\u503c\u4e3a1\u7684\u6570\u91cf\uff0c\u518d\u52a0\u4e0a\u7b2c\u4e8c\u4e2a\u5b57\u8282\uff08\u4e0b\u6807\u4e3a1\uff09\u4e2d\u7684\u7b2c0\u52301\u4f4d\u4e2d\u6bd4\u7279\u503c\u4e3a1\u7684\u6570\u91cf\u3002\u52a0\u8d77\u6765\u521a\u597d10\u4f4d\uff0c\u4e5f\u5c31\u662f\u5bf9\u5e94\u7528\u6237\u6ce8\u518c\u524d10\u5929\u7684\u767b\u5f55\u5929\u6570\u3002\u5f53\u7136\u6211\u4eec\u4e5f\u53ef\u4ee5\u7edf\u8ba1\u51fa\u8fd92\u4e2a\u5b57\u8282\u4e2d\u7684\u6bd4\u7279\u503c\u4e3a1\u7684\u603b\u6570\uff0c\u518d\u51cf\u53bb\u7b2c\u4e8c\u4e2a\u5b57\u8282\u7684\u4ece2\u52307\u4f4d\uff08\u4e0a\u8ff0\u8868\u683c\u6807\u7ea2\u7684\u5730\u65b9\uff09\u4e2d\u6bd4\u7279\u503c\u4e3a1\u7684\u6570\u91cf\uff0c\u4e5f\u53ef\u7edf\u8ba1\u51fa\u8be5\u7528\u6237\u6ce8\u518c\u540e\u524d10\u5929\u7684\u767b\u5f55\u5929\u6570\u3002\u672c\u6587\u7528\u7684\u662f\u7b2c\u4e8c\u79cd\u65b9\u6cd5\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u4e0a\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<pre>private&nbsp;static&nbsp;final&nbsp;int&nbsp;BIT_AMOUNT_IN_ONE_BYTE&nbsp;=8;&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;Jedis&nbsp;jedis;&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;int&nbsp;bitCountByBitIndex(String&nbsp;key,&nbsp;long&nbsp;startBitIndex,&nbsp;long&nbsp;endBitIndex)&nbsp;{&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;startByteIndex&nbsp;=&nbsp;getByteIndexInTheBytes(startBitIndex);&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;endByteIndex&nbsp;=&nbsp;getByteIndexInTheBytes(endBitIndex);&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;byte[]&nbsp;bytes&nbsp;=&nbsp;jedis.getrange(key.getBytes(),&nbsp;startByteIndex,&nbsp;endByteIndex);&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;totalBitInBytes&nbsp;=&nbsp;getTotalBitInBytes(bytes);&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;startBitIndexInFirstByte&nbsp;=&nbsp;getBitIndexInTheByte(startBitIndex);&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;endBitIndexInLastByte&nbsp;=&nbsp;getBitIndexInTheByte(endBitIndex);&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;byte&nbsp;firstByte&nbsp;=&nbsp;bytes[0];&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;byte&nbsp;lastByte&nbsp;=&nbsp;bytes[bytes.length-1];&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(int&nbsp;i=7;i&gt;(BIT_AMOUNT_IN_ONE_BYTE-1-startBitIndexInFirstByte);i--){&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(((firstByte&gt;&gt;i)&amp;1)==1){&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;totalBitInBytes--;&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(int&nbsp;i=0;i&gt;i)&amp;1)==1){&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;totalBitInBytes--;&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;totalBitInBytes;&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;int&nbsp;getTotalBitInBytes(byte[]&nbsp;bytes){&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;count=0;&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(byte&nbsp;b:bytes){&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&gt;i)&amp;1)==1){&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;count++;&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;count;&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;int&nbsp;getByteIndexInTheBytes(long&nbsp;offset){&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(int)&nbsp;offset\/&nbsp;BIT_AMOUNT_IN_ONE_BYTE;&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;int&nbsp;getBitIndexInTheByte(long&nbsp;offset){&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(int)(offset-offset\/&nbsp;BIT_AMOUNT_IN_ONE_BYTE&nbsp;*&nbsp;BIT_AMOUNT_IN_ONE_BYTE);&nbsp;\r\n<\/pre>\n<p>\u4ee3\u7801\u5c31\u4e0d\u6ce8\u91ca\u4e86\uff0c\u6574\u4f53\u601d\u8def\u5df2\u7ecf\u5728\u4e0a\u9762\u8bb2\u89e3\u4e86\u3002<\/p>\n<p>\u5f53\u7136\u8981\u5b9e\u73b0\u672c\u6587\u6240\u8ff0\u7684\u529f\u80fd\uff0c\u4e5f\u4e0d\u4e00\u5b9a\u975e\u8981\u8fd9\u4e48\u505a\uff0c\u8fd8\u662f\u6709\u5176\u4ed6\u7684\u65b9\u6848\u7684\u3002\u6bd4\u5982\uff1a\u53ef\u4ee5\u5c06\u653e\u5165\u4f4d\u56fe\u7684offset\u7edf\u4e00\u4e58\u4ee58\uff08\u4e00\u4e2a\u5b57\u8282\u53608\u6bd4\u7279\uff09\uff0c\u8fd9\u6837\u4e00\u6765\u5c31\u53ef\u4ee5\u76f4\u63a5\u7528redis\u7684bitcount\u6765\u7edf\u8ba1\u5bf9\u5e94\u7d22\u5f15\u8303\u56f4\u5185\u7684\u6bd4\u7279\u503c\u4e3a1\u7684\u6570\u91cf\u4e86\uff0c\u5f53\u7136\u8fd9\u79cd\u65b9\u6848\u7684\u7f3a\u70b9\u4e5f\u76f8\u5f53\u660e\u663e\uff0c\u5c31\u662f\u6d6a\u8d39\u5185\u5b58\uff0c\u56e0\u4e3a\u539f\u5148\u53ea\u9700\u89811\u6bd4\u7279\u5b58\u50a8\u7684\u6570\u636e\uff0c\u73b0\u5728\u9700\u89818\u6bd4\u7279\u5b58\u50a8\uff0c\u6240\u4ee5\u8fd9\u79cd\u65b9\u6848\u4e0d\u80fd\u5f88\u597d\u5730\u5229\u7528\u4f4d\u56fe\u7d22\u5f15\u8282\u7701\u5b58\u50a8\u7a7a\u95f4\u7684\u7279\u70b9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f18\u96c5\u5730\u4f7f\u7528Redis\u4e4b\u4f4d\u56fe\u64cd&#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-201473","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201473","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=201473"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201473\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}