()\r\n .eq(Order::getId, id));\r\n redisTemplate.opsForValue().set(k, myOrder, 120, TimeUnit.SECONDS);\r\n return myOrder;\r\n });\r\n return order;\r\n}<\/order><\/pre>\n\u5728Cache\u7684get\u65b9\u6cd5\u4e2d\uff0c\u4f1a\u5148\u4ece\u7f13\u5b58\u4e2d\u8fdb\u884c\u67e5\u627e\uff0c\u5982\u679c\u627e\u5230\u7f13\u5b58\u7684\u503c\u90a3\u4e48\u76f4\u63a5\u8fd4\u56de\u3002\u5982\u679c\u6ca1\u6709\u627e\u5230\u5219\u6267\u884c\u540e\u9762\u7684\u65b9\u6cd5\uff0c\u5e76\u628a\u7ed3\u679c\u52a0\u5165\u5230\u7f13\u5b58\u4e2d\u3002<\/p>\n
\u56e0\u6b64\u4e0a\u9762\u7684\u903b\u8f91\u5c31\u662f\u5148\u67e5\u627eCaffeine\u4e2d\u7684\u7f13\u5b58\uff0c\u6ca1\u6709\u7684\u8bdd\u67e5\u627eRedis\uff0cRedis\u518d\u4e0d\u547d\u4e2d\u5219\u67e5\u8be2\u6570\u636e\u5e93\uff0c\u5199\u5165Redis\u7f13\u5b58\u7684\u64cd\u4f5c\u9700\u8981\u624b\u52a8\u5199\u5165\uff0c\u800cCaffeine\u7684\u5199\u5165\u7531get\u65b9\u6cd5\u81ea\u5df1\u5b8c\u6210\u3002<\/p>\n
\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u8bbe\u7f6eCaffeine\u7684\u8fc7\u671f\u65f6\u95f4\u4e3a60\u79d2\uff0c\u800cRedis\u7684\u8fc7\u671f\u65f6\u95f4\u4e3a120\u79d2\uff0c\u4e0b\u9762\u8fdb\u884c\u6d4b\u8bd5\uff0c\u9996\u5148\u770b\u7b2c\u4e00\u6b21\u63a5\u53e3\u8c03\u7528\u65f6\uff0c\u8fdb\u884c\u4e86\u6570\u636e\u5e93\u7684\u67e5\u8be2\uff1a<\/p>\n
<\/p>\n
\u800c\u5728\u4e4b\u540e60\u79d2\u5185\u8bbf\u95ee\u63a5\u53e3\u65f6\uff0c\u90fd\u6ca1\u6709\u6253\u5370\u6253\u4efb\u4f55sql\u6216\u81ea\u5b9a\u4e49\u7684\u65e5\u5fd7\u5185\u5bb9\uff0c\u8bf4\u660e\u63a5\u53e3\u6ca1\u6709\u67e5\u8be2Redis\u6216\u6570\u636e\u5e93\uff0c\u76f4\u63a5\u4eceCaffeine\u4e2d\u8bfb\u53d6\u4e86\u7f13\u5b58\u3002<\/p>\n
\u7b49\u5230\u8ddd\u79bb\u7b2c\u4e00\u6b21\u8c03\u7528\u63a5\u53e3\u8fdb\u884c\u7f13\u5b58\u768460\u79d2\u540e\uff0c\u518d\u6b21\u8c03\u7528\u63a5\u53e3\uff1a<\/p>\n
<\/p>\n
\u53ef\u4ee5\u770b\u5230\u8fd9\u65f6\u4eceRedis\u4e2d\u8bfb\u53d6\u4e86\u6570\u636e\uff0c\u56e0\u4e3a\u8fd9\u65f6Caffeine\u4e2d\u7684\u7f13\u5b58\u5df2\u7ecf\u8fc7\u671f\u4e86\uff0c\u4f46\u662fRedis\u4e2d\u7684\u7f13\u5b58\u6ca1\u6709\u8fc7\u671f\u4ecd\u7136\u53ef\u7528\u3002<\/p>\n
\u4e0b\u9762\u518d\u6765\u770b\u4e00\u4e0b\u4fee\u6539\u64cd\u4f5c\uff0c\u4ee3\u7801\u5728\u539f\u5148\u7684\u57fa\u7840\u4e0a\u6dfb\u52a0\u4e86\u624b\u52a8\u4fee\u6539Redis\u548cCaffeine\u7f13\u5b58\u7684\u903b\u8f91\uff1a<\/p>\n
public void updateOrder(Order order) {\r\n log.info(\"update order data\");\r\n String key=CacheConstant.ORDER + order.getId();\r\n orderMapper.updateById(order);\r\n \/\/\u4fee\u6539 Redis\r\n redisTemplate.opsForValue().set(key,order,120, TimeUnit.SECONDS);\r\n \/\/ \u4fee\u6539\u672c\u5730\u7f13\u5b58\r\n cache.put(key,order);\r\n}<\/pre>\n\u770b\u4e00\u4e0b\u4e0b\u9762\u56fe\u4e2d\u63a5\u53e3\u7684\u8c03\u7528\u3001\u4ee5\u53ca\u7f13\u5b58\u7684\u5237\u65b0\u8fc7\u7a0b\u3002\u53ef\u4ee5\u770b\u5230\u5728\u66f4\u65b0\u6570\u636e\u540e\uff0c\u540c\u6b65\u5237\u65b0\u4e86\u7f13\u5b58\u4e2d\u7684\u5185\u5bb9\uff0c\u518d\u4e4b\u540e\u7684\u8bbf\u95ee\u63a5\u53e3\u65f6\u4e0d\u67e5\u8be2\u6570\u636e\u5e93\uff0c\u4e5f\u53ef\u4ee5\u62ff\u5230\u6b63\u786e\u7684\u7ed3\u679c\uff1a<\/p>\n
<\/p>\n
\u6700\u540e\u518d\u6765\u770b\u4e00\u4e0b\u5220\u9664\u64cd\u4f5c\uff0c\u5728\u5220\u9664\u6570\u636e\u7684\u540c\u65f6\uff0c\u624b\u52a8\u79fb\u9664Reids\u548cCaffeine\u4e2d\u7684\u7f13\u5b58\uff1a<\/p>\n
public void deleteOrder(Long id) {\r\n log.info(\"delete order\");\r\n orderMapper.deleteById(id);\r\n String key= CacheConstant.ORDER + id;\r\n redisTemplate.delete(key);\r\n cache.invalidate(key);\r\n}<\/pre>\n\u6211\u4eec\u5728\u5220\u9664\u67d0\u4e2a\u7f13\u5b58\u540e\uff0c\u518d\u6b21\u8c03\u7528\u4e4b\u524d\u7684\u67e5\u8be2\u63a5\u53e3\u65f6\uff0c\u53c8\u4f1a\u51fa\u73b0\u91cd\u65b0\u67e5\u8be2\u6570\u636e\u5e93\u7684\u60c5\u51b5\uff1a<\/p>\n
<\/p>\n
\u7b80\u5355\u7684\u6f14\u793a\u5230\u6b64\u4e3a\u6b62\uff0c\u53ef\u4ee5\u770b\u5230\u4e0a\u9762\u8fd9\u79cd\u4f7f\u7528\u7f13\u5b58\u7684\u65b9\u5f0f\uff0c\u867d\u7136\u770b\u8d77\u6765\u6ca1\u4ec0\u4e48\u5927\u95ee\u9898\uff0c\u4f46\u662f\u5bf9\u4ee3\u7801\u7684\u5165\u4fb5\u6027\u6bd4\u8f83\u5f3a\u3002\u5728\u4e1a\u52a1\u5904\u7406\u7684\u8fc7\u7a0b\u4e2d\u8981\u7531\u6211\u4eec\u9891\u7e41\u7684\u64cd\u4f5c\u4e24\u7ea7\u7f13\u5b58\uff0c\u4f1a\u7ed9\u5f00\u53d1\u4eba\u5458\u5e26\u6765\u5f88\u5927\u8d1f\u62c5\u3002\u90a3\u4e48\uff0c\u6709\u4ec0\u4e48\u65b9\u6cd5\u80fd\u591f\u7b80\u5316\u8fd9\u4e00\u8fc7\u7a0b\u5462?\u6211\u4eec\u770b\u770b\u4e0b\u4e00\u4e2a\u7248\u672c\u3002<\/p>\n
V2.0\u7248\u672c<\/strong><\/div>\n\u5728spring\u9879\u76ee\u4e2d\uff0c\u63d0\u4f9b\u4e86CacheManager\u63a5\u53e3\u548c\u4e00\u4e9b\u6ce8\u89e3\uff0c\u5141\u8bb8\u8ba9\u6211\u4eec\u901a\u8fc7\u6ce8\u89e3\u7684\u65b9\u5f0f\u6765\u64cd\u4f5c\u7f13\u5b58\u3002\u5148\u6765\u770b\u4e00\u4e0b\u5e38\u7528\u51e0\u4e2a\u6ce8\u89e3\u8bf4\u660e\uff1a<\/p>\n
@Cacheable\uff1a\u6839\u636e\u952e\u4ece\u7f13\u5b58\u4e2d\u53d6\u503c\uff0c\u5982\u679c\u7f13\u5b58\u5b58\u5728\uff0c\u90a3\u4e48\u83b7\u53d6\u7f13\u5b58\u6210\u529f\u4e4b\u540e\uff0c\u76f4\u63a5\u8fd4\u56de\u8fd9\u4e2a\u7f13\u5b58\u7684\u7ed3\u679c\u3002\u5982\u679c\u7f13\u5b58\u4e0d\u5b58\u5728\uff0c\u90a3\u4e48\u6267\u884c\u65b9\u6cd5\uff0c\u5e76\u5c06\u7ed3\u679c\u653e\u5165\u7f13\u5b58\u4e2d\u3002
\n@CachePut\uff1a\u4e0d\u7ba1\u4e4b\u524d\u7684\u952e\u5bf9\u5e94\u7684\u7f13\u5b58\u662f\u5426\u5b58\u5728\uff0c\u90fd\u6267\u884c\u65b9\u6cd5\uff0c\u5e76\u5c06\u7ed3\u679c\u5f3a\u5236\u653e\u5165\u7f13\u5b58\u3002
\n@CacheEvict\uff1a\u6267\u884c\u5b8c\u65b9\u6cd5\u540e\uff0c\u4f1a\u79fb\u9664\u6389\u7f13\u5b58\u4e2d\u7684\u6570\u636e\u3002
\n\u5982\u679c\u8981\u4f7f\u7528\u4e0a\u9762\u8fd9\u51e0\u4e2a\u6ce8\u89e3\u7ba1\u7406\u7f13\u5b58\u7684\u8bdd\uff0c\u6211\u4eec\u5c31\u4e0d\u9700\u8981\u914d\u7f6eV1\u7248\u672c\u4e2d\u7684\u90a3\u4e2a\u7c7b\u578b\u4e3aCache\u7684Bean\u4e86\uff0c\u800c\u662f\u9700\u8981\u914d\u7f6espring\u4e2d\u7684CacheManager\u7684\u76f8\u5173\u53c2\u6570\uff0c\u5177\u4f53\u53c2\u6570\u7684\u914d\u7f6e\u548c\u4e4b\u524d\u4e00\u6837\uff1a<\/p>\n
@Configuration\r\npublic class CacheManagerConfig {\r\n @Bean\r\n public CacheManager cacheManager(){\r\n CaffeineCacheManager cacheManager=new CaffeineCacheManager();\r\n cacheManager.setCaffeine(Caffeine.newBuilder()\r\n .initialCapacity(128)\r\n .maximumSize(1024)\r\n .expireAfterWrite(60, TimeUnit.SECONDS));\r\n return cacheManager;\r\n }\r\n}<\/pre>\n\u7136\u540e\u5728\u542f\u52a8\u7c7b\u4e0a\u518d\u6dfb\u52a0\u4e0a@EnableCaching\u6ce8\u89e3\uff0c\u5c31\u53ef\u4ee5\u5728\u9879\u76ee\u4e2d\u57fa\u4e8e\u6ce8\u89e3\u6765\u4f7f\u7528Caffeine\u7684\u7f13\u5b58\u652f\u6301\u4e86\u3002\u4e0b\u9762\uff0c\u518d\u6b21\u5bf9Service\u5c42\u4ee3\u7801\u8fdb\u884c\u6539\u9020\u3002<\/p>\n
\u9996\u5148\uff0c\u8fd8\u662f\u6539\u9020\u67e5\u8be2\u65b9\u6cd5\uff0c\u5728\u65b9\u6cd5\u4e0a\u6dfb\u52a0@Cacheable\u6ce8\u89e3\uff1a<\/p>\n
@Cacheable(value = \"order\",key = \"#id\")\r\n\/\/@Cacheable(cacheNames = \"order\",key = \"#p0\")\r\npublic Order getOrderById(Long id) {\r\n String key= CacheConstant.ORDER + id;\r\n \/\/\u5148\u67e5\u8be2 Redis\r\n Object obj = redisTemplate.opsForValue().get(key);\r\n if (Objects.nonNull(obj)){\r\n log.info(\"get data from redis\");\r\n return (Order) obj;\r\n }\r\n \/\/ Redis\u6ca1\u6709\u5219\u67e5\u8be2 DB\r\n log.info(\"get data from database\");\r\n Order myOrder = orderMapper.selectOne(new LambdaQueryWrapper()\r\n .eq(Order::getId, id));\r\n redisTemplate.opsForValue().set(key,myOrder,120, TimeUnit.SECONDS);\r\n return myOrder;<\/order><\/pre>\n@Cacheable\u6ce8\u89e3\u7684\u5c5e\u6027\u591a\u8fbe9\u4e2a\uff0c\u597d\u5728\u6211\u4eec\u65e5\u5e38\u4f7f\u7528\u65f6\u53ea\u9700\u8981\u914d\u7f6e\u4e24\u4e2a\u5e38\u7528\u7684\u5c31\u53ef\u4ee5\u4e86\u3002\u5176\u4e2dvalue\u548ccacheNames\u4e92\u4e3a\u522b\u540d\u5173\u7cfb\uff0c\u8868\u793a\u5f53\u524d\u65b9\u6cd5\u7684\u7ed3\u679c\u4f1a\u88ab\u7f13\u5b58\u5728\u54ea\u4e2aCache\u4e0a\uff0c\u5e94\u7528\u4e2d\u901a\u8fc7cacheName\u6765\u5bf9Cache\u8fdb\u884c\u9694\u79bb\uff0c\u6bcf\u4e2acacheName\u5bf9\u5e94\u4e00\u4e2aCache\u5b9e\u73b0\u3002value\u548ccacheNames\u53ef\u4ee5\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u7ed1\u5b9a\u591a\u4e2aCache\u3002<\/p>\n
\u800c\u53e6\u4e00\u4e2a\u91cd\u8981\u5c5e\u6027key\uff0c\u7528\u6765\u6307\u5b9a\u7f13\u5b58\u65b9\u6cd5\u7684\u8fd4\u56de\u7ed3\u679c\u65f6\u5bf9\u5e94\u7684key\uff0c\u8fd9\u4e2a\u5c5e\u6027\u652f\u6301\u4f7f\u7528SpringEL\u8868\u8fbe\u5f0f\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u51e0\u79cd\u65b9\u5f0f\u4f5c\u4e3akey\uff1a<\/p>\n
#\u53c2\u6570\u540d\r\n#\u53c2\u6570\u5bf9\u8c61.\u5c5e\u6027\u540d\r\n#p\u53c2\u6570\u5bf9\u5e94\u4e0b\u6807<\/pre>\n\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u770b\u5230\u6dfb\u52a0\u4e86@Cacheable\u6ce8\u89e3\u540e\uff0c\u5728\u4ee3\u7801\u4e2d\u53ea\u9700\u8981\u4fdd\u7559\u539f\u6709\u7684\u4e1a\u52a1\u5904\u7406\u903b\u8f91\u548c\u64cd\u4f5cRedis\u90e8\u5206\u7684\u4ee3\u7801\u5373\u53ef\uff0cCaffeine\u90e8\u5206\u7684\u7f13\u5b58\u5c31\u4ea4\u7ed9spring\u5904\u7406\u4e86\u3002<\/p>\n
\u4e0b\u9762\uff0c\u6211\u4eec\u518d\u6765\u6539\u9020\u4e00\u4e0b\u66f4\u65b0\u65b9\u6cd5\uff0c\u540c\u6837\uff0c\u4f7f\u7528@CachePut\u6ce8\u89e3\u540e\u79fb\u9664\u6389\u624b\u52a8\u66f4\u65b0Cache\u7684\u64cd\u4f5c\uff1a<\/p>\n
@CachePut(cacheNames = \"order\",key = \"#order.id\")\r\npublic Order updateOrder(Order order) {\r\n log.info(\"update order data\");\r\n orderMapper.updateById(order);\r\n \/\/\u4fee\u6539 Redis\r\n redisTemplate.opsForValue().set(CacheConstant.ORDER + order.getId(),\r\n order, 120, TimeUnit.SECONDS);\r\n return order;\r\n}<\/pre>\n\u6ce8\u610f\uff0c\u8fd9\u91cc\u548cV1\u7248\u672c\u7684\u4ee3\u7801\u6709\u4e00\u70b9\u533a\u522b\uff0c\u5728\u4e4b\u524d\u7684\u66f4\u65b0\u64cd\u4f5c\u65b9\u6cd5\u4e2d\uff0c\u662f\u6ca1\u6709\u8fd4\u56de\u503c\u7684void\u7c7b\u578b\uff0c\u4f46\u662f\u8fd9\u91cc\u9700\u8981\u4fee\u6539\u8fd4\u56de\u503c\u7684\u7c7b\u578b\uff0c\u5426\u5219\u4f1a\u7f13\u5b58\u4e00\u4e2a\u7a7a\u5bf9\u8c61\u5230\u7f13\u5b58\u4e2d\u5bf9\u5e94\u7684key\u4e0a\u3002\u5f53\u4e0b\u6b21\u6267\u884c\u67e5\u8be2\u64cd\u4f5c\u65f6\uff0c\u4f1a\u76f4\u63a5\u8fd4\u56de\u7a7a\u5bf9\u8c61\u7ed9\u8c03\u7528\u65b9\uff0c\u800c\u4e0d\u4f1a\u6267\u884c\u65b9\u6cd5\u4e2d\u67e5\u8be2\u6570\u636e\u5e93\u6216Redis\u7684\u64cd\u4f5c\u3002<\/p>\n
\u6700\u540e\uff0c\u5220\u9664\u65b9\u6cd5\u7684\u6539\u9020\u5c31\u5f88\u7b80\u5355\u4e86\uff0c\u4f7f\u7528@CacheEvict\u6ce8\u89e3\uff0c\u65b9\u6cd5\u4e2d\u53ea\u9700\u8981\u5220\u9664Redis\u4e2d\u7684\u7f13\u5b58\u5373\u53ef\uff1a<\/p>\n
@CacheEvict(cacheNames = \"order\",key = \"#id\")\r\npublic void deleteOrder(Long id) {\r\n log.info(\"delete order\");\r\n orderMapper.deleteById(id);\r\n redisTemplate.delete(CacheConstant.ORDER + id);\r\n}<\/pre>\n\u53ef\u4ee5\u770b\u5230\uff0c\u501f\u52a9spring\u4e2d\u7684CacheManager\u548cCache\u76f8\u5173\u7684\u6ce8\u89e3\uff0c\u5bf9V1\u7248\u672c\u7684\u4ee3\u7801\u7ecf\u8fc7\u6539\u8fdb\u540e\uff0c\u53ef\u4ee5\u628a\u5168\u624b\u52a8\u64cd\u4f5c\u4e24\u7ea7\u7f13\u5b58\u7684\u5f3a\u5165\u4fb5\u4ee3\u7801\u65b9\u5f0f\uff0c\u6539\u8fdb\u4e3a\u672c\u5730\u7f13\u5b58\u4ea4\u7ed9spring\u7ba1\u7406\uff0cRedis\u7f13\u5b58\u624b\u52a8\u4fee\u6539\u7684\u534a\u5165\u4fb5\u65b9\u5f0f\u3002\u90a3\u4e48\uff0c\u8fd8\u80fd\u8fdb\u4e00\u6b65\u6539\u9020\uff0c\u4f7f\u4e4b\u6210\u4e3a\u5bf9\u4e1a\u52a1\u4ee3\u7801\u5b8c\u5168\u65e0\u5165\u4fb5\u7684\u65b9\u5f0f\u5417?<\/p>\n
V3.0\u7248\u672c<\/strong><\/div>\n\u6a21\u4effspring\u901a\u8fc7\u6ce8\u89e3\u7ba1\u7406\u7f13\u5b58\u7684\u65b9\u5f0f\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u9009\u62e9\u81ea\u5b9a\u4e49\u6ce8\u89e3\uff0c\u7136\u540e\u5728\u5207\u9762\u4e2d\u5904\u7406\u7f13\u5b58\uff0c\u4ece\u800c\u5c06\u5bf9\u4e1a\u52a1\u4ee3\u7801\u7684\u5165\u4fb5\u964d\u5230\u6700\u4f4e\u3002<\/p>\n
\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u6ce8\u89e3\uff0c\u7528\u4e8e\u6dfb\u52a0\u5728\u9700\u8981\u64cd\u4f5c\u7f13\u5b58\u7684\u65b9\u6cd5\u4e0a\uff1a<\/p>\n
@Target(ElementType.METHOD)\r\n@Retention(RetentionPolicy.RUNTIME)\r\n@Documented\r\npublic @interface DoubleCache {\r\n String cacheName();\r\n String key(); \/\/\u652f\u6301springEl\u8868\u8fbe\u5f0f\r\n long l2TimeOut() default 120;\r\n CacheType type() default CacheType.FULL;\r\n}<\/pre>\n\u6211\u4eec\u4f7f\u7528cacheName + key\u4f5c\u4e3a\u7f13\u5b58\u7684\u771f\u6b63key(\u4ec5\u5b58\u5728\u4e00\u4e2aCache\u4e2d\uff0c\u4e0d\u505aCacheName\u9694\u79bb)\uff0cl2TimeOut\u4e3a\u53ef\u4ee5\u8bbe\u7f6e\u7684\u4e8c\u7ea7\u7f13\u5b58Redis\u7684\u8fc7\u671f\u65f6\u95f4\uff0ctype\u662f\u4e00\u4e2a\u679a\u4e3e\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u8868\u793a\u64cd\u4f5c\u7f13\u5b58\u7684\u7c7b\u578b\uff0c\u679a\u4e3e\u7c7b\u578b\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n
public enum CacheType {\r\n FULL, \/\/\u5b58\u53d6\r\n PUT, \/\/\u53ea\u5b58\r\n DELETE \/\/\u5220\u9664\r\n}<\/pre>\n\u56e0\u4e3a\u8981\u4f7fkey\u652f\u6301springEl\u8868\u8fbe\u5f0f\uff0c\u6240\u4ee5\u9700\u8981\u5199\u4e00\u4e2a\u65b9\u6cd5\uff0c\u4f7f\u7528\u8868\u8fbe\u5f0f\u89e3\u6790\u5668\u89e3\u6790\u53c2\u6570\uff1a<\/p>\n
public static String parse(String elString, TreeMap map){\r\n elString=String.format(\"#{%s}\",elString);\r\n \/\/\u521b\u5efa\u8868\u8fbe\u5f0f\u89e3\u6790\u5668\r\n ExpressionParser parser = new SpelExpressionParser();\r\n \/\/\u901a\u8fc7evaluationContext.setVariable\u53ef\u4ee5\u5728\u4e0a\u4e0b\u6587\u4e2d\u8bbe\u5b9a\u53d8\u91cf\u3002\r\n EvaluationContext context = new StandardEvaluationContext();\r\n map.entrySet().forEach(entry->\r\n context.setVariable(entry.getKey(),entry.getValue())\r\n );\r\n\r\n \/\/\u89e3\u6790\u8868\u8fbe\u5f0f\r\n Expression expression = parser.parseExpression(elString, new TemplateParserContext());\r\n \/\/\u4f7f\u7528Expression.getValue()\u83b7\u53d6\u8868\u8fbe\u5f0f\u7684\u503c\uff0c\u8fd9\u91cc\u4f20\u5165\u4e86Evaluation\u4e0a\u4e0b\u6587\r\n String value = expression.getValue(context, String.class);\r\n return value;\r\n}<\/pre>\n\u53c2\u6570\u4e2d\u7684elString\u5bf9\u5e94\u7684\u5c31\u662f\u6ce8\u89e3\u4e2dkey\u7684\u503c\uff0cmap\u662f\u5c06\u539f\u65b9\u6cd5\u7684\u53c2\u6570\u5c01\u88c5\u540e\u7684\u7ed3\u679c\u3002\u7b80\u5355\u8fdb\u884c\u4e00\u4e0b\u6d4b\u8bd5\uff1a<\/p>\n
public void test() {\r\n String elString=\"#order.money\";\r\n String elString2=\"#user\";\r\n String elString3=\"#p0\"; \r\n\r\n TreeMap map=new TreeMap<>();\r\n Order order = new Order();\r\n order.setId(111L);\r\n order.setMoney(123D);\r\n map.put(\"order\",order);\r\n map.put(\"user\",\"Hydra\");\r\n\r\n String val = parse(elString, map);\r\n String val2 = parse(elString2, map);\r\n String val3 = parse(elString3, map);\r\n\r\n System.out.println(val);\r\n System.out.println(val2);\r\n System.out.println(val3);\r\n}<\/pre>\n\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff0c\u53ef\u4ee5\u770b\u5230\u652f\u6301\u6309\u7167\u53c2\u6570\u540d\u79f0\u3001\u53c2\u6570\u5bf9\u8c61\u7684\u5c5e\u6027\u540d\u79f0\u8bfb\u53d6\uff0c\u4f46\u662f\u4e0d\u652f\u6301\u6309\u7167\u53c2\u6570\u4e0b\u6807\u8bfb\u53d6\uff0c\u6682\u65f6\u7559\u4e2a\u5c0f\u5751\u4ee5\u540e\u518d\u5904\u7406\u3002<\/p>\n
123.0\r\nHydra\r\nnull<\/pre>\n\u81f3\u4e8eCache\u76f8\u5173\u53c2\u6570\u7684\u914d\u7f6e\uff0c\u6211\u4eec\u6cbf\u7528V1\u7248\u672c\u4e2d\u7684\u914d\u7f6e\u5373\u53ef\u3002\u51c6\u5907\u5de5\u4f5c\u505a\u5b8c\u4e86\uff0c\u4e0b\u9762\u6211\u4eec\u5b9a\u4e49\u5207\u9762\uff0c\u5728\u5207\u9762\u4e2d\u64cd\u4f5cCache\u6765\u8bfb\u5199Caffeine\u7684\u7f13\u5b58\uff0c\u64cd\u4f5cRedisTemplate\u8bfb\u5199Redis\u7f13\u5b58\u3002<\/p>\n
@Slf4j @Component @Aspect \r\n@AllArgsConstructor\r\npublic class CacheAspect {\r\n private final Cache cache;\r\n private final RedisTemplate redisTemplate;\r\n\r\n @Pointcut(\"@annotation(com.cn.dc.annotation.DoubleCache)\")\r\n public void cacheAspect() {\r\n }\r\n\r\n @Around(\"cacheAspect()\")\r\n public Object doAround(ProceedingJoinPoint point) throws Throwable {\r\n MethodSignature signature = (MethodSignature) point.getSignature();\r\n Method method = signature.getMethod();\r\n\r\n \/\/\u62fc\u63a5\u89e3\u6790springEl\u8868\u8fbe\u5f0f\u7684map\r\n String[] paramNames = signature.getParameterNames();\r\n Object[] args = point.getArgs();\r\n TreeMap treeMap = new TreeMap<>();\r\n for (int i = 0; i < paramNames.length; i++) {\r\n treeMap.put(paramNames[i],args[i]);\r\n }\r\n\r\n DoubleCache annotation = method.getAnnotation(DoubleCache.class);\r\n String elResult = ElParser.parse(annotation.key(), treeMap);\r\n String realKey = annotation.cacheName() + CacheConstant.COLON + elResult;\r\n\r\n \/\/\u5f3a\u5236\u66f4\u65b0\r\n if (annotation.type()== CacheType.PUT){\r\n Object object = point.proceed();\r\n redisTemplate.opsForValue().set(realKey, object,annotation.l2TimeOut(), TimeUnit.SECONDS);\r\n cache.put(realKey, object);\r\n return object;\r\n }\r\n \/\/\u5220\u9664\r\n else if (annotation.type()== CacheType.DELETE){\r\n redisTemplate.delete(realKey);\r\n cache.invalidate(realKey);\r\n return point.proceed();\r\n }\r\n\r\n \/\/\u8bfb\u5199\uff0c\u67e5\u8be2Caffeine\r\n Object caffeineCache = cache.getIfPresent(realKey);\r\n if (Objects.nonNull(caffeineCache)) {\r\n log.info(\"get data from caffeine\");\r\n return caffeineCache;\r\n }\r\n\r\n \/\/\u67e5\u8be2Redis\r\n Object redisCache = redisTemplate.opsForValue().get(realKey);\r\n if (Objects.nonNull(redisCache)) {\r\n log.info(\"get data from redis\");\r\n cache.put(realKey, redisCache);\r\n return redisCache;\r\n }\r\n\r\n log.info(\"get data from database\");\r\n Object object = point.proceed();\r\n if (Objects.nonNull(object)){\r\n \/\/\u5199\u5165Redis\r\n redisTemplate.opsForValue().set(realKey, object,annotation.l2TimeOut(), TimeUnit.SECONDS);\r\n \/\/\u5199\u5165Caffeine\r\n cache.put(realKey, object); \r\n }\r\n return object;\r\n }\r\n}<\/pre>\n\u5207\u9762\u4e2d\u4e3b\u8981\u505a\u4e86\u4e0b\u9762\u51e0\u4ef6\u5de5\u4f5c\uff1a<\/p>\n
\u901a\u8fc7\u65b9\u6cd5\u7684\u53c2\u6570\uff0c\u89e3\u6790\u6ce8\u89e3\u4e2dkey\u7684springEl\u8868\u8fbe\u5f0f\uff0c\u7ec4\u88c5\u771f\u6b63\u7f13\u5b58\u7684key\u3002<\/li>\n\u6839\u636e\u64cd\u4f5c\u7f13\u5b58\u7684\u7c7b\u578b\uff0c\u5206\u522b\u5904\u7406\u5b58\u53d6\u3001\u53ea\u5b58\u3001\u5220\u9664\u7f13\u5b58\u64cd\u4f5c\u3002<\/li>\n\u5220\u9664\u548c\u5f3a\u5236\u66f4\u65b0\u7f13\u5b58\u7684\u64cd\u4f5c\uff0c\u90fd\u9700\u8981\u6267\u884c\u539f\u65b9\u6cd5\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u7f13\u5b58\u5220\u9664\u6216\u66f4\u65b0\u64cd\u4f5c\u3002<\/li>\n\u5b58\u53d6\u64cd\u4f5c\u524d\uff0c\u5148\u68c0\u67e5\u7f13\u5b58\u4e2d\u662f\u5426\u6709\u6570\u636e\uff0c\u5982\u679c\u6709\u5219\u76f4\u63a5\u8fd4\u56de\uff0c\u6ca1\u6709\u5219\u6267\u884c\u539f\u65b9\u6cd5\uff0c\u5e76\u5c06\u7ed3\u679c\u5b58\u5165\u7f13\u5b58\u3002<\/li>\n\u4fee\u6539Service\u5c42\u4ee3\u7801\uff0c\u4ee3\u7801\u4e2d\u53ea\u4fdd\u7559\u539f\u6709\u4e1a\u52a1\u4ee3\u7801\uff0c\u518d\u6dfb\u52a0\u4e0a\u6211\u4eec\u81ea\u5b9a\u4e49\u7684\u6ce8\u89e3\u5c31\u53ef\u4ee5\u4e86\uff1a<\/p>\n
@DoubleCache(cacheName = \"order\", key = \"#id\",\r\n type = CacheType.FULL)\r\npublic Order getOrderById(Long id) {\r\n Order myOrder = orderMapper.selectOne(new LambdaQueryWrapper()\r\n .eq(Order::getId, id));\r\n return myOrder;\r\n}\r\n\r\n@DoubleCache(cacheName = \"order\",key = \"#order.id\",\r\n type = CacheType.PUT)\r\npublic Order updateOrder(Order order) {\r\n orderMapper.updateById(order);\r\n return order;\r\n}\r\n\r\n@DoubleCache(cacheName = \"order\",key = \"#id\",\r\n type = CacheType.DELETE)\r\npublic void deleteOrder(Long id) {\r\n orderMapper.deleteById(id);\r\n}<\/order><\/pre>\n\u5230\u8fd9\u91cc\uff0c\u57fa\u4e8e\u5207\u9762\u64cd\u4f5c\u7f13\u5b58\u7684\u6539\u9020\u5c31\u5b8c\u6210\u4e86\uff0cService\u7684\u4ee3\u7801\u4e5f\u77ac\u95f4\u6e05\u723d\u4e86\u5f88\u591a\uff0c\u8ba9\u6211\u4eec\u53ef\u4ee5\u7ee7\u7eed\u4e13\u6ce8\u4e8e\u4e1a\u52a1\u903b\u8f91\u5904\u7406\uff0c\u800c\u4e0d\u7528\u8d39\u5fc3\u53bb\u64cd\u4f5c\u4e24\u7ea7\u7f13\u5b58\u4e86\u3002<\/p>\n
\u603b\u7ed3\u672c\u6587\u6309\u7167\u5bf9\u4e1a\u52a1\u5165\u4fb5\u7684\u9012\u51cf\u7a0b\u5ea6\uff0c\u4f9d\u6b21\u4ecb\u7ecd\u4e86\u4e09\u79cd\u7ba1\u7406\u4e24\u7ea7\u7f13\u5b58\u7684\u65b9\u6cd5\u3002\u81f3\u4e8e\u5728\u9879\u76ee\u4e2d\u662f\u5426\u9700\u8981\u4f7f\u7528\u4e8c\u7ea7\u7f13\u5b58\uff0c\u9700\u8981\u8003\u8651\u81ea\u8eab\u4e1a\u52a1\u60c5\u51b5\uff0c\u5982\u679cRedis\u8fd9\u79cd\u8fdc\u7a0b\u7f13\u5b58\u5df2\u7ecf\u80fd\u591f\u6ee1\u8db3\u4f60\u7684\u4e1a\u52a1\u9700\u6c42\uff0c\u90a3\u4e48\u5c31\u6ca1\u6709\u5fc5\u8981\u518d\u4f7f\u7528\u672c\u5730\u7f13\u5b58\u4e86\u3002\u6bd5\u7adf\u5b9e\u9645\u4f7f\u7528\u8d77\u6765\u8fdc\u6ca1\u6709\u90a3\u4e48\u7b80\u5355\uff0c\u672c\u6587\u4e2d\u53ea\u662f\u4ecb\u7ecd\u4e86\u6700\u57fa\u7840\u7684\u4f7f\u7528\uff0c\u5b9e\u9645\u4e2d\u7684\u5e76\u53d1\u95ee\u9898\u3001\u4e8b\u52a1\u7684\u56de\u6eda\u95ee\u9898\u90fd\u9700\u8981\u8003\u8651\uff0c\u8fd8\u9700\u8981\u601d\u8003\u4ec0\u4e48\u6570\u636e\u9002\u5408\u653e\u5728\u4e00\u7ea7\u7f13\u5b58\u3001\u4ec0\u4e48\u6570\u636e\u9002\u5408\u653e\u5728\u4e8c\u7ea7\u7f13\u5b58\u7b49\u7b49\u7684\u5176\u4ed6\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u5728\u9ad8\u6027\u80fd\u7684\u670d\u52a1\u67b6\u6784\u8bbe\u8ba1\u4e2d\uff0c\u7f13\u5b58\u662f\u4e00\u4e2a\u4e0d\u53ef\u6216\u7f3a\u7684\u73af\u8282\u3002\u5728\u5b9e\u9645\u7684\u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u4e00\u4e9b\u70ed\u70b9\u6570\u636e\u5b58\u50a8\u5230Redis\u6216 […]<\/p>\n","protected":false},"author":1920,"featured_media":202522,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-238251","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/238251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1920"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=238251"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/238251\/revisions"}],"predecessor-version":[{"id":238357,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/238251\/revisions\/238357"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/202522"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=238251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=238251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=238251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}