spring boot注解方式使用redis緩存操作示例
本文實(shí)例講述了spring boot注解方式使用redis緩存操作。分享給大家供大家參考,具體如下:
引入依賴庫
在pom中引入依賴庫,如下
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>
注解使用
@Cacheable @Cacheable("product") @Cacheable(value = {"product","order"}, key = "#root.targetClass+'-'+#id") @Cacheable(value = "product", key = "#root.targetClass+'-'+#id")
自定義cacheManager
@Cacheable(value = "product", key = "#root.targetClass+'-'+#id” cacheManager="cacheManager") @CachePut
應(yīng)用到寫數(shù)據(jù)的方法上,如新增/修改方法
@CachePut(value = "product", key = "#root.targetClass+'-'+#product.id") @CacheEvict
即應(yīng)用到移除數(shù)據(jù)的方法上,如刪除方法
@CacheEvict(value = "product", key = "#root.targetClass+'-'+#id")
提供的SpEL上下文數(shù)據(jù)
Spring Cache提供了一些供我們使用的SpEL上下文數(shù)據(jù),下表直接摘自Spring官方文檔:
名字 | 位置 | 描述 | 示例 |
---|---|---|---|
methodName | root對象 | 當(dāng)前被調(diào)用的方法名 | #root.methodName |
method | root對象 | 當(dāng)前被調(diào)用的方法 | #root.method.name |
target | root對象 | 當(dāng)前被調(diào)用的目標(biāo)對象 | #root.target |
targetClass | root對象 | 當(dāng)前被調(diào)用的目標(biāo)對象類 | #root.targetClass |
args | root對象 | 當(dāng)前被調(diào)用的方法的參數(shù)列表 | #root.args[0] |
caches | root對象 | 當(dāng)前方法調(diào)用使用的緩存列表(如@Cacheable(value={"cache1", "cache2"})),則有兩個(gè)cache | #root.caches[0].name |
argument name | 執(zhí)行上下文 | 當(dāng)前被調(diào)用的方法的參數(shù),如findById(Long id),我們可以通過#id拿到參數(shù) | #user.id |
result | 執(zhí)行上下文 | 方法執(zhí)行后的返回值(僅當(dāng)方法執(zhí)行之后的判斷有效,如‘unless','cache evict'的beforeInvocation=false) | #result |
自定義Cache配置
@Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport { /** * 自定義redis key值生成策略 */ @Bean @Override public KeyGenerator keyGenerator() { return (target, method, params) -> { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append(method.getName()); for (Object obj : params) { sb.append(obj.toString()); } return sb.toString(); }; } @Bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(Object
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
談?wù)凧ava類型中ParameterizedType,GenericArrayType,TypeVariabl,Wild
這篇文章主要介紹Java類型中ParameterizedType,GenericArrayType,TypeVariabl,WildcardType的相關(guān)資料,需要的朋友可以參考下2015-10-10spring cloud Hystrix斷路器的使用(熔斷器)
這篇文章主要介紹了spring cloud Hystrix斷路器的使用(熔斷器),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08SWT(JFace)體驗(yàn)之模擬BorderLayout布局
SWT(JFace)體驗(yàn)之模擬BorderLayout布局代碼。2009-06-06Spring Security靈活的PasswordEncoder加密方式解析
這篇文章主要介紹了Spring Security靈活的PasswordEncoder加密方式解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09