{"id":201360,"date":"2025-05-10T08:30:22","date_gmt":"2025-05-10T00:30:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201360\/"},"modified":"2025-05-10T08:30:22","modified_gmt":"2025-05-10T00:30:22","slug":"%e4%bd%bf%e7%94%a8redis%e5%ae%9e%e7%8e%b0%e7%82%b9%e8%b5%9e%e5%8f%96%e6%b6%88%e7%82%b9%e8%b5%9e%e7%9a%84%e8%af%a6%e7%bb%86%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201360\/","title":{"rendered":"\u4f7f\u7528Redis\u5b9e\u73b0\u70b9\u8d5e\u53d6\u6d88\u70b9\u8d5e\u7684\u8be6\u7ec6\u4ee3\u7801"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u4f7f\u7528Redis\u5b9e\u73b0\u70b9\u8d5e\u53d6\u6d88\u70b9\u8d5e\u7684\u8be6\u7ec6\u4ee3\u7801<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300a\u4f7f\u7528Redis\u5b9e\u73b0\u70b9\u8d5e\u53d6\u6d88\u70b9\u8d5e\u7684\u8be6\u7ec6\u4ee3\u7801\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<h2>\u4ee3\u7801\u5b9e\u73b0:<\/h2>\n<pre>\/**\n     *\n     * @param userId \u70b9\u8d5e\u7684\u4eba\n     * @param type \u70b9\u8d5e\u4e0e\u53d6\u6d88\u70b9\u8d5e\u7684\u8868\u793a\n     * @param textId   \u6587\u7ae0ID\n     * @param entityUserId -- \u88ab\u70b9\u8d5e\u7684\u4eba\uff0c\u6587\u7ae0\u4f5c\u8005\n     *\/\n    private void like(long userId,int type,int textId,long entityUserId){\n        redisTemplate.execute(new SessionCallback() {\n            @Override\n            public Object execute(RedisOperations operations) throws DataAccessException {\n                String entityLikeKey = RedisKeyUtil.getEntityLikeKey(type, textId);\n                String userLikeKey = RedisKeyUtil.getUserLikeKey(entityUserId);\n                boolean isMember = redisTemplate.opsForSet().isMember(entityLikeKey, userId);\n                \/\/\u591a\u4e2a\u66f4\u65b0\u64cd\u4f5c\uff0c\u9700\u8981\u4e8b\u52a1\n                operations.multi();\n                if (isMember) {\n                    \/\/\u53d6\u6d88\u8d5e\n                    redisTemplate.opsForSet().remove(entityLikeKey, userId);\n                    redisTemplate.opsForValue().decrement(userLikeKey);\n                } else {\n                    \/\/\u70b9\u8d5e\n                    redisTemplate.opsForSet().add(entityLikeKey, userId);\n                    redisTemplate.opsForValue().increment(userLikeKey);\n                }\n                return operations.exec();\n            }\n        });\n\n    }\n\n    \/**\n     *\u67e5\u8be2\u67d0\u5b9e\u4f53\uff08\u5e16\u5b50\uff0c\u8bc4\u8bba\u7b49\uff09\u70b9\u8d5e\u6570\u91cf\n     * @param type 1\u70b9\u8d5e\uff0c2\u8bc4\u8bba\u30020\u8868\u793a\u53d6\u6d88\u70b9\u8d5e\n     * @param textId\n     * @return\n     *\/\n    private long findEntityLikeCount(int type, int textId){\n        String entityLikeKey = RedisKeyUtil.getEntityLikeKey(type, textId);\n        return redisTemplate.opsForSet().size(entityLikeKey);\n    }\n\n    \/**\n     * \u67e5\u8be2\u67d0\u4eba\u5bf9\u67d0\u6587\u7ae0\u7684\u70b9\u8d5e\u72b6\u6001\n     * @param textId \u5e16\u5b50ID\n     * @param userId\n     * @return\n     *\/\n    private int findEntityLikeStatus(int textId,long userId){\n        String entityLikeKey = RedisKeyUtil.getEntityLikeKey(1, textId);\n        \/\/\u6b64\u5904\u8fd4\u56deint\uff0c\u662f\u4e3a\u4e86\u8fdb\u884c\u6269\u5c55\u3002\u6bd4\u5982\u6269\u5c55\u8e29\uff0c\u4e3a\u6b622.\u7b49\u7b49\u60c5\u51b5\n        return redisTemplate.opsForSet().isMember(entityLikeKey,userId)?1:0;\n    }\n\n    \/**\n     * \u67e5\u8be2\u67d0\u4e2a\u7528\u6237\u83b7\u5f97\u8d5e\uff0c\u7528\u4e8e\u5728\u4e2a\u4eba\u4e3b\u9875\u67e5\u770b\u6536\u83b7\u4e86\u591a\u5c11\u8d5e\n     * @param userId\n     * @return\n     *\/\n    private int findUserLikeCount(long userId){\n        String userLikeKey = RedisKeyUtil.getUserLikeKey(userId);\n        Integer count = (Integer) redisTemplate.opsForValue().get(userLikeKey);\n        \/\/ count.intValue()\u6570\u636e\u7684\u6574\u6570\u5f62\u5f0f;\n        return count==null?0:count.intValue();\n    }\n\n<\/pre>\n<p>Redis\u2013key\u8bbe\u7f6e<\/p>\n<pre>public class RedisKeyUtil {\n    private static final String SPLIT = \":\";\n    private static final String PREFIX_ENTITY_LIKE = \"like:entity\";\n    private static final String PREFIX_USER_LIKE = \"like:user\";\n    private static final String PREFIX_USER_COMMENTS=\"comments:user\";\n    \/**\n     *\u67d0\u4e2a\u5b9e\u4f53\u6536\u5230\u7684\u8d5e\uff0c\u5982\u5e16\u5b50\uff0c\n     * like:entity:entityType:entityId -&gt; set(userId) \u5bf9\u5e94set\uff0c\u5b58\u5165userId\n     * @param entityType\n     * @param entityId\n     * @return\n     *\/\n    public static String getEntityLikeKey(int entityType, int entityId) {\n        return PREFIX_ENTITY_LIKE + entityType + SPLIT + entityId;\n    }\n     *\u67d0\u4e2a\u7528\u6237\u6536\u5230\u7684\u603b\u8d5e\u6570\n     * like:user:userId -&gt;long\n     * @param userId\n    public static String getUserLikeKey(long userId) {\n        return PREFIX_USER_LIKE + SPLIT + userId;\n     * \u6c47\u603b\u67d0\u4e2a\u5e16\u5b50\u7684\u8bc4\u8bba\u6570\u91cf\n    public static String getUserCommentsKey(int articleId) {\n        return PREFIX_USER_COMMENTS + SPLIT + articleId;\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Redis\u5b9e\u73b0\u70b9\u8d5e\u53d6\u6d88\u70b9\u8d5e\u7684&#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-201360","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201360","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=201360"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201360\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}