{"id":646,"date":"2023-12-15T09:01:01","date_gmt":"2023-12-15T01:01:01","guid":{"rendered":""},"modified":"2023-12-15T09:01:01","modified_gmt":"2023-12-15T01:01:01","slug":"Redis\u7f13\u5b58\u6ce8\u89e3","status":"publish","type":"post","link":"https:\/\/mushiming.com\/646.html","title":{"rendered":"Redis\u7f13\u5b58\u6ce8\u89e3"},"content":{"rendered":"

\u672c\u7bc7\u535a\u5ba2\u5c06\u8be6\u7ec6\u8bb2\u89e3@Cacheable,@CachePut , @CacheEvict<\/strong>\u4f7f\u7528\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n

1\u3001@Cacheable<\/h3>\n

\u4f5c\u7528\u662f\u4e3b\u8981\u9488\u5bf9\u65b9\u6cd5\u914d\u7f6e\uff0c\u80fd\u591f\u6839\u636e\u65b9\u6cd5\u7684\u8bf7\u6c42\u53c2\u6570\u5bf9\u5176\u7ed3\u679c\u8fdb\u884c\u7f13\u5b58 <\/strong><\/p>\n\n\n\n\n\n\n\n
\u53c2\u6570<\/th>\n\u89e3\u91ca<\/th>\nexample<\/th>\n<\/tr>\n<\/thead>\n
value<\/span><\/td>\n\u7f13\u5b58\u7684\u540d\u79f0\uff0c\u5728 spring \u914d\u7f6e\u6587\u4ef6\u4e2d\u5b9a\u4e49\uff0c\u5fc5\u987b\u6307\u5b9a\u81f3\u5c11\u4e00\u4e2a<\/td>\n\u4f8b\u5982:
@Cacheable(value=\u201dmycache\u201d)
@Cacheable(value={\u201dcache1\u201d,\u201dcache2\u201d}<\/td>\n<\/tr>\n
key<\/span><\/td>\n\u7f13\u5b58\u7684 key\uff0c\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u5982\u679c\u6307\u5b9a\u8981\u6309\u7167 SpEL \u8868\u8fbe\u5f0f\u7f16\u5199\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\uff0c\u5219\u7f3a\u7701\u6309\u7167\u65b9\u6cd5\u7684\u6240\u6709\u53c2\u6570\u8fdb\u884c\u7ec4\u5408<\/td>\n@Cacheable(value=\u201dtestcache\u201d,key=\u201d#userName\u201d)<\/td>\n<\/tr>\n
condition<\/td>\n\u7f13\u5b58\u7684\u6761\u4ef6\uff0c\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u4f7f\u7528 SpEL \u7f16\u5199\uff0c\u8fd4\u56de true \u6216\u8005 false\uff0c\u53ea\u6709\u4e3a true \u624d\u8fdb\u884c\u7f13\u5b58<\/td>\n@Cacheable(value=\u201dtestcache\u201d,condition=\"#userName.length()>2\u201d)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u6848\u4f8b\uff1a<\/p>\n

service\u5c42<\/p>\n

\/\/\u8868\u793a\u4f7f\u7528my-redis-cache1\u7f13\u5b58\u7a7a\u95f4\uff0ckey\u7684\u751f\u6210\u7b56\u7565\u4e3abook+bid\uff0c\u5f53bid>10\u7684\u65f6\u5019\u624d\u4f1a\u4f7f\u7528\u7f13\u5b58\n    @Cacheable(value = \"my-redis-cache1\",key = \"'book'+#bid\",condition = \"#bid>10\")\n    public Book selectByPrimaryKey(Integer bid) {\n        return bookMapper.selectByPrimaryKey(bid);\n    }<\/code><\/pre>\n

Test\u7c7b<\/p>\n

 @Test\n    public void selectByPrimaryKey() {\n        Book book = bookService.selectByPrimaryKey(18);\n        System.out.println(book);\n        System.out.println(\"---------------------------------------------\");\n        Book book2 = bookService.selectByPrimaryKey(18);\n        System.out.println(book2);\n    }<\/code><\/pre>\n

\u63a7\u5236\u53f0\u8f93\u51fa<\/p>\n

\"Redis\u7f13\u5b58\u6ce8\u89e3<\/p>\n

 2\u3001@CachePut<\/strong><\/h3>\n

\u4f5c\u7528\u662f\u4e3b\u8981\u9488\u5bf9\u65b9\u6cd5\u914d\u7f6e\uff0c\u80fd\u591f\u6839\u636e\u65b9\u6cd5\u7684\u8bf7\u6c42\u53c2\u6570\u5bf9\u5176\u7ed3\u679c\u8fdb\u884c\u7f13\u5b58\uff0c\u548c @Cacheable \u4e0d\u540c\u7684\u662f\uff0c\u5b83\u6bcf\u6b21\u90fd\u4f1a\u89e6\u53d1\u771f\u5b9e\u67e5\u8be2\u3002<\/strong><\/p>\n\n\n\n\n\n\n\n
\u53c2\u6570<\/th>\n\u89e3\u91ca<\/th>\nexample<\/th>\n<\/tr>\n<\/thead>\n
value<\/span><\/td>\n\u7f13\u5b58\u7684\u540d\u79f0\uff0c\u5728 spring \u914d\u7f6e\u6587\u4ef6\u4e2d\u5b9a\u4e49\uff0c\u5fc5\u987b\u6307\u5b9a\u81f3\u5c11\u4e00\u4e2a<\/td>\n@CachePut(value=\u201dmy cache\u201d)<\/td>\n<\/tr>\n
key<\/span><\/td>\n\u7f13\u5b58\u7684 key\uff0c\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u5982\u679c\u6307\u5b9a\u8981\u6309\u7167 SpEL \u8868\u8fbe\u5f0f\u7f16\u5199\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\uff0c\u5219\u7f3a\u7701\u6309\u7167\u65b9\u6cd5\u7684\u6240\u6709\u53c2\u6570\u8fdb\u884c\u7ec4\u5408<\/td>\n@CachePut(value=\u201dtestcache\u201d,key=\u201d#userName\u201d)<\/td>\n<\/tr>\n
condition<\/td>\n\u7f13\u5b58\u7684\u6761\u4ef6\uff0c\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u4f7f\u7528 SpEL \u7f16\u5199\uff0c\u8fd4\u56de true \u6216\u8005 false\uff0c\u53ea\u6709\u4e3a true \u624d\u8fdb\u884c\u7f13\u5b58<\/td>\n@CachePut(value=\u201dtestcache\u201d,condition=\u201d#userName.length()>2\u201d)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

3\u3001@CacheEvict<\/h3>\n

\u4f5c\u7528\u662f\u4e3b\u8981\u9488\u5bf9\u65b9\u6cd5\u914d\u7f6e\uff0c\u80fd\u591f\u6839\u636e\u4e00\u5b9a\u7684\u6761\u4ef6\u5bf9\u7f13\u5b58\u8fdb\u884c\u6e05\u7a7a<\/strong><\/p>\n\n\n\n\n\n\n\n\n\n
\u53c2\u6570<\/th>\n\u89e3\u91ca<\/th>\nexample<\/th>\n<\/tr>\n<\/thead>\n
value<\/span><\/td>\n\u7f13\u5b58\u7684\u540d\u79f0\uff0c\u5728 spring \u914d\u7f6e\u6587\u4ef6\u4e2d\u5b9a\u4e49\uff0c\u5fc5\u987b\u6307\u5b9a\u81f3\u5c11\u4e00\u4e2a<\/td>\n@CacheEvict(value=\u201dmy cache\u201d)<\/td>\n<\/tr>\n
key<\/span><\/td>\n\u7f13\u5b58\u7684 key\uff0c\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u5982\u679c\u6307\u5b9a\u8981\u6309\u7167 SpEL \u8868\u8fbe\u5f0f\u7f16\u5199\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\uff0c\u5219\u7f3a\u7701\u6309\u7167\u65b9\u6cd5\u7684\u6240\u6709\u53c2\u6570\u8fdb\u884c\u7ec4\u5408<\/td>\n@CacheEvict(value=\u201dtestcache\u201d,key=\u201d#userName\u201d)<\/td>\n<\/tr>\n
condition<\/td>\n\u7f13\u5b58\u7684\u6761\u4ef6\uff0c\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u4f7f\u7528 SpEL \u7f16\u5199\uff0c\u8fd4\u56de true \u6216\u8005 false\uff0c\u53ea\u6709\u4e3a true \u624d\u8fdb\u884c\u7f13\u5b58<\/td>\n@CacheEvict(value=\u201dtestcache\u201d,condition=\u201d#userName.length()>2\u201d)<\/td>\n<\/tr>\n
allEntries<\/td>\n\u662f\u5426\u6e05\u7a7a\u6240\u6709\u7f13\u5b58\u5185\u5bb9\uff0c\u7f3a\u7701\u4e3a false\uff0c\u5982\u679c\u6307\u5b9a\u4e3a true\uff0c\u5219\u65b9\u6cd5\u8c03\u7528\u540e\u5c06\u7acb\u5373\u6e05\u7a7a\u6240\u6709\u7f13\u5b58<\/td>\n@CachEvict(value=\u201dtestcache\u201d,allEntries=true)<\/td>\n<\/tr>\n
beforeInvocation<\/td>\n\u662f\u5426\u5728\u65b9\u6cd5\u6267\u884c\u524d\u5c31\u6e05\u7a7a\uff0c\u7f3a\u7701\u4e3a false\uff0c\u5982\u679c\u6307\u5b9a\u4e3a true\uff0c\u5219\u5728\u65b9\u6cd5\u8fd8\u6ca1\u6709\u6267\u884c\u7684\u65f6\u5019\u5c31\u6e05\u7a7a\u7f13\u5b58\uff0c\u7f3a\u7701\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u65b9\u6cd5\u6267\u884c\u629b\u51fa\u5f02\u5e38\uff0c\u5219\u4e0d\u4f1a\u6e05\u7a7a\u7f13\u5b58<\/td>\n@CachEvict(value=\u201dtestcache\u201d\uff0cbeforeInvocation=true)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u6848\u4f8b\uff1a<\/p>\n

service\u5c42\uff1a<\/p>\n

 @CacheEvict(value = \"my-redis-cache2\",allEntries = true)\n    public void clear() {\n        System.out.println(\"\u6e05\u7a7amy-redis-cache2\u7f13\u5b58\u69fd\u4e2d\u7684\u6240\u6709\u5bf9\u8c61....\");\n    }<\/code><\/pre>\n

Test\u7c7b\uff1a<\/p>\n

public void clear() {\n    bookService.clear();\n}<\/code><\/pre>\n

\u6ce8\u610f<\/strong>\uff1a\u9700\u8981\u6d4b\u8bd5\u7684\u8bdd\uff0c\u5148\u5f80\u7f13\u5b58\u4e2d\u7f13\u5b582\u4e2a\u5bf9\u8c61<\/p>\n

\n

\u58f0\u660e\uff1a\u5b66\u4e60\u8bb0\u5f55\uff0c\u82e5\u6709\u9519\u8bef\uff0c\u6b22\u8fce\u6307\u6b63\u3002<\/p>\n

\u53c2\u8003\u535a\u5ba2\uff1aSpringBoot\u4e4bredis\u4e2d\u5e38\u7528\u7684\u6ce8\u89e3_\u5c0f\u5f6d\u4e0d\u4f1a\u79c3\u5934\u7684\u535a\u5ba2-CSDN\u535a\u5ba2<\/p>\n

\u4f5c\u8005\uff1a\u501f\u6211\u4e39\u9752\u5999\u7b14<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"Redis\u7f13\u5b58\u6ce8\u89e3\u672c\u7bc7\u535a\u5ba2\u5c06\u8be6\u7ec6\u8bb2\u89e3@Cacheable,@CachePut,@CacheEvict\u4f7f\u7528\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\u30021\u3001@Cacheable\u4f5c\u7528\u662f\u4e3b\u8981\u9488\u5bf9...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/646"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=646"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/646\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}