{"id":900,"date":"2020-05-18T09:38:55","date_gmt":"2020-05-18T01:38:55","guid":{"rendered":"http:\/\/toradora.xyz\/?p=900"},"modified":"2020-05-22T15:29:27","modified_gmt":"2020-05-22T07:29:27","slug":"redis%e5%90%8c%e6%ad%a5mysql%e5%ae%9e%e7%8e%b0%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"https:\/\/diuut.com\/?p=900","title":{"rendered":"Redis\u540c\u6b65mysql\u5b9e\u73b0\u89e3\u51b3\u65b9\u6848"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote\"><p>\u9700\u6c42<\/p><cite><strong>redis\u4e2d\u6570\u636e\u540c\u6b65\u5230mysql\u4e2d\u6570\u636e\uff0c\u5982\u679c\u5728\u66f4\u65b0\u9014\u4e2dredis\u53c8\u66f4\u65b0\u4e86\uff0c\u6309\u7167Redis\u4e2d\u6700\u65b0\u7684\u6570\u636e\u8fdb\u884c\u66f4\u65b0\u3002<\/strong> <\/cite><\/blockquote>\n\n\n\n<p>\u5b9e\u73b0\u601d\u8def\uff1a\u5b58\u50a8redis\u6570\u636e\u7528RedisTemplate.opsForValue\u8fdb\u884c\u6570\u636e\u5b58\u50a8\uff0c\u5728\u6570\u636e\u53d1\u751f\u6539\u53d8\u7684\u65f6\u5019\uff0c\u4f18\u5148\u5411redis\u4e2d\u66f4\u65b0\u6570\u636e\uff0c\u7136\u540e\u5148\u4e00\u4e2aset\u96c6\u5408\u4e2dadd\u65b0\u7684\u6570\u636e\u7684key\u503c\u3002\u4e1a\u52a1\u7aef\u7684\u64cd\u4f5c\u5c31\u7ed3\u675f\u4e86\uff0c\u63a5\u4e0b\u6765\u901a\u8fc7\u4e00\u4e2a\u4e13\u95e8\u7684\u5b9a\u65f6\u4efb\u52a1\u670d\u52a1\uff0c\u901a\u8fc7\u8bbe\u7f6e@Scheduled(fixedDelay = 500)\u8bbe\u7f6e\u4e00\u4e2a\u4efb\u52a1\u4e13\u95e8\u4ece\u4e0a\u9762\u7684\u5b58\u653ekey\u503c\u7684set\u4e2dpop\u51fakey\u503c\uff0c\u7136\u540e\u4eceredis\u4e2d\u67e5\u8be2\u51fa\u6765\uff0c\u518d\u901a\u8fc7\u8fd9\u4e2akey\u503c\u66f4\u65b0\u5230\u5bf9\u5e94\u7684mysql\u6570\u636e\u5e93\u4e2d\uff0c\u8fd9\u4e2a\u4efb\u52a1\u4ece\u542f\u52a8\u5c31\u5f00\u59cb\u6267\u884c\uff0c\u6267\u884c\u7ed3\u675f\u540e\u7b49\u5f85fixedDelay\u540e\u8bbe\u7f6e\u7684\u6beb\u79d2\u65f6\u95f4\uff0c\u53c8\u63a5\u7740\u6267\u884c\u4e0b\u4e00\u6b21\u8be5\u4efb\u52a1\u3002\u4ee5\u6b64\u5f80\u590d\uff0c\u5c31\u4f1a\u5c06\u524d\u53f0\u53d1\u9001\u81f3set\u4e2d\u7684key\u5bf9\u5e94\u7684redis\u6570\u636e\u66f4\u65b0\u81f3mysql\u4e2d\uff0c\u4ee5\u5b9e\u73b0\u6570\u636e\u540c\u6b65\u3002<\/p>\n\n\n\n<h4>\u5b9e\u73b0\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n\/**\n     * \u66f4\u65b0Redis\u4e2dUser\u4fe1\u606f\n     * @param userUid \u7528\u6237uid\n     * @param user  User\u5bf9\u8c61\n     *\/\n    public void saveUser(int userUid,User user){\n        try {\n            redisLockUtil.lock(&quot;userLock-uid:&quot; + userUid);\n            log.info(&quot;userLock-uid: &quot; + userUid + &quot;\u4e0a\u9501&quot;);\n            String userData = JSONObject.toJSONString(user);\n            redisTemplate.opsForValue().set(&quot;user-uid:&quot; + userUid, userData, 7, TimeUnit.DAYS);\n            redisTemplate.opsForSet().add(&quot;redis-update-queue&quot;, &quot;user-uid:&quot;+userUid);\n            \/\/\u5411\u66f4\u65b0\u5217\u8868set\u4e2d\u53d1\u9001\u9700\u8981\u66f4\u65b0\u7684\u6570\u636e\u7684key\n        } catch (Exception e) {\n            log.error(e.getMessage(), e);\n        } finally {\n            redisLockUtil.unlock(&quot;userLock-uid:&quot; + userUid);\n            log.info(&quot;userLock-uid: &quot; + 10135 + &quot;\u89e3\u9501&quot;);\n        }\n    }\n<\/pre><\/code><\/pre>\n\n\n\n<p>\u5728\u5b9a\u65f6\u4efb\u52a1\u4e2d\u8fd8\u53ef\u4ee5\u91c7\u7528\u81ea\u5b9a\u4e49\u7ebf\u7a0b\u6c60\u7684\u65b9\u5f0f\u624b\u52a8\u8bbe\u7f6e\u7ebf\u7a0b\u6570\u91cf\u7b49\u53c2\u6570\uff0c\u4ee5\u8fbe\u5230\u6700\u4f18\u7684\u66f4\u65b0mysql\u7684\u6548\u7387\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@EnableAsync\n@Configuration\n@Slf4j\npublic class UserDataUpdateQueue {\n    @Autowired\n    private RedisTemplate redisTemplate;\n    @Autowired\n    private UserDao userDao;\n\n    @Async(&quot;taskExecutor&quot;)\n    @Scheduled(fixedDelay = 500)\n    public void getUserUpdateQueue() {\n        log.info(&quot;------getUserUpdateQueue------&quot;);\n        while (redisTemplate.opsForSet().size(&quot;xyddz-redis-update-queue&quot;) &gt; 0) {\n            List&lt;Object&gt; pop = redisTemplate.opsForSet().pop(&quot;xyddz-redis-update-queue&quot;, 10);\n            for (Object userData : pop) {\n                String userStr = (String) redisTemplate.opsForValue().get(userData.toString());\n                if (!StringUtils.isEmpty(userStr)) {\n                    User user = JSONObject.parseObject(userStr, User.class);\n                    userDao.save(user);\n                    log.info(&quot;\u66f4\u65b0User-Uid:&quot; + user.getUid());\n                }\n            }\n        }\n    }\n}\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42 redis\u4e2d\u6570\u636e\u540c\u6b65\u5230mysql\u4e2d\u6570\u636e\uff0c\u5982\u679c\u5728\u66f4\u65b0\u9014\u4e2dredis\u53c8\u66f4\u65b0\u4e86\uff0c\u6309\u7167Redis\u4e2d\u6700\u65b0\u7684\u6570\u636e\u8fdb\u884c<span class=\"more-button\"><a href=\"https:\/\/diuut.com\/?p=900\" class=\"more-link\">view all . . .<span class=\"screen-reader-text\">Redis\u540c\u6b65mysql\u5b9e\u73b0\u89e3\u51b3\u65b9\u6848<\/span><\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":688,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[9,10],"_links":{"self":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/900"}],"collection":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=900"}],"version-history":[{"count":3,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/900\/revisions"}],"predecessor-version":[{"id":977,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/900\/revisions\/977"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/media\/688"}],"wp:attachment":[{"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}