{"id":17351,"date":"2025-05-16T15:03:59","date_gmt":"2025-05-16T07:03:59","guid":{"rendered":"https:\/\/92it.top\/?p=17351"},"modified":"2025-05-16T15:03:59","modified_gmt":"2025-05-16T07:03:59","slug":"redis%e6%9c%80%e5%b8%b8%e7%94%a8%e7%9a%84-8%e7%a7%8d%e5%9c%ba%e6%99%af","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=17351","title":{"rendered":"Redis\u6700\u5e38\u7528\u7684 8\u79cd\u573a\u666f"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1a<a href=\"https:\/\/mp.weixin.qq.com\/s?__biz=MzIwNDAyOTI2Nw==&amp;mid=2247485340&amp;idx=1&amp;sn=94ea11cfa51922614bb3135f2a831866&amp;chksm=96c72ba0a1b0a2b61d4372b3d92689878d4d5d754aa766c56a593baa6d056b3a4b14f014a37b&amp;cur_album_id=2818595943724974083&amp;scene=190#rd\">Redis\u6700\u5e38\u7528\u7684 8\u79cd\u573a\u666f<\/a><\/p>\n\n\n\n<p>\u4f5c\u4e3a\u4e00\u540d Java\u540e\u7aef\u4eba\u5458\uff0c\u5bf9 Redis\u80af\u5b9a\u5e76\u4e0d\u964c\u751f\uff0cRedis\u4f5c\u4e3a\u4e00\u79cd\u5185\u5b58\u6570\u636e\u5e93\uff0c\u4ee5\u5176\u901f\u5ea6\u4e4b\u5feb\u5728\u7f16\u7a0b\u7684\u821e\u53f0\u4e0a\u7eb5\u6a2a\u591a\u5e74\uff0c\u90a3\u4e48\uff0cRedis\u5230\u5e95\u9002\u5408\u54ea\u4e9b\u4e1a\u52a1\u573a\u666f\uff1f\u4eca\u5929\u5c31\u6765\u804a\u4e00\u804a\u3002<\/p>\n\n\n\n<p><strong>1. \u7f13\u5b58\/\u6570\u636e\u5e93<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u7f13\u5b58\uff08Cache\uff09\u662f Redis\u4f7f\u7528\u6700\u5e7f\u6cdb\u7684\u573a\u666f\u4e4b\u4e00\uff0c\u4e5f\u662f\u5f88\u591a\u5c0f\u4f19\u4f34\u7ed3\u8bc6 Redis\u7684\u91cd\u8981\u539f\u56e0\uff0c\u5728 8\u79cd10\u500d\u63d0\u5347API\u6027\u80fd\u7684\u65b9\u5f0f \u6587\u7ae0\u4e2d\u6211\u4eec\u4e5f\u5f3a\u8c03\u4e86Redis\u53ef\u4ee5\u4f5c\u4e3a\u7f13\u5b58\u7684\u6765\u52a0\u901f API\u6027\u80fd\u3002\u5982\u4e0b\u56fe\uff0c\u5728 WebServer\u548c\u6570\u636e\u5e93\u4e4b\u95f4\u4f1a\u589e\u52a0\u4e00\u5c42 Redis\u7f13\u5b58\uff0c\u8fd9\u6837 WebServer\u53ef\u4ee5\u76f4\u63a5\u4eceRedis\u4e2d\u5feb\u901f\u62ff\u5230\u6570\u636e\u8fd4\u56de\uff0c\u52a0\u5feb\u4e86 WebServer\u7684\u54cd\u5e94\u901f\u5ea6\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"217\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/image-21-1024x217.png\" alt=\"\" class=\"wp-image-17352\" style=\"width:440px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/image-21-1024x217.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/image-21-300x64.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/image-21-768x163.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/image-21-830x176.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/image-21-230x49.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/image-21-350x74.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/image-21-480x102.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/image-21.png 1208w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u4e3e\u4f8b\uff1a<\/p>\n\n\n\n<p>1. \u7535\u5546\u9886\u57df\uff0c\u53ef\u4ee5\u7f13\u5b58\u4e00\u4e9b\u70ed\u95e8\u5546\u54c1\u7684\u9759\u6001\u4fe1\u606f\u6216\u7528\u6237\u6570\u636e\uff0c\u8fd9\u6837\uff0c\u5728\u5927\u6d41\u91cf\u8bbf\u95ee\u65f6\uff0c\u4e0d\u7528\u67e5\u8be2\u6570\u636e\u5e93\uff0c\u52a0\u901f\u8bbf\u95ee\u901f\u5ea6\u3002<\/p>\n\n\n\n<p>2. \u914d\u7f6e\u4e2d\u5fc3\uff0c\u4e1a\u52a1\u5f00\u53d1\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u4f7f\u7528\u4e00\u4e9b\u5168\u5c40\u914d\u7f6e\uff0c\u800c\u4e14\u914d\u7f6e\u66f4\u6539\u7684\u9891\u7387\u5f88\u4f4e\uff0c\u56e0\u6b64\uff0c\u53ef\u4ee5\u628a\u914d\u7f6e\u90fd\u52a0\u8f7d\u5230 Redis\u5185\u5b58\u4e2d\uff0c\u52a0\u5feb\u67e5\u8be2\u6570\u636e\u3002<\/p>\n\n\n\n<p>\u9700\u8981\u8bf4\u660e\u7684\u662f\uff1a\u5f88\u591a\u65f6\u5019\uff0c\u6211\u4eec\u90fd\u4f1a\u628a Redis\u7684\u6301\u4e45\u5316\u529f\u80fd\u6253\u5f00\uff0c\u56e0\u6b64\uff0c\u5728\u628a Redis\u5f53\u4f5c\u7f13\u5b58\u7684\u540c\u65f6\uff0c\u540c\u6837\u628a Redis\u5f53\u4f5c\u6570\u636e\u5e93\u5728\u4f7f\u7528\u3002<\/p>\n\n\n\n<p><strong>2. \u5206\u5e03\u5f0f\u9501<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5206\u5e03\u5f0f\u9501\uff08Distributed Lock\uff09\u4e5f\u662f Redis\u4f7f\u7528\u6700\u5e7f\u6cdb\u7684\u573a\u666f\u4e4b\u4e00\uff0c\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u5f53\u6211\u4eec\u5728\u5904\u7406\u6709\u5e76\u53d1\u7684\u4e1a\u52a1\u573a\u666f\u65f6\uff0c\u4e3a\u4e86\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\uff0c\u901a\u5e38\u901a\u5e38\u4f1a\u4f7f\u7528\u5206\u5e03\u5f0f\u9501\uff0c\u5355\u673a\u4e0b\uff0cRedis\u901a\u5e38\u4f7f\u7528 SET NX\uff08if Not Exist\uff09\u548c PX\uff08\u8fc7\u671f\u65f6\u95f4\uff09\u6765\u521b\u5efa\u9501\uff0c\u6307\u4ee4\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u5982\u679ckey\u4e0d\u5b58\uff0cset key=value\uff0c\n# \u5931\u6548\u65f6\u95f4\u662f expiration\u6beb\u79d2\nSET key value NX PX expiration<\/pre>\n\n\n\n<p>\u5728 Java\u4e2d\uff0cRedission\u662f\u4e00\u4e2a\u57fa\u4e8e Redis\u7684\u5206\u5e03\u5f0fJava\u5bf9\u8c61\u6620\u5c04\uff08Java Redis Client\uff09\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7279\u6027\u548c\u5de5\u5177\uff0c\u793a\u4f8b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5728 Redission\u6846\u67b6\u4e2d\u4f7f\u7528 Redis\u5206\u5e03\u5f0f\u9501\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import org.redisson.Redisson;\nimport org.redisson.api.RedissonClient;\nimport org.redisson.config.Config;\n\npublic class RedissonExample {\n    public static void main(String[] args) {\n        Config config = new Config();\n        config.useSingleServer().setAddress(\"redis:\/\/127.0.0.1:6379\");\n        RedissonClient redisson = Redisson.create(config);\n\n        \/\/ \u4f7f\u7528\u5206\u5e03\u5f0f\u9501\n        String lockKey = \"lockKey\";\n        boolean isLocked = redisson.getLock(lockKey).tryLock();\n        if (isLocked) {\n            \/\/ \u83b7\u5f97\u9501\uff0c\u6267\u884c\u76f8\u5173\u64cd\u4f5c\n            System.out.println(\"Lock acquired successfully\");\n            redisson.getLock(lockKey).unlock(); \/\/ \u91ca\u653e\u9501\n        } else {\n            System.out.println(\"Failed to acquire lock\");\n        }\n        redisson.shutdown(); \/\/ \u5173\u95ed\u8fde\u63a5\n    }\n}<\/pre>\n\n\n\n<p>\u9700\u8981\u8bf4\u660e\u7684\u662f\uff1a\u9664\u4e86Redis\uff0cZookeeper\u4e5f\u662f\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\u6bd4\u8f83\u5e38\u7528\u7684\u4e00\u79cd\u6280\u672f\u65b9\u6848\u3002<\/p>\n\n\n\n<p><strong>3. \u9650\u6d41<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u9650\u6d41\uff08Rate Limiter\uff09\u4e5f\u662f Redis\u4f7f\u7528\u6bd4\u8f83\u591a\u7684\u4e00\u4e2a\u573a\u666f\uff0c\u9650\u6d41\u662f\u4fdd\u62a4\u7cfb\u7edf\u514d\u53d7\u8fc7\u8f7d\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u786e\u4fdd\u5728\u6307\u5b9a\u65f6\u95f4\u5185\u5bf9\u7cfb\u7edf\u7684\u8bf7\u6c42\u8fdb\u884c\u9650\u5236\u3002\u5728 Redis \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 SET\u3001EXPIRE \u548c Lua \u811a\u672c\u6765\u5b9e\u73b0\u7b80\u5355\u7684\u9650\u6d41\u529f\u80fd\u3002<\/p>\n\n\n\n<p>\u793a\u4f8b\u4ee3\u7801\u5c55\u793a\u4e86Java Jedis \u5e93\u5b9e\u73b0\u57fa\u4e8e\u4ee4\u724c\u6876\u7b97\u6cd5\u7684 Redis \u9650\u6d41\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import redis.clients.jedis.Jedis;\nimport redis.clients.jedis.Response;\nimport redis.clients.jedis.Transaction;\n\npublic class RateLimiter {\n    private final Jedis jedis;\n    private final String key;\n    private final int capacity;\n    private final int tokensPerSecond;\n\n    public RateLimiter(Jedis jedis, String key, int capacity, int tokensPerSecond) {\n        this.jedis = jedis;\n        this.key = key;\n        this.capacity = capacity;\n        this.tokensPerSecond = tokensPerSecond;\n    }\n   \n    \/**\n    * \u7528\u4e8e\u68c0\u67e5\u662f\u5426\u5141\u8bb8\u8bf7\u6c42\uff0c\u6839\u636e\u5f53\u524d\u4ee4\u724c\u6570\u91cf\u548c\u5bb9\u91cf\u8fdb\u884c\u5224\u65ad\n    *\/ \n    public boolean allowRequest() {\n        long now = System.currentTimeMillis();\n        Transaction transaction = jedis.multi();\n        \/\/ Add current time with score\n        transaction.zadd(key, now, String.valueOf(now)); \n        \/\/ Remove tokens that are older than 1 second\n        transaction.zremrangeByScore(key, 0, now - 1000); \n        \/\/ Get current number of tokens\n        Response&lt;Long> sizeResponse = transaction.zcard(key); \n\n        transaction.exec();\n\n        long size = sizeResponse.get();\n        \/\/ Check if number of tokens is within capacity\n        return size &lt;= capacity; \n    }\n\n    public static void main(String[] args) {\n        Jedis jedis = new Jedis(\"localhost\"); \/\/ Connect to Redis\n        \/\/ Create rate limiter\n        RateLimiter rateLimiter = new RateLimiter(jedis, \"RateLimiterKey\", 10, 5); \n\n        for (int i = 0; i &lt; 15; i++) {\n            boolean allowed = rateLimiter.allowRequest();\n            if (allowed) {\n                System.out.println(\"Request allowed\");\n            } else {\n                System.out.println(\"Request denied\");\n            }\n        }\n\n        \/\/ Close Redis connection\n        jedis.close(); \n    }\n}\n\n<\/pre>\n\n\n\n<p><strong>4. \u4f1a\u8bdd\u5b58\u50a8<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4f1a\u8bdd\uff08Session\uff09\u4e5f\u662f Redis\u5e38\u89c1\u7684\u4e00\u79cd\u529f\u80fd\uff0c\u719f\u6089 Spring\u7684\u5c0f\u4f19\u4f34\u80af\u5b9a\u77e5\u9053\u5b83\u4e5f\u6709\u4e00\u4e2a Session\u529f\u80fd\uff0c\u90a3\u4e48\u5b83\u548c Redis\u7684 Session\u6709\u4ec0\u4e48\u533a\u522b\u5462\uff1f<\/p>\n\n\n\n<p>Spring Session\u662f\u4e00\u4e2a\u62bd\u8c61\u5c42\uff0c\u63d0\u4f9b\u4e0e\u5b58\u50a8\u540e\u7aef\u8854\u63a5\u80fd\u529b\uff0c\u81f3\u4e8e\u540e\u7aef\u91c7\u7528\u5185\u5b58\u3001\u6570\u636e\u5e93\u8fd8\u662fRedis\u5b58\u50a8Spring Session\u4e0d\u5173\u6ce8\u3002\u800c Redis Session\u662f Spring Session\u7684\u4e00\u79cd\u5177\u4f53\u5b9e\u73b0\uff0c\u5c06\u4f1a\u8bdd\u6570\u636e\u5b58\u50a8\u5728Redis\u4e2d\u3002<\/p>\n\n\n\n<p>\u4e3e\u4f8b\uff1a<\/p>\n\n\n\n<p>1. \u5b58\u50a8\u7528\u6237\u767b\u5f55\u4fe1\u606f\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#\u5c06\u7528\u6237\u7684\u767b\u5f55\u4fe1\u606f\u5b58\u50a8\u5728 Redis\u4e2d\uff0ckey\uff1auserId\uff0c value\uff1auser\u767b\u5f55\u4fe1\u606f\nSET user:userId {\"username\": \"xxx\", \"password\": \"xxx\",...}<\/pre>\n\n\n\n<p><strong>5. \u53d1\u5e03\/\u8ba2\u9605<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u53d1\u5e03\/\u8ba2\u9605\uff08Sub\/Pub\uff09\u662f Redis\u4e2d\u4e00\u4e2a\u7c7b\u4f3c\u4e8e\u6d88\u606f\u4e2d\u95f4\u4ef6\uff08MQ\uff09\u7684\u529f\u80fd\uff0c\u5f53\u6211\u4eec\u7684\u4e1a\u52a1\u4e2d\u6709\u9700\u8981\u901a\u8fc7\u4e8b\u4ef6\u89e6\u53d1\u7684\u573a\u666f\u65f6\u53ef\u4ee5\u4f7f\u7528\u8be5\u529f\u80fd\uff0c\u4e0d\u8fc7 Redis\u7684 Sub\/Pub\u529f\u80fd\u8fd8\u662f\u6bd4\u8f83\u7b80\u964b\uff0c\u6709\u590d\u6742\u7684\u4e1a\u52a1\u573a\u666f\u65f6\u8fd8\u662f\u63a8\u8350MQ\u3002<\/p>\n\n\n\n<p>\u4e3e\u4f8b\uff1a<\/p>\n\n\n\n<ul>\n<li>\u7fa4\u804a\uff0c\u5728\u5b9e\u65f6\u804a\u5929\u5e94\u7528\u4e2d\uff0c\u5229\u7528 Redis\u7684\u53d1\u5e03\/\u8ba2\u9605\u529f\u80fd\u5b9e\u73b0\u6d88\u606f\u5e7f\u64ad\u3002<\/li>\n\n\n\n<li>\u5b9e\u73b0\u4e8b\u4ef6\u901a\u77e5\u7cfb\u7edf\uff0c\u5982\u65b0\u8ba2\u5355\u901a\u77e5\u3001\u65b0\u90ae\u4ef6\u63d0\u9192\u7b49\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#\u8ba2\u9605\u9891\u9053\nSUBSCRIBE channel_xxx\n# \u5411\u9891\u9053\u4e2d\u53d1\u9001\u6d88\u606f\nPUBLISH channel_xxx \"Message content\"\n\n<\/pre>\n\n\n\n<p><strong>6. \u6392\u540d\/\u6392\u884c\u699c<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u6392\u540d\/\u6392\u884c\u699c\uff08Rank\/LeaderBoard\uff09\u662f Redis\u4e2d\u4e00\u4e2a\u6bd4\u8f83\u5b9e\u7528\u7684\u529f\u80fd\uff0c\u5728\u6587\u7ae0\u70ed\u699c\u3001\u6e38\u620f\u7ade\u6280\u6216\u793e\u533a\u5e73\u53f0\u4e2d\uff0c\u6392\u884c\u699c\uff08Leaderboard\uff09\u548c\u6392\u540d\uff08Ranking\uff09\u7cfb\u7edf\u662f\u5e38\u89c1\u7684\u529f\u80fd\uff0c\u7528\u4e8e\u5c55\u793a\u7528\u6237\u5728\u7279\u5b9a\u6d3b\u52a8\u3001\u6bd4\u8d5b\u6216\u6307\u6807\u4e0a\u7684\u6392\u540d\u60c5\u51b5\uff0c\u800c Redis\u7684\u6709\u5e8f\u96c6\u5408\uff08Sorted Set\uff09\u662f\u5b9e\u73b0\u6392\u884c\u699c\u529f\u80fd\u7684\u7406\u60f3\u6570\u636e\u7ed3\u6784\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5b58\u50a8\u6bcf\u4e2a\u6210\u5458\u7684\u5206\u6570\uff0c\u5e76\u6839\u636e\u5206\u6570\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n\n\n\n<p>\u4e3e\u4f8b\uff1a<\/p>\n\n\n\n<p>1. \u8bb0\u5f55\u7f51\u7ad9\u8bbf\u95ee\u6b21\u6570\u6216\u6587\u7ae0\u9605\u8bfb\u6b21\u6570\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u6dfb\u52a0\u6587\u7ae0\u7684\u70b9\u51fb\u91cf\n#\u5c06 Article1 \u7684\u503c\u589e\u52a01\u3002\u5982\u679cArticle1 \u4e0d\u5b58\u5728\u4e8e\u6709\u5e8f\u96c6\u5408\u4e2d\uff0c\n# \u8be5\u547d\u4ee4\u4f1a\u5c06 Article1 \u6dfb\u52a0\u5230\u96c6\u5408\uff0c\u5e76\u8bbe\u7f6e\u503c\u4e3a1\nZINCRBY leaderboard 1 \"Article1\"\nZINCRBY leaderboard 1 \"Article2\"\nZINCRBY leaderboard 1 \"Article3\"\n\n# \u83b7\u53d6 Article2\u7684\u6392\u540d\uff08\u4ece\u9ad8\u5230\u4f4e\u6392\u540d\uff09\nZREVRANK leaderboard \"Article2\" \n\n# \u83b7\u70b9\u51fb\u91cf Top10 \u7684\u6587\u7ae0\u548c\u70b9\u51fb\u91cf\nZREVRANGE leaderboard 0 9 WITHSCORES <\/pre>\n\n\n\n<p>2. \u5b9e\u73b0\u7528\u6237\u79ef\u5206\u6392\u884c\u699c\u3002<\/p>\n\n\n\n<p><strong>7. \u8ba1\u6570<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728 Redis\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 INCR\u3001DECR \u7b49\u547d\u4ee4\u6765\u8fdb\u884c\u7b80\u5355\u7684\u8ba1\u6570\u64cd\u4f5c\u3002\u8fd9\u4e9b\u547d\u4ee4\u7528\u4e8e\u5bf9\u952e\u7684\u503c\u8fdb\u884c\u9012\u589e\u6216\u9012\u51cf\u64cd\u4f5c\uff0c\u5e38\u7528\u4e8e\u5b9e\u73b0\u8ba1\u6570\u5668\u7b49\u529f\u80fd\u3002<\/p>\n\n\n\n<p>\u5e38\u7528\u8ba1\u6570\u547d\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u5bf9 key\u8fdb\u884c +1 \u64cd\u4f5c\nINCR key\n# \u5bf9key \u8fdb\u884c -1 \u64cd\u4f5c\nDECR key\n\n# \u5bf9 key\u8fdb\u884c +n \u64cd\u4f5c\nINCRBY key n\n# \u5bf9 key\u8fdb\u884c -n \u64cd\u4f5c\nDECRBY key n<\/pre>\n\n\n\n<p>\u53e6\u5916\uff0c\u5728\u6392\u540d\/\u6392\u884c\u699c\u4e2d\uff0cZINCRBY \u662f\u5bf9 Sort set\u7684\u4e00\u79cd\u8ba1\u6570\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p>\u4e3e\u4f8b\uff1a<\/p>\n\n\n\n<ul>\n<li>\u7f51\u7ad9\u8bbf\u95ee\u91cf\u8ba1\u6570\u5668\uff0c\u53ef\u4ee5\u5b9e\u65f6\u7edf\u8ba1\u7f51\u7ad9\u7684\u8bbf\u95ee\u6b21\u6570\uff1b<\/li>\n\n\n\n<li>\u70b9\u8d5e\/\u559c\u6b22\u8ba1\u6570\u5668\uff0c\u5bf9\u6587\u7ae0\u6216\u5185\u5bb9\u7684\u70b9\u8d5e\u6216\u559c\u6b22\u6b21\u6570\u8fdb\u884c\u8ba1\u6570\uff1b<\/li>\n\n\n\n<li>\u7528\u6237\u5728\u7ebf\u72b6\u6001\u7edf\u8ba1\uff0c\u7edf\u8ba1\u5728\u7ebf\u7528\u6237\u6570\u91cf\uff1b<\/li>\n<\/ul>\n\n\n\n<p><strong>8. \u5730\u7406\u4f4d\u7f6e\u5e94\u7528<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Redis\u7684\u5730\u7406\u4f4d\u7f6e\u5e94\u7528\u529f\u80fd\u4f7f\u7528\u7684\u573a\u666f\u6bd4\u8f83\u6709\u9488\u5bf9\u6027\uff0c\u5982\u679c\u4f60\u4e0d\u662f\u5728\u7279\u5b9a\u7684\u9886\u57df\uff0c\u53ef\u80fd\u5e76\u4e0d\u4f1a\u4f7f\u7528\u8be5\u529f\u80fd\uff0c\u5b83\u662f \u901a\u8fc7 Geo\u6570\u636e\u7c7b\u578b\u652f\u6301\u5730\u7406\u4f4d\u7f6e\u5b58\u50a8\u548c\u68c0\u7d22\uff0c\u53ef\u4ee5\u7528\u4e8e\u6784\u5efa\u4f4d\u7f6e\u670d\u52a1\u548c\u5730\u7406\u4f4d\u7f6e\u76f8\u5173\u7684\u5e94\u7528\u3002<\/p>\n\n\n\n<p>\u4e3e\u4f8b\uff1a<\/p>\n\n\n\n<p>1. \u70b9\u8fc7\u5916\u5356\u7684\u5c0f\u4f19\u4f34\u80af\u5b9a\u4e0d\u964c\u751f\uff0c\u5916\u5356\u5e73\u53f0\u4e0a\uff0c\u9644\u8fd1\u5546\u5bb6\u641c\u7d22\u529f\u80fd\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u67e5\u627e\u7ecf\u5ea612.087269\uff0c\u7eac\u5ea639.412669 \u9644\u8fd15 km\u7684\u8ddd\u79bb\u8303\u56f4\nGEORADIUS locations 12.087269 39.412669 5 km<\/pre>\n\n\n\n<p>2. \u6253\u8f66\u5e73\u53f0\uff0c\u6bd4\u5982\u4e00\u4e9b\u7f51\u7ea6\u8f66\u5e73\u53f0\uff0c\u5c31\u53ef\u4ee5\u628a\u53f8\u673a\uff0c\u4e58\u5ba2\u7684\u4f4d\u7f6e\u4fe1\u606f\uff08\u7ecf\u7eac\u5ea6\uff09\u901a\u8fc7Redis\u7684Geo\u6765\u5b9e\u73b0\u8ddd\u79bb\u8ba1\u7b97\u7b49\u529f\u80fd\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u6dfb\u52a0\u7ecf\u7eac\u5ea6\u4fe1\u606f\uff0c\u7ecf\u7eac\u5ea6\u662f\u7f16\u9020\u7684\uff0c\u4e0d\u4e00\u5b9a\u771f\u5b9e\nGEOADD locations 12.322389 39.125356 \"Driver\"\nGEOADD locations 12.087269 39.412669 \"Passenger\"\n# \u8ba1\u7b97\u53f8\u673a\u548c\u4e58\u5ba2\u4e24\u5730\u70b9\u95f4\u7684\u8ddd\u79bb\nGEODIST locations Driver Passenger<\/pre>\n\n\n\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86Redis\u5e38\u89c1\u7684 8\u79cd\u5b9e\u7528\u573a\u666f\uff1a<\/p>\n\n\n\n<ol>\n<li>\u7f13\u5b58\/\u6570\u636e\u5e93<\/li>\n\n\n\n<li>\u5206\u5e03\u5f0f\u9501<\/li>\n\n\n\n<li>\u9650\u6d41<\/li>\n\n\n\n<li>\u4f1a\u8bdd\u5b58\u50a8<\/li>\n\n\n\n<li>\u53d1\u5e03\/\u8ba2\u9605<\/li>\n\n\n\n<li>\u6392\u540d\/\u6392\u884c\u699c<\/li>\n\n\n\n<li>\u8ba1\u6570<\/li>\n\n\n\n<li>\u5730\u7406\u4f4d\u7f6e\u5e94\u7528<br><\/li>\n<\/ol>\n\n\n\n<p>\u5982\u679c\u4f60\u5728\u4e1a\u52a1\u4e2d\u4e0d\u786e\u5b9a\u662f\u5426\u9700\u8981\u5f15\u5165 Redis\u65f6\uff0c\u53ef\u4ee5\u53c2\u8003\u672c\u6587\uff0c\u5982\u679c\u573a\u666f\u521a\u597d\u547d\u4e2d\u5176\u4e2d\u4e00\u79cd\uff0c\u90a3\u4e48\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528 Redis\u3002\u5982\u679c\u4f60\u7684\u4e1a\u52a1\u573a\u666f\u4e0d\u5728\u8fd9 8\u79cd\u5e38\u89c1\u573a\u666f\u4e2d\uff0c\u4e0d\u4ee3\u8868\u4e0d\u80fd\u4f7f\u7528 Redis\uff0c\u53ef\u80fd\u9700\u8981\u4f60\u66f4\u591a\u7684\u6280\u672f\u8c03\u7814\u6765\u786e\u8ba4\u5408\u9002\u7684\u6280\u672f\u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1aRedis\u6700\u5e38\u7528\u7684 8\u79cd\u573a\u666f \u4f5c\u4e3a\u4e00\u540d Java\u540e\u7aef\u4eba\u5458\uff0c\u5bf9 Redis\u80af\u5b9a\u5e76\u4e0d\u964c\u751f\uff0cRedis\u4f5c\u4e3a\u4e00\u79cd [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17351"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17351"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17351\/revisions"}],"predecessor-version":[{"id":17353,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17351\/revisions\/17353"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}