{"id":1098,"date":"2022-01-22T21:08:17","date_gmt":"2022-01-22T13:08:17","guid":{"rendered":"https:\/\/fireinsect.top\/?p=1098"},"modified":"2022-05-02T21:11:52","modified_gmt":"2022-05-02T13:11:52","slug":"redis%e4%ba%8b%e5%8a%a1","status":"publish","type":"post","link":"https:\/\/fireinsect.top\/index.php\/2022\/01\/22\/redis%e4%ba%8b%e5%8a%a1\/","title":{"rendered":"Redis\u4e8b\u52a1"},"content":{"rendered":"\n<p>\u5f53\u4f60\u60f3\u505a\u4e00\u4e2a\u62a2\u8d2d\u7a0b\u5e8f\uff0c\u5229\u7528\u539f\u59cb\u7684if\u8bed\u53e5\u6765\u5b9e\u73b0\uff0c\u4f1a\u53d1\u73b0\u663e\u793a\u62a2\u8d2d\u6210\u529f\u7684\u6570\u91cf\u5927\u4e8e\u9884\u5b9a\u503c\u3002\u8fd9\u662f\u7531\u4e8e\u62a2\u8d2d\u672c\u8eab\u662f\u4e00\u4e2a\u5e76\u53d1\u64cd\u4f5c\uff0c\u7cfb\u7edf\u53d1\u51fa\u591a\u4e2a\u5e76\u53d1\u8bf7\u6c42\uff0c\u6709\u6162\u6709\u5feb\uff0c\u5f53\u4e00\u4e2a\u8bf7\u6c42\u8fdb\u884c\u5224\u65ad\u65f6\u53ef\u80fd\u6b64\u65f6\u5176\u4f59\u60c5\u51b5\u8fd8\u6ca1\u8fdb\u884c\u8d27\u5b58\u91cf-1\u7684\u64cd\u4f5c\uff0c\u5bfc\u81f4\u201c\u6210\u529f\u201d\u6570\u91cf\u4f1a\u5927\u4e8e\u9884\u8ba2\u503c\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>\u8fd9\u65f6\u5019\u6211\u4eec\u9700\u8981\u4f7f\u7528\u4e8b\u52a1<\/p>\n\n\n\n<p class=\"has-luminous-vivid-orange-color has-text-color has-medium-font-size\">\u4ec0\u4e48\u662f\u4e8b\u52a1<\/p>\n\n\n\n<p>\u4e8b\u52a1\uff08Transaction\uff09\u662f\u6307\u5c06\u4e00\u4e2a\u4e1a\u52a1\u903b\u8f91\u4f5c\u4e3a\u4e00\u4e2a\u6574\u4f53\u4e00\u8d77\u6267\u884c\u3002\u4e8b\u52a1\u5176\u5b9e\u5c31\u662f\u6253\u5305\u4e00\u7ec4\u64cd\u4f5c\uff08\u6216\u8005\u547d\u4ee4\uff09\u4f5c\u4e3a\u4e00\u4e2a\u6574\u4f53\uff0c\u5728\u4e8b\u52a1\u5904\u7406\u65f6\u5c06\u987a\u5e8f\u6267\u884c\u8fd9\u4e9b\u64cd\u4f5c\uff0c\u5e76\u8fd4\u56de\u7ed3\u679c\uff0c\u5982\u679c\u5176\u4e2d\u4efb\u4f55\u4e00\u4e2a\u73af\u8282\u51fa\u9519\uff0c\u6240\u6709\u7684\u64cd\u4f5c\u5c06\u88ab\u56de\u6eda\u3002<\/p>\n\n\n\n<p>Redis\u4e8b\u52a1\u53ef\u4ee5\u4fdd\u8bc1\u53ea\u6709\u5728\u6267\u884c\u73a9\u73a9\u4e8b\u52a1\u4e2d\u7684\u6240\u6709\u547d\u4ee4\u540e\uff0c\u624d\u4f1a\u7ee7\u7eed\u5904\u7406\u6b64\u5ba2\u6237\u7aef\u7684\u5176\u4ed6\u547d\u4ee4\u3002<\/p>\n\n\n\n<p>\u4e5f\u5c31\u662f\u8bf4\u53ea\u6709\u4e00\u4e2a\u7528\u6237\u53ef\u4ee5\u64cd\u4f5c\u4e8b\u52a1\u5f53\u4e2d\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<p><strong>redis\u4e2d\u4e8b\u52a1\u4ece\u5f00\u59cb\u5230\u7ed3\u675f\u7ecf\u5386\u4e09\u4e2a\u9636\u6bb5\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/style.youkeda.com\/img\/course\/d2\/guocheng.svg\" alt=\"\"\/><\/figure>\n\n\n\n<p>redis\u4e8b\u52a1\u5b58\u5728\u56db\u4e2a\u6307\u4ee4\uff1a<strong>multi\u3001exec\u3001discard\u3001watch<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>multi <\/strong>\u5f00\u542f\u4e00\u4e2a\u4e8b\u52a1<\/li><li><strong>exec <\/strong>\u6267\u884c\u4e00\u4e2a\u4e8b\u52a1<\/li><li><strong>discard<\/strong> \u53d6\u6d88\u4e00\u4e2a\u4e8b\u52a1<\/li><li><strong>watch<\/strong> \u7528\u4e8e\u5e76\u53d1\u60c5\u51b5\u4e0b\uff0c\u4e3a\u4e8b\u52a1\u63d0\u4f9b\u4e00\u4e2a\u9501\uff0c\u4f7f\u7528watch\u76d1\u63a7\u53d8\u91cf\u5982\u679c\u5728\u6267\u884c\u4e8b\u52a1\u4e4b\u524d\uff0c\u76d1\u63a7\u9879\u88ab\u4fee\u6539\u4e86\uff0c\u90a3\u4e48\u6574\u4e2a\u4e8b\u52a1\u88ab\u4e2d\u6b62\u3002\uff08watch\u5fc5\u987b\u5199\u5728\u4e8b\u52a1\u524d\u9762\uff0c\u800c\u4e0d\u662f\u5f53\u4e2d\uff09<\/li><\/ul>\n\n\n\n<p><code>redisTemplate.execute()<\/code>\u662f\u6267\u884c\u5668\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6267\u884c\u4e00\u7cfb\u5217\u64cd\u4f5c<\/p>\n\n\n\n<p>\u4f7f\u7528\u65f6\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">redisTemplate.execute(<strong>new<\/strong> SessionCallback&lt;List&lt;Object&gt;&gt;() {\n            <strong>@Override<\/strong>\n            <strong>public<\/strong> List&lt;Object&gt; <strong>execute<\/strong>(RedisOperations operations) <strong>throws<\/strong> DataAccessException {\n            }\n        });<\/code><\/pre>\n\n\n\n<div style=\"height:18px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u5f53\u6211\u4eec\u9700\u8981\u76d1\u542c\u5bf9\u8c61\u65f6\uff0c\u4f7f\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">redisTemplate.execute(<strong>new<\/strong> SessionCallback&lt;List&lt;Object&gt;&gt;() {\n    <strong>@Override<\/strong>\n    <strong>public<\/strong> List&lt;Object&gt; <strong>execute<\/strong>(RedisOperations operations) <strong>throws<\/strong> DataAccessException {\n        <em>\/\/\u76d1\u542c\u5546\u54c1\u7684ID<\/em>\n        operations.watch(id);\n    }\n});<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2dwatch()\u5185\u4f20\u5165\u5f85\u76d1\u89c6\u7684Redis\u6570\u636e\u7684Key\u3002<\/p>\n\n\n\n<div style=\"height:17px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">\u4e8b\u52a1\u4e09\u9636\u6bb5\uff1a<\/p>\n\n\n\n<p style=\"font-size:25px\"><strong>1.\u5f00\u542f\u4e8b\u52a1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">redisTemplate.execute(<strong>new<\/strong> SessionCallback&lt;List&lt;Object>>() {\n    <strong>@Override<\/strong>\n    <strong>public<\/strong> List&lt;Object> <strong>execute<\/strong>(RedisOperations operations) <strong>throws<\/strong> DataAccessException {\n        <em>\/\/\u76d1\u542c\u5546\u54c1\u7684ID<\/em>\n        operations.watch(id);\n\n        <em>\/\/\u5f00\u542f\u4e8b\u52a1<\/em>\n        operations.multi();\/\/\u5f00\u542f\u4e8b\u52a1\u65f6\u4e0d\u9700\u8981\u53c2\u6570\n    }\n});<\/code><\/pre>\n\n\n\n<p style=\"font-size:25px\"><strong>2.\u547d\u4ee4\u5165\u5217:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">redisTemplate.execute(<strong>new<\/strong> SessionCallback&lt;List&lt;Object&gt;&gt;() {\n    <strong>@Override<\/strong>\n    <strong>public<\/strong> List&lt;Object&gt; <strong>execute<\/strong>(RedisOperations operations) <strong>throws<\/strong> DataAccessException {\n        <em>\/\/\u76d1\u542c\u5546\u54c1\u7684ID<\/em>\n        operations.watch(id);\n\n        <em>\/\/\u5f00\u542f\u4e8b\u52a1<\/em>\n        operations.multi();\n\n        <em>\/\/ \u63d2\u5165\u4e00\u6761\u8ba2\u5355\u6570\u636e\u3002<\/em>\n        <em>\/\/ \u7f13\u5b58\u5e93\u7684\u5b58\u51cf 1<\/em>\n        operations.opsForValue().set(idKey, (stock - 1));\n        <em>\/\/ \u6570\u636e\u5e93\u7684\u5e93\u5b58\u51cf 1<\/em>\n        productDAO.reduceStock(id, 1);\n    }\n});<\/code><\/pre>\n\n\n\n<p class=\"has-luminous-vivid-orange-color has-text-color\">\u5728<code>execute()<\/code>\u00a0\u65b9\u6cd5\u4e2d\uff0cRedis \u7684\u64cd\u4f5c\u4e0d\u518d\u4f7f\u7528\u00a0<code>redisTemplate.opsForValue()<\/code>\uff0c\u800c\u662f\u4f7f\u7528\u00a0<code>operations.opsForValue()<\/code>\uff0c\u8fd9\u6837\u7cfb\u7edf\u624d\u77e5\u9053\u662f\u540c\u4e00\u4e2a\u4e8b\u52a1\u4e2d\u7684\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p style=\"font-size:25px\"><strong>3.\u6267\u884c\u4e8b\u52a1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">redisTemplate.execute(<strong>new<\/strong> SessionCallback&lt;List&lt;Object&gt;&gt;() {\n    <strong>@Override<\/strong>\n    <strong>public<\/strong> List&lt;Object&gt; <strong>execute<\/strong>(RedisOperations operations) <strong>throws<\/strong> DataAccessException {\n        <em>\/\/\u76d1\u542c\u5546\u54c1\u7684ID<\/em>\n        operations.watch(id);\n\n        <em>\/\/\u5f00\u542f\u4e8b\u52a1<\/em>\n        operations.multi();\n\n        <em>\/\/ \u63d2\u5165\u4e00\u6761\u8ba2\u5355\u6570\u636e\u3002<\/em>\n        <em>\/\/ \u7f13\u5b58\u5e93\u7684\u5b58\u51cf 1<\/em>\n        <em>\/\/ \u6570\u636e\u5e93\u7684\u5e93\u5b58\u51cf 1<\/em>\n\n        <em>\/\/ \u6267\u884c\u4e8b\u52a1<\/em>\n        List exec = operations.exec();\n    }\n});<\/code><\/pre>\n\n\n\n<p><code>operations.exec()<\/code>&nbsp;\u7528\u4e8e\u6267\u884c\u4e8b\u52a1\uff0c\u8fd4\u56de\u503c\u662f List \u5217\u8868\uff0c\u5b58\u653e\u4e86\u6bcf\u4e2a\u4e8b\u52a1\u6267\u884c\u7ed3\u679c\u7684\u6807\u8bb0\u3002\u4e8b\u52a1\u5f00\u542f\u540e\u6267\u884c\u7684\u6bcf\u4e2a\u64cd\u4f5c\uff0c\u5982\u679c\u6210\u529f\u5219\u653e\u5165 true \u503c\u4f5c\u4e3a\u6807\u8bb0\uff0c\u64cd\u4f5c\u5931\u8d25\u5219\u4e0d\u653e\u5165\u7ed3\u679c\u6807\u8bb0\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u6709\u51e0\u4e2a\u64cd\u4f5c\u5c31\u6709\u51e0\u4e2a\u7ed3\u679c\u6807\u8bb0\u3002\u56e0\u4e3a\u672c\u6f14\u793a\u6848\u4f8b\uff0cRedis \u53ea\u6709\u4e00\u4e2a\u8bbe\u7f6e\u5e93\u5b58\u7684\u64cd\u4f5c\uff0c\u6240\u4ee5\u53ea\u6709\u4e00\u4e2a\u6807\u8bb0\u3002<\/p><\/blockquote>\n\n\n\n<p>\u56e0\u4e3a\u4e8b\u52a1\u662f\u8981\u4e48\u6bcf\u4e2a\u64cd\u4f5c\u90fd\u6210\u529f\uff0c\u8981\u4e48\u90fd\u5931\u8d25\uff0c\u6240\u4ee5\u4e00\u822c\u6765\u8bf4\u53ef\u4ee5\u7b80\u5355\u5904\u7406\uff0c\u4e0d\u7528\u5224\u65ad operations.exec() \u65b9\u6cd5\u8fd4\u56de\u503c\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u90fd\u4e3a true\uff0c\u53ea\u8981\u5224\u65ad\u8fd4\u56de\u503c\u5217\u8868\u957f\u5ea6\u5927\u4e8e 0 \u5219\u8868\u793a\u6267\u884c\u6210\u529f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-\u53d6\u6d88\u4e8b\u52a1\" style=\"font-size:25px\">4. \u53d6\u6d88\u4e8b\u52a1<\/h3>\n\n\n\n<p>\u5f53execute\u51fa\u73b0\u5f02\u5e38\u65f6\u4f1a\u81ea\u52a8\u53d6\u6d88\u4e8b\u52a1\uff0c\u5f53\u6211\u4eec\u9700\u8981\u624b\u52a8\u53d6\u6d88\u65f6\u4f7f\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">operations.discard();<\/code><\/pre>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662fexec()\u548cdiscard()\u662f\u4e92\u65a5\u7684\u3002<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">\u4e8b\u52a1\u4f7f\u7528\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<p>\u4f8b\u5b50\u4e0a\u90e8\u5206\u4e3a\u5224\u65ad\u6570\u636e\u5341\u5206\u9700\u8981\u5b58\u5165\u7f13\u5b58\uff08\u6570\u636e\u5e93\uff09<\/p>\n\n\n\n<p>\u4e0b\u90e8\u5206\u4e3a\u4e8b\u52a1\u7684\u6267\u884c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\"><strong>public<\/strong> Result&lt;Boolean&gt; <strong>snappedUp<\/strong>(<strong>@RequestParam(\"id\")<\/strong> Long id) {\n        <em>\/\/\u521d\u59cb\u5316\u8fd4\u56de\u6570\u636e<\/em>\n        Result result = <strong>new<\/strong> Result();\n        result.data(<strong>true<\/strong>);\n        result.setSuccess(<strong>true<\/strong>);\n        <em>\/\/\u5b9e\u73b0\u8d2d\u4e70\u4ee3\u7801<\/em>\n        <em>\/\/\u5148\u53bbredis\u67e5\u8be2\u4e00\u4e0b<\/em>\n        Object value = redisTemplate.opsForValue().get(id);\n        <strong>int<\/strong> stock = 0;\n        <em>\/\/\u5982\u679credis\u6ca1\u6709\u5219\u53bb\u6570\u636e\u5e93\u67e5\u8be2<\/em>\n        <strong>if<\/strong> (value == <strong>null<\/strong>) {\n            <em>\/\/\u53bb\u6570\u636e\u5e93\u67e5\u8be2\u8be5\u5546\u54c1\u7684\u4fe1\u606f<\/em>\n            ProductDO product = productDAO.selectById(id);\n            <em>\/\/\u5c06\u4fe1\u606f\u7f13\u5b58\u5230redis\u91cc\u8fb9<\/em>\n            stock = product.getStock();\n            redisTemplate.opsForValue().set(product.getId(), stock);\n        } <strong>else<\/strong> {\n            stock = (<strong>int<\/strong>)value;\n        }\n\n        redisTemplate.execute(<strong>new<\/strong> SessionCallback&lt;List&lt;Object&gt;&gt;() {\n            <strong>@Override<\/strong>\n            <strong>public<\/strong> List&lt;Object&gt; <strong>execute<\/strong>(RedisOperations operations) <strong>throws<\/strong> DataAccessException {\n                Integer stock = (Integer)operations.opsForValue().get(id);\n                <em>\/\/\u5224\u65ad\u8be5\u5546\u54c1\u7684\u5e93\u5b58\u662f\u5426\u5927\u4e8e1<\/em>\n                <strong>if<\/strong> (Integer.valueOf(stock) &gt;= 1) {\n                    <em>\/\/\u76d1\u542c\u5546\u54c1\u7684\u540d\u5b57\uff0credis\u91cc\u8fb9\u7684key<\/em>\n                    operations.watch(id);\n\n                    <em>\/\/\u5f00\u542f\u4e8b\u52a1<\/em>\n                    operations.multi();\n\n                    <em>\/\/\u5c06\u8be5\u5546\u54c1\u7684\u5e93\u5b58\u81ea\u51cf1<\/em>\n                    operations.opsForValue().set(id, stock - 1);\n                    <em>\/\/\u4fee\u6539mysql\u6570\u636e\u5e93\u5e93\u5b58\u6570\u91cf<\/em>\n                    ProductDO productDO = <strong>new<\/strong> ProductDO();\n                    productDO.setId(id);\n                    productDO.setStock(stock - 1);\n                    productDAO.updateStock(productDO);\n                    <em>\/\/ \u6267\u884c\u4e8b\u52a1<\/em>\n                    List exec = operations.exec();\n                    <strong>if<\/strong> (exec.size() &gt; 0) {\n                        <em>\/\/ TODO:\u53ef\u4ee5\u6709\u5176\u5b83\u4e1a\u52a1\u903b\u8f91\uff0c\u4f8b\u5982\u63d2\u5165\u8ba2\u5355\u7b49\uff0c\u89c6\u5177\u4f53\u9700\u6c42\u800c\u5b9a<\/em>\n                        result.setMessage(\"\u62a2\u8d2d\u6210\u529f\");\n                        result.setData(<strong>true<\/strong>);\n                    } <strong>else<\/strong> {\n                        result.setMessage(\"\u62a2\u8d2d\u5931\u8d25\");\n                        result.setData(<strong>false<\/strong>);\n                    }\n\n                    <strong>return<\/strong> exec;\n                } <strong>else<\/strong> {\n                    result.setMessage(\"\u5546\u54c1\u5e93\u5b58\u4e0d\u8db3\");\n                    result.setData(<strong>false<\/strong>);\n\n                    <strong>return<\/strong> <strong>null<\/strong>;\n                }\n            }\n        });\n\n        <strong>return<\/strong> result;\n    }<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u4f60\u60f3\u505a\u4e00\u4e2a\u62a2\u8d2d\u7a0b\u5e8f\uff0c\u5229\u7528\u539f\u59cb\u7684if\u8bed\u53e5\u6765\u5b9e\u73b0\uff0c\u4f1a\u53d1\u73b0\u663e\u793a\u62a2\u8d2d\u6210\u529f\u7684\u6570\u91cf\u5927\u4e8e\u9884\u5b9a\u503c\u3002\u8fd9\u662f\u7531\u4e8e\u62a2\u8d2d\u672c\u8eab\u662f\u4e00\u4e2a\u5e76\u53d1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[6,1],"tags":[],"class_list":["post-1098","post","type-post","status-publish","format-standard","hentry","category-6","category-wcd"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1098"}],"collection":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/comments?post=1098"}],"version-history":[{"count":6,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1098\/revisions"}],"predecessor-version":[{"id":1166,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1098\/revisions\/1166"}],"wp:attachment":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/media?parent=1098"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/categories?post=1098"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/tags?post=1098"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}