{"id":1276,"date":"2023-06-26T17:02:48","date_gmt":"2023-06-26T09:02:48","guid":{"rendered":"https:\/\/rain1024.com\/?p=1235"},"modified":"2023-08-07T20:57:23","modified_gmt":"2023-08-07T12:57:23","slug":"redis-%e5%ae%9e%e7%8e%b0%e5%ba%93%e5%ad%98%e6%89%a3%e5%87%8f","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2023\/06\/26\/redis-%e5%ae%9e%e7%8e%b0%e5%ba%93%e5%ad%98%e6%89%a3%e5%87%8f\/","title":{"rendered":"Redis \u5b9e\u73b0\u5e93\u5b58\u6263\u51cf"},"content":{"rendered":"\n\n\n<div class=\"lake-content\">\n<p id=\"u02e322c1\" class=\"ne-p\"><span class=\"ne-text\">\u5728\u65e5\u5e38\u5f00\u53d1\u4e2d\u6709\u5f88\u591a\u5730\u65b9\u90fd\u6709\u7c7b\u4f3c\u6263\u51cf\u5e93\u5b58\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u7535\u5546\u7cfb\u7edf\u4e2d\u7684\u5546\u54c1\u5e93\u5b58\uff0c\u62bd\u5956\u7cfb\u7edf\u4e2d\u7684\u5956\u54c1\u5e93\u5b58\u7b49\u3002\u8fd9\u6b21\u5206\u4eab\u7684\u662f\u5e93\u5b58\u7684\u64cd\u4f5c\u600e\u4e48\u9632\u6b62\u5546\u54c1\u88ab\u8d85\u5356\u3002<\/span><\/p>\n<p id=\"u50fae873\" class=\"ne-p\">\u00a0<\/p>\n<h3 id=\"ZoNYj\"><span class=\"ne-text\">\u89e3\u51b3\u65b9\u6848<\/span><\/h3>\n<ul class=\"ne-ul\">\n<li id=\"u156a0349\"><span class=\"ne-text\">\u57fa\u4e8e\u6570\u636e\u5e93\u5355\u5e93\u5b58<\/span><\/li>\n<li id=\"u5039d04a\"><span class=\"ne-text\">\u57fa\u4e8e\u6570\u636e\u5e93\u591a\u5e93\u5b58<\/span><\/li>\n<li id=\"u108fdba3\"><span class=\"ne-text\">\u57fa\u4e8eredis<\/span><\/li>\n<\/ul>\n<h3 id=\"KR8DM\"><span class=\"ne-text\">\u89e3\u51b3\u601d\u8def<\/span><\/h3>\n<ol class=\"ne-ol\">\n<li id=\"u0f112f36\"><span class=\"ne-text\">\u4f7f\u7528mysql\u6570\u636e\u5e93\uff0c\u4f7f\u7528\u4e00\u4e2a\u5b57\u6bb5\u6765\u5b58\u50a8\u5e93\u5b58\uff0c\u6bcf\u6b21\u6263\u51cf\u5e93\u5b58\u53bb\u66f4\u65b0\u8fd9\u4e2a\u5b57\u6bb5\u3002<\/span><\/li>\n<li id=\"u31c7c262\"><span class=\"ne-text\">\u8fd8\u662f\u4f7f\u7528\u6570\u636e\u5e93\uff0c\u4f46\u662f\u5c06\u5e93\u5b58\u5206\u5c42\u591a\u4efd\u5b58\u5230\u591a\u6761\u8bb0\u5f55\u91cc\u9762\uff0c\u6263\u51cf\u5e93\u5b58\u7684\u65f6\u5019\u8def\u7531\u4e00\u4e0b\uff0c\u8fd9\u6837\u5b50\u589e\u5927\u4e86\u5e76\u53d1\u91cf\uff0c\u4f46\u662f\u8fd8\u662f\u907f\u514d\u4e0d\u4e86\u5927\u91cf\u7684\u53bb\u8bbf\u95ee\u6570\u636e\u5e93\u6765\u66f4\u65b0\u5e93\u5b58\u3002<\/span><\/li>\n<li id=\"u0d388f83\"><span class=\"ne-text\">\u5c06\u5e93\u5b58\u653e\u5230redis\u4f7f\u7528redis\u7684incrby\u7279\u6027\u6765\u6263\u51cf\u5e93\u5b58\u3002<\/span><\/li>\n<\/ol>\n<h3 id=\"ewxhg\"><span class=\"ne-text\">\u57fa\u4e8e\u6570\u636e\u5e93\u5355\u5e93\u5b58<\/span><\/h3>\n<p id=\"u0d63527a\" class=\"ne-p\"><span class=\"ne-text\">\u7b2c\u4e00\u79cd\u65b9\u5f0f\u5728\u6240\u6709\u8bf7\u6c42\u90fd\u4f1a\u5728\u8fd9\u91cc\u7b49\u5f85\u9501\uff0c\u83b7\u53d6\u9501\u6709\u53bb\u6263\u51cf\u5e93\u5b58\u3002\u5728\u5e76\u53d1\u91cf\u4e0d\u9ad8\u7684\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4f7f\u7528\uff0c\u4f46\u662f\u4e00\u65e6\u5e76\u53d1\u91cf\u5927\u4e86\u5c31\u4f1a\u6709\u5927\u91cf\u8bf7\u6c42\u963b\u585e\u5728\u8fd9\u91cc\uff0c\u5bfc\u81f4\u8bf7\u6c42\u8d85\u65f6\uff0c\u8fdb\u800c\u6574\u4e2a\u7cfb\u7edf\u96ea\u5d29\uff1b\u800c\u4e14\u4f1a\u9891\u7e41\u7684\u53bb\u8bbf\u95ee\u6570\u636e\u5e93\uff0c\u5927\u91cf\u5360\u7528\u6570\u636e\u5e93\u8d44\u6e90\uff0c\u6240\u4ee5\u5728\u5e76\u53d1\u9ad8\u7684\u60c5\u51b5\u4e0b\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u9002\u7528\u3002<\/span><\/p>\n<h3 id=\"Vbtta\"><span class=\"ne-text\">\u57fa\u4e8e\u6570\u636e\u5e93\u591a\u5e93\u5b58<\/span><\/h3>\n<p id=\"u5058678c\" class=\"ne-p\"><span class=\"ne-text\">\u7b2c\u4e8c\u79cd\u65b9\u5f0f\u5176\u5b9e\u662f\u7b2c\u4e00\u79cd\u65b9\u5f0f\u7684\u4f18\u5316\u7248\u672c\uff0c\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u63d0\u9ad8\u4e86\u5e76\u53d1\u91cf\uff0c\u4f46\u662f\u5728\u8fd8\u662f\u4f1a\u5927\u91cf\u7684\u5bf9\u6570\u636e\u5e93\u505a\u66f4\u65b0\u64cd\u4f5c\u5927\u91cf\u5360\u7528\u6570\u636e\u5e93\u8d44\u6e90\u3002<\/span><\/p>\n<p id=\"ucee5734a\" class=\"ne-p\"><strong><span class=\"ne-text\">\u57fa\u4e8e\u6570\u636e\u5e93\u6765\u5b9e\u73b0\u6263\u51cf\u5e93\u5b58\u8fd8\u5b58\u5728\u7684\u4e00\u4e9b\u95ee\u9898\uff1a<\/span><\/strong><\/p>\n<ul class=\"ne-ul\">\n<li id=\"u29cecfde\"><span class=\"ne-text\">\u7528\u6570\u636e\u5e93\u6263\u51cf\u5e93\u5b58\u7684\u65b9\u5f0f\uff0c\u6263\u51cf\u5e93\u5b58\u7684\u64cd\u4f5c\u5fc5\u987b\u5728\u4e00\u6761\u8bed\u53e5\u4e2d\u6267\u884c\uff0c\u4e0d\u80fd\u5148selec\u5728update\uff0c\u8fd9\u6837\u5728\u5e76\u53d1\u4e0b\u4f1a\u51fa\u73b0\u8d85\u6263\u7684\u60c5\u51b5\u3002\u5982\uff1a<\/span><\/li>\n<\/ul>\n<pre id=\"g0pmK\" class=\"ne-codeblock language-plain\" data-language=\"plain\">update number set x=x-1 where x &gt; 0<\/pre>\n<ul class=\"ne-ul\">\n<li id=\"ua06d0cd3\"><span class=\"ne-text\">MySQL\u81ea\u8eab\u5bf9\u4e8e\u9ad8\u5e76\u53d1\u7684\u5904\u7406\u6027\u80fd\u5c31\u4f1a\u51fa\u73b0\u95ee\u9898\uff0c\u4e00\u822c\u6765\u8bf4\uff0cMySQL\u7684\u5904\u7406\u6027\u80fd\u4f1a\u968f\u7740\u5e76\u53d1thread\u4e0a\u5347\u800c\u4e0a\u5347\uff0c\u4f46\u662f\u5230\u4e86\u4e00\u5b9a\u7684\u5e76\u53d1\u5ea6\u4e4b\u540e\u4f1a\u51fa\u73b0\u660e\u663e\u7684\u62d0\u70b9\uff0c\u4e4b\u540e\u4e00\u8def\u4e0b\u964d\uff0c\u6700\u7ec8\u751a\u81f3\u4f1a\u6bd4\u5355thread\u7684\u6027\u80fd\u8fd8\u8981\u5dee\u3002<\/span><\/li>\n<li id=\"u7dfdfaef\"><span class=\"ne-text\">\u5f53\u51cf\u5e93\u5b58\u548c\u9ad8\u5e76\u53d1\u78b0\u5230\u4e00\u8d77\u7684\u65f6\u5019\uff0c\u7531\u4e8e\u64cd\u4f5c\u7684\u5e93\u5b58\u6570\u76ee\u5728\u540c\u4e00\u884c\uff0c\u5c31\u4f1a\u51fa\u73b0\u4e89\u62a2InnoDB\u884c\u9501\u7684\u95ee\u9898\uff0c\u5bfc\u81f4\u51fa\u73b0\u4e92\u76f8\u7b49\u5f85\u751a\u81f3\u6b7b\u9501\uff0c\u4ece\u800c\u5927\u5927\u964d\u4f4eMySQL\u7684\u5904\u7406\u6027\u80fd\uff0c\u6700\u7ec8\u5bfc\u81f4\u524d\u7aef\u9875\u9762\u51fa\u73b0\u8d85\u65f6\u5f02\u5e38\u3002<\/span><\/li>\n<\/ul>\n<h3 id=\"Go0F3\"><span class=\"ne-text\">\u57fa\u4e8eredis<\/span><\/h3>\n<p id=\"u3b0772b5\" class=\"ne-p\"><span class=\"ne-text\">\u9488\u5bf9\u4e0a\u8ff0\u95ee\u9898\u7684\u95ee\u9898\u6211\u4eec\u5c31\u6709\u4e86\u7b2c\u4e09\u79cd\u65b9\u6848\uff0c\u5c06\u5e93\u5b58\u653e\u5230\u7f13\u5b58\uff0c\u5229\u7528redis\u7684incrby\u7279\u6027\u6765\u6263\u51cf\u5e93\u5b58\uff0c\u89e3\u51b3\u4e86\u8d85\u6263\u548c\u6027\u80fd\u95ee\u9898\u3002\u4f46\u662f\u4e00\u65e6\u7f13\u5b58\u4e22\u5931\u9700\u8981\u8003\u8651\u6062\u590d\u65b9\u6848\u3002\u6bd4\u5982\u62bd\u5956\u7cfb\u7edf\u6263\u5956\u54c1\u5e93\u5b58\u7684\u65f6\u5019\uff0c\u521d\u59cb\u5e93\u5b58=\u603b\u7684\u5e93\u5b58\u6570-\u5df2\u7ecf\u53d1\u653e\u7684\u5956\u52b1\u6570\uff0c\u4f46\u662f\u5982\u679c\u662f\u5f02\u6b65\u53d1\u5956\uff0c\u9700\u8981\u7b49\u5230MQ\u6d88\u606f\u6d88\u8d39\u5b8c\u4e86\u624d\u80fd\u91cd\u542fredis\u521d\u59cb\u5316\u5e93\u5b58\uff0c\u5426\u5219\u4e5f\u5b58\u5728\u5e93\u5b58\u4e0d\u4e00\u81f4\u7684\u95ee\u9898\u3002<\/span><span class=\"ne-text\"><br \/><br \/><\/span><\/p>\n<p id=\"u0b44d4ba\" class=\"ne-p\"><span class=\"ne-text\">\u57fa\u4e8eredis\u5b9e\u73b0\u6263\u51cf\u5e93\u5b58\u7684\u5177\u4f53\u5b9e\u73b0<\/span><\/p>\n<ol class=\"ne-ol\">\n<li id=\"u7f6ecd18\"><span class=\"ne-text\">\u4f7f\u7528redis\u7684lua\u811a\u672c\u6765\u5b9e\u73b0\u6263\u51cf\u5e93\u5b58<\/span><\/li>\n<li id=\"u583d027c\"><span class=\"ne-text\">\u5982\u679c\u662f\u5206\u5e03\u5f0f\u73af\u5883\u4e0b\u6240\u4ee5\u8fd8\u9700\u8981\u4e00\u4e2a\u5206\u5e03\u5f0f\u9501\u6765\u63a7\u5236\u540c\u65f6\u53ea\u6709\u4e00\u4e2a\u670d\u52a1\u53bb\u521d\u59cb\u5316\u5e93\u5b58<\/span><\/li>\n<li id=\"ude5ba84a\"><span class=\"ne-text\">\u9700\u8981\u63d0\u4f9b\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u5728\u521d\u59cb\u5316\u5e93\u5b58\u7684\u65f6\u5019\u53bb\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u83b7\u53d6\u521d\u59cb\u5316\u5e93\u5b58<\/span><\/li>\n<\/ol>\n<pre id=\"VhrSJ\" class=\"ne-codeblock language-plain\" data-language=\"plain\">\/**\n * \u83b7\u53d6\u5e93\u5b58\u56de\u8c03\n * @author \n *\/\npublic interface IStockCallback {\n\n \/**\n  * \u83b7\u53d6\u5e93\u5b58\n  * @return\n  *\/\n int getStock();\n}\n\u6263\u51cf\u5e93\u5b58\u670d\u52a1\uff08StockService\uff09\n\/**\n * \u6263\u5e93\u5b58\n *\n * @author bjzhangzhenzhen\n *\/\n@Service\npublic class StockService {\n    Logger logger = LoggerFactory.getLogger(StockService.class);\n\n    \/**\n     * \u4e0d\u9650\u5e93\u5b58\n     *\/\n    public static final long UNINITIALIZED_STOCK = -3L;\n\n    \/**\n     * Redis \u5ba2\u6237\u7aef\n     *\/\n    @Autowired\n    private RedisTemplate&lt;String, Object&gt; redisTemplate;\n\n    \/**\n     * \u6267\u884c\u6263\u5e93\u5b58\u7684\u811a\u672c\n     *\/\n    public static final String STOCK_LUA;\n\n    static {\n        \/**\n         *\n         * @desc \u6263\u51cf\u5e93\u5b58Lua\u811a\u672c\n         * \u5e93\u5b58\uff08stock\uff09-1\uff1a\u8868\u793a\u4e0d\u9650\u5e93\u5b58\n         * \u5e93\u5b58\uff08stock\uff090\uff1a\u8868\u793a\u6ca1\u6709\u5e93\u5b58\n         * \u5e93\u5b58\uff08stock\uff09\u5927\u4e8e0\uff1a\u8868\u793a\u5269\u4f59\u5e93\u5b58\n         *\n         * @params \u5e93\u5b58key\n         * @return\n         *   -3:\u5e93\u5b58\u672a\u521d\u59cb\u5316\n         *   -2:\u5e93\u5b58\u4e0d\u8db3\n         *   -1:\u4e0d\u9650\u5e93\u5b58\n         *   \u5927\u4e8e\u7b49\u4e8e0:\u5269\u4f59\u5e93\u5b58\uff08\u6263\u51cf\u4e4b\u540e\u5269\u4f59\u7684\u5e93\u5b58\uff09\n         *      redis\u7f13\u5b58\u7684\u5e93\u5b58(value)\u662f-1\u8868\u793a\u4e0d\u9650\u5e93\u5b58\uff0c\u76f4\u63a5\u8fd4\u56de1\n         *\/\n        StringBuilder sb = new StringBuilder();\n        sb.append(\"if (redis.call('exists', KEYS[1]) == 1) then\");\n        sb.append(\"    local stock = tonumber(redis.call('get', KEYS[1]));\");\n        sb.append(\"    local num = tonumber(ARGV[1]);\");\n        sb.append(\"    if (stock == -1) then\");\n        sb.append(\"        return -1;\");\n        sb.append(\"    end;\");\n        sb.append(\"    if (stock &gt;= num) then\");\n        sb.append(\"        return redis.call('incrby', KEYS[1], 0 - num);\");\n        sb.append(\"    end;\");\n        sb.append(\"    return -2;\");\n        sb.append(\"end;\");\n        sb.append(\"return -3;\");\n        STOCK_LUA = sb.toString();\n    }\n\n    \/**\n     * @param key           \u5e93\u5b58key\n     * @param expire        \u5e93\u5b58\u6709\u6548\u65f6\u95f4,\u5355\u4f4d\u79d2\n     * @param num           \u6263\u51cf\u6570\u91cf\n     * @param stockCallback \u521d\u59cb\u5316\u5e93\u5b58\u56de\u8c03\u51fd\u6570\n     * @return -2:\u5e93\u5b58\u4e0d\u8db3; -1:\u4e0d\u9650\u5e93\u5b58; \u5927\u4e8e\u7b49\u4e8e0:\u6263\u51cf\u5e93\u5b58\u4e4b\u540e\u7684\u5269\u4f59\u5e93\u5b58\n     *\/\n    public long stock(String key, long expire, int num, IStockCallback stockCallback) {\n        long stock = stock(key, num);\n        \/\/ \u521d\u59cb\u5316\u5e93\u5b58\n        if (stock == UNINITIALIZED_STOCK) {\n            RedisLock redisLock = new RedisLock(redisTemplate, key);\n            try {\n                \/\/ \u83b7\u53d6\u9501\n                if (redisLock.tryLock()) {\n                    \/\/ \u53cc\u91cd\u9a8c\u8bc1\uff0c\u907f\u514d\u5e76\u53d1\u65f6\u91cd\u590d\u56de\u6e90\u5230\u6570\u636e\u5e93\n                    stock = stock(key, num);\n                    if (stock == UNINITIALIZED_STOCK) {\n                        \/\/ \u83b7\u53d6\u521d\u59cb\u5316\u5e93\u5b58\n                        final int initStock = stockCallback.getStock();\n                        \/\/ \u5c06\u5e93\u5b58\u8bbe\u7f6e\u5230redis\n                        redisTemplate.opsForValue().set(key, initStock, expire, TimeUnit.SECONDS);\n                        \/\/ \u8c03\u4e00\u6b21\u6263\u5e93\u5b58\u7684\u64cd\u4f5c\n                        stock = stock(key, num);\n                    }\n                }\n            } catch (Exception e) {\n                logger.error(e.getMessage(), e);\n            } finally {\n                redisLock.unlock();\n            }\n\n        }\n        return stock;\n    }\n\n    \/**\n     * \u52a0\u5e93\u5b58(\u8fd8\u539f\u5e93\u5b58)\n     *\n     * @param key    \u5e93\u5b58key\n     * @param num    \u5e93\u5b58\u6570\u91cf\n     * @return\n     *\/\n    public long addStock(String key, int num) {\n\n        return addStock(key, null, num);\n    }\n\n    \/**\n     * \u52a0\u5e93\u5b58\n     *\n     * @param key    \u5e93\u5b58key\n     * @param expire \u8fc7\u671f\u65f6\u95f4\uff08\u79d2\uff09\n     * @param num    \u5e93\u5b58\u6570\u91cf\n     * @return\n     *\/\n    public long addStock(String key, Long expire, int num) {\n        boolean hasKey = redisTemplate.hasKey(key);\n        \/\/ \u5224\u65adkey\u662f\u5426\u5b58\u5728\uff0c\u5b58\u5728\u5c31\u76f4\u63a5\u66f4\u65b0\n        if (hasKey) {\n            return redisTemplate.opsForValue().increment(key, num);\n        }\n\n        Assert.notNull(expire,\"\u521d\u59cb\u5316\u5e93\u5b58\u5931\u8d25\uff0c\u5e93\u5b58\u8fc7\u671f\u65f6\u95f4\u4e0d\u80fd\u4e3anull\");\n        RedisLock redisLock = new RedisLock(redisTemplate, key);\n        try {\n            if (redisLock.tryLock()) {\n                \/\/ \u83b7\u53d6\u5230\u9501\u540e\u518d\u6b21\u5224\u65ad\u4e00\u4e0b\u662f\u5426\u6709key\n                hasKey = redisTemplate.hasKey(key);\n                if (!hasKey) {\n                    \/\/ \u521d\u59cb\u5316\u5e93\u5b58\n                    redisTemplate.opsForValue().set(key, num, expire, TimeUnit.SECONDS);\n                }\n            }\n        } catch (Exception e) {\n            logger.error(e.getMessage(), e);\n        } finally {\n            redisLock.unlock();\n        }\n\n        return num;\n    }\n\n    \/**\n     * \u83b7\u53d6\u5e93\u5b58\n     *\n     * @param key \u5e93\u5b58key\n     * @return -1:\u4e0d\u9650\u5e93\u5b58; \u5927\u4e8e\u7b49\u4e8e0:\u5269\u4f59\u5e93\u5b58\n     *\/\n    public int getStock(String key) {\n        Integer stock = (Integer) redisTemplate.opsForValue().get(key);\n        return stock == null ? -1 : stock;\n    }\n\n    \/**\n     * \u6263\u5e93\u5b58\n     *\n     * @param key \u5e93\u5b58key\n     * @param num \u6263\u51cf\u5e93\u5b58\u6570\u91cf\n     * @return \u6263\u51cf\u4e4b\u540e\u5269\u4f59\u7684\u5e93\u5b58\u3010-3:\u5e93\u5b58\u672a\u521d\u59cb\u5316; -2:\u5e93\u5b58\u4e0d\u8db3; -1:\u4e0d\u9650\u5e93\u5b58; \u5927\u4e8e\u7b49\u4e8e0:\u6263\u51cf\u5e93\u5b58\u4e4b\u540e\u7684\u5269\u4f59\u5e93\u5b58\u3011\n     *\/\n    private Long stock(String key, int num) {\n        \/\/ \u811a\u672c\u91cc\u7684KEYS\u53c2\u6570\n        List&lt;String&gt; keys = new ArrayList&lt;&gt;();\n        keys.add(key);\n        \/\/ \u811a\u672c\u91cc\u7684ARGV\u53c2\u6570\n        List&lt;String&gt; args = new ArrayList&lt;&gt;();\n        args.add(Integer.toString(num));\n\n        long result = redisTemplate.execute(new RedisCallback&lt;Long&gt;() {\n            @Override\n            public Long doInRedis(RedisConnection connection) throws DataAccessException {\n                Object nativeConnection = connection.getNativeConnection();\n                \/\/ \u96c6\u7fa4\u6a21\u5f0f\u548c\u5355\u673a\u6a21\u5f0f\u867d\u7136\u6267\u884c\u811a\u672c\u7684\u65b9\u6cd5\u4e00\u6837\uff0c\u4f46\u662f\u6ca1\u6709\u5171\u540c\u7684\u63a5\u53e3\uff0c\u6240\u4ee5\u53ea\u80fd\u5206\u5f00\u6267\u884c\n                \/\/ \u96c6\u7fa4\u6a21\u5f0f\n                if (nativeConnection instanceof JedisCluster) {\n                    return (Long) ((JedisCluster) nativeConnection).eval(STOCK_LUA, keys, args);\n                }\n\n                \/\/ \u5355\u673a\u6a21\u5f0f\n                else if (nativeConnection instanceof Jedis) {\n                    return (Long) ((Jedis) nativeConnection).eval(STOCK_LUA, keys, args);\n                }\n                return UNINITIALIZED_STOCK;\n            }\n        });\n        return result;\n    }\n\n}\n\u8c03\u7528\n\/**\n * @author bjzhangzhenzhen\n *\/\n@RestController\npublic class StockController {\n\n    @Autowired\n    private StockService stockService;\n\n    @RequestMapping(value = \"stock\", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)\n    public Object stock() {\n        \/\/ \u5546\u54c1ID\n        long commodityId = 1;\n        \/\/ \u5e93\u5b58ID\n        String redisKey = \"redis_key:stock:\" + commodityId;\n        long stock = stockService.stock(redisKey, 60 * 60, 2, () -&gt; initStock(commodityId));\n        return stock &gt;= 0;\n    }\n\n    \/**\n     * \u83b7\u53d6\u521d\u59cb\u7684\u5e93\u5b58\n     *\n     * @return\n     *\/\n    private int initStock(long commodityId) {\n        \/\/ TODO \u8fd9\u91cc\u505a\u4e00\u4e9b\u521d\u59cb\u5316\u5e93\u5b58\u7684\u64cd\u4f5c\n        return 1000;\n    }\n\n    @RequestMapping(value = \"getStock\", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)\n    public Object getStock() {\n        \/\/ \u5546\u54c1ID\n        long commodityId = 1;\n        \/\/ \u5e93\u5b58ID\n        String redisKey = \"redis_key:stock:\" + commodityId;\n\n        return stockService.getStock(redisKey);\n    }\n\n    @RequestMapping(value = \"addStock\", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)\n    public Object addStock() {\n        \/\/ \u5546\u54c1ID\n        long commodityId = 2;\n        \/\/ \u5e93\u5b58ID\n        String redisKey = \"redis_key:stock:\" + commodityId;\n\n        return stockService.addStock(redisKey, 2);\n    }\n}<\/pre>\n<\/div>\n\n\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u65e5\u5e38\u5f00\u53d1\u4e2d\u6709\u5f88\u591a\u5730\u65b9\u90fd\u6709\u7c7b\u4f3c\u6263\u51cf\u5e93\u5b58\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u7535\u5546\u7cfb\u7edf\u4e2d\u7684\u5546\u54c1\u5e93\u5b58\uff0c\u62bd\u5956\u7cfb\u7edf\u4e2d\u7684\u5956\u54c1\u5e93\u5b58\u7b49\u3002\u8fd9\u6b21\u5206\u4eab\u7684\u662f\u5e93\u2026 <span class=\"read-more\"><a href=\"http:\/\/rain1024.com\/index.php\/2023\/06\/26\/redis-%e5%ae%9e%e7%8e%b0%e5%ba%93%e5%ad%98%e6%89%a3%e5%87%8f\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":1300,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-1276","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programme"],"_links":{"self":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/1276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/comments?post=1276"}],"version-history":[{"count":2,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/1276\/revisions"}],"predecessor-version":[{"id":1301,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/1276\/revisions\/1301"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media\/1300"}],"wp:attachment":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media?parent=1276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=1276"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=1276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}