{"id":201222,"date":"2025-05-10T08:14:07","date_gmt":"2025-05-10T00:14:07","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201222\/"},"modified":"2025-05-10T08:14:07","modified_gmt":"2025-05-10T00:14:07","slug":"%e5%9f%ba%e4%ba%8e-spring-aop-%e7%8e%af%e7%bb%95%e9%80%9a%e7%9f%a5%e5%ae%9e%e7%8e%b0-redis-%e7%bc%93%e5%ad%98%e5%8f%8c%e5%88%a0%e5%8a%9f%e8%83%bd%e7%a4%ba%e4%be%8b%e4%bb%a3","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201222\/","title":{"rendered":"\u57fa\u4e8e\u00a0Spring\u00a0Aop\u00a0\u73af\u7ed5\u901a\u77e5\u5b9e\u73b0\u00a0Redis\u00a0\u7f13\u5b58\u53cc\u5220\u529f\u80fd(\u793a\u4f8b\u4ee3\u7801)"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u57fa\u4e8e&nbsp;Spring&nbsp;Aop&nbsp;\u73af\u7ed5\u901a\u77e5\u5b9e\u73b0&nbsp;Redis&nbsp;\u7f13\u5b58\u53cc\u5220\u529f\u80fd(\u793a\u4f8b\u4ee3\u7801)<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762golang\u5b66\u4e60\u7f51\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6570\u636e\u5e93\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300a\u57fa\u4e8e&nbsp;Spring&nbsp;Aop&nbsp;\u73af\u7ed5\u901a\u77e5\u5b9e\u73b0&nbsp;Redis&nbsp;\u7f13\u5b58\u53cc\u5220\u529f\u80fd(\u793a\u4f8b\u4ee3\u7801)\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<p>\u57fa\u4e8e spring aop \u5e38\u89c4\u5e94\u7528\u573a\u666f\u591a\u662f\u7528\u4e8e\u65e5\u5fd7\u8bb0\u5f55\u4ee5\u53ca\u5b9e\u73b0 redis \u5206\u5e03\u5f0f\u9501\uff0c\u5728 github \u4e2d\u4e5f\u6709\u9879\u76ee\u662f\u628a\u5b83\u62ff\u6765\u5f53\u4f5c\u7f13\u5b58\u7684\u5f02\u5e38\u6355\u6349\u3002\u4ece\u800c\u907f\u514d\u5f71\u54cd\u5b9e\u9645\u4e1a\u52a1\u7684\u5f00\u53d1\uff1b\u5728\u67d0\u5929\uff0c\u7b14\u8005\u6709\u4e2a\u4e1a\u52a1\u5f00\u53d1\u662f\u7ed9\u67d0\u4e2a\u670d\u52a1\u6a21\u5757\u589e\u52a0 redis \u7f13\u5b58\u3002\u589e\u52a0\u7f13\u5b58\u5c31\u4f1a\u6d89\u53ca redis \u5220\u9664\u3002\u6240\u4ee5\u7b14\u8005\u5c31\u5728\u601d\u8003\u662f\u4e0d\u662f\u53ef\u4ee5\u7528\u73af\u7ed5\u901a\u77e5\u7684\u65b9\u5f0f\u6765\u8fdb\u884c\u5b9e\u73b0<\/p>\n<h2>\u4ee3\u7801\u5b9e\u73b0<\/h2>\n<p>\u7ed3\u6784\u793a\u610f\u56fe\uff1a<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" style=\"max-width:100%\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167247904663b00146e9341.png\" class=\"aligncenter\"><\/p>\n<p>\u81ea\u5b9a\u4e49\u6ce8\u89e3 RedisDelByDbUpdate<\/p>\n<p>@Repeatable \u8868\u793a\u5141\u8bb8\u5728\u540c\u4e00\u4e2a\u5730\u65b9\u4e0a\u4f7f\u7528\u76f8\u540c\u7684\u6ce8\u89e3\uff0c\u6ca1\u6709\u8be5\u6ce8\u89e3\u65f6\u8d34\u76f8\u540c\u6ce8\u89e3\u4f1a\u62a5\u9519<\/p>\n<pre>@Target(ElementType.METHOD)\n@Retention(RetentionPolicy.RUNTIME)\n@Documented\n@Repeatable(RedisDelByDbUpdateList.class)\npublic @interface RedisDelByDbUpdate {\n    \/**\n     * \u5177\u4f53\u64cd\u4f5c\u5f97\u7f13\u5b58\u524d\u7f00\n     *\/\n    String redisPrefix() default \"\";\n \n    \/**\n     * \u5177\u4f53\u7684\u7f13\u5b58\u540d\u79f0\uff0c\u4e3a\u7a7a\u5219\u5220\u9664 redisPrefix \u4e0b\u7684\u6240\u6709\u7f13\u5b58\n     *\/\n    String fieldName() default \"\";\n}<\/pre>\n<p>\u81ea\u5b9a\u4e49\u6ce8\u89e3 RedisDelByUpdateList<\/p>\n<pre>@Target(ElementType.METHOD)\n@Retention(RetentionPolicy.RUNTIME)\n@Documented\npublic @interface RedisDelByDbUpdateList {\n    \n    RedisDelByDbUpdate[] value();\n}<\/pre>\n<p>\u5177\u4f53\u5b9e\u73b0 redis \u53cc\u5220\u903b\u8f91<\/p>\n<pre>@Aspect\n@Component\n@Slf4j\npublic class RedisDelAspect_bak {\n    \/\/\u73af\u7ed5\u589e\u5f3a\n    @Autowired\n    private RedisUtil redis;\n \n    @Pointcut(\"@annotation(com.cili.baseserver.aop.annotation.RedisDelByDbUpdate) \" +\n            \"|| @annotation(com.cili.baseserver.aop.annotation.RedisDelByDbUpdateList)\")\n    public void pointCut() {\n \n    }\n \n    \/\/ \u7ebf\u7a0b\u6c60\u5b9a\u957f\u8bbe\u7f6e\uff1a\u6700\u4f73\u7ebf\u7a0b\u6570\u76ee = ((\u7ebf\u7a0b\u7b49\u5f85\u65f6\u95f4+\u7ebf\u7a0bCPU\u65f6\u95f4)\/\u7ebf\u7a0bCPU\u65f6\u95f4 )* CPU\u6570\u76ee\n    ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(26);\n \n    \/**\n     * \u73af\u7ed5\u589e\u5f3a\n     *\n     * @param point\n     * @return\n     * @throws Throwable\n     *\/\n    @Around(\"pointCut()\")\n    public Object around(ProceedingJoinPoint point) throws Throwable {\n        MethodSignature signature = (MethodSignature) point.getSignature();\n        Method method = signature.getMethod();\n \n        RedisDelByDbUpdate redisDelByDbUpdate = method.getAnnotation(RedisDelByDbUpdate.class);\n        if (redisDelByDbUpdate != null) {\n            return singleRedisDel(redisDelByDbUpdate, point);\n        }\n \n        RedisDelByDbUpdateList updateList = method.getAnnotation(RedisDelByDbUpdateList.class);\n        return arraysRedisDel(updateList, point);\n    }\n \n    private Object singleRedisDel(RedisDelByDbUpdate redisDelByDbUpdate, ProceedingJoinPoint point) throws Throwable {\n        return handle(redisDelByDbUpdate, point, new IObjectCallback() {\n            public \n    \n      T callback() throws Throwable { return (T) point.proceed(); } }); } private Object arraysRedisDel(RedisDelByDbUpdateList updateList, ProceedingJoinPoint point) throws Throwable { RedisDelByDbUpdate[] redisDelByDbUpdates = updateList.value(); for (int i = 0; i () { public \n     \n       T callback() throws Throwable { return flag ? (T) point.proceed() : null; } }); if (flag) return obj; } return null; } private Object handle(RedisDelByDbUpdate redisDelByDbUpdate, ProceedingJoinPoint point, IObjectCallback\n       object) throws Throwable { String prefix = redisDelByDbUpdate.redisPrefix(); String fieldName = redisDelByDbUpdate.fieldName(); if (ValueUtil.isEmpty(prefix)) { log.info(\"redis\u7f13\u5b58\u524d\u7f00\u4e0d\u80fd\u4e3a\u7a7a\"); throw new BizException(BaseResponseCode.SYSTEM_BUSY); } Object arg = point.getArgs()[0]; String key = \"\"; String[] redisKeys = null; if (ValueUtil.isNotEmpty(fieldName)) { if (arg instanceof ArrayList) { redisKeys = ((ArrayList&gt;) arg).stream().map(item -&gt; prefix + item).toArray(String[]::new); } else { Map\n       \n         map = (Map\n        \n         ) JsonUtil.toMap(JsonUtil.toJSON(point.getArgs()[0])); key = map.get(fieldName).toString(); } } else { \/\/ \u83b7\u53d6\u6240\u6709\u8be5\u524d\u7f00\u4e0b\u7f13\u5b58 Set\n         \n           keys = redis.keys(prefix + \"*\"); redisKeys = keys.stream().toArray(String[]::new); } \/\/ \u5220\u9664\u7f13\u5b58 String redisKey = prefix + key; delete(redisKey, redisKeys); Object result = object.callback(); \/\/ \u5ef6\u65f6\u5220\u9664 try { String[] finalRedisKeys = redisKeys; threadPool.schedule(new Runnable() { @Override public void run() { delete(redisKey, finalRedisKeys); } }, 500, TimeUnit.MILLISECONDS); } catch (Exception e) { log.error(\"\u7ebf\u7a0b\u6c60\u5ef6\u65f6\u5220\u9664\u7f13\u5b58\u5931\u8d25:{}\", redisKey); } return result; } private void delete(String redisKey, String[] redisKeys) { if (ValueUtil.isEmpty(redisKeys)) { redis.delete(redisKey); return; } redis.del(redisKeys); } }\n         \n        \n       \n     \n    <\/pre>\n<p>\u6ce8\u89e3\u4f7f\u7528\u793a\u4f8b<\/p>\n<pre>public class Test {\n    @Override\n    @RedisDelByDbUpdate(redisPrefix = RedisConstant.BANNER, fieldName = \"ids\")\n    @RedisDelByDbUpdate(redisPrefix = RedisConstant.BANNER_LIST)\n    public void batchDeleted(List\n   \n     ids) throws BizException {\n        if (CollectionUtils.isEmpty(ids)) {\n            log.info(\"banner ID \u4e0d\u80fd\u4e3a\u7a7a\");\n            throw new BizException(BaseResponseCode.PARAM_IS_NOT_NULL);\n        }\n        try {\n            bannerMapper.batchDeleted(ids);\n        } catch (Exception e) {\n            log.error(\"==&gt;\u6279\u91cf\u5220\u9664Banner\u9519\u8bef:{}\n\n    \n   <\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u57fa\u4e8e&nbsp;Spring&#038;n&#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-201222","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201222","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=201222"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201222\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}