国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Spring cache整合redis代碼實例

瀏覽:20日期:2023-09-07 16:37:23

Spring-Cache是Spring3.1引入的基于注解的緩存技術,本質上它并不是一個具體的緩存實現,而是一個對緩存使用的抽象,通過Spring AOP技術,在原有的代碼上添加少量的注解來實現將這個方法轉成緩存方法的效果。

本來想來個分析源碼,奈何水平有限,先從實戰搞起。

先引入依賴:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.1.6.RELEASE</version></dependency><dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.3</version></dependency>

redis配置:

server: port: 8000spring: redis: host: 23.95.x.x port: 6379 timeout: 20s database: 0 jedis: pool: max-active: 5 max-idle: 3 max-wait: 5s password: testtest

配置類:

package me.yanand.config;import org.springframework.cache.annotation.EnableCaching;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.cache.RedisCacheConfiguration;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.RedisSerializationContext;import org.springframework.data.redis.serializer.StringRedisSerializer;import java.time.Duration;@Configuration@EnableCachingpublic class RedisConfig{ private Duration timeOut = Duration.ofMinutes(30); @Bean public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()//設置緩存超時時間 30分鐘.entryTtl(timeOut)//設置key序列化方式.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))//設置value序列化方式.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())).disableCachingNullValues(); return RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(config).transactionAware().build(); }}

主要看@EnableCaching注解,這個注解引入了@Import(CachingConfigurationSelector.class),通過CachingConfigurationSelector把代理創建類、CacheInterceptor、CacheOperationSource、BeanFactoryCacheOperationSourceAdvisor注入到容器,spring通過CacheInterceptor攔截器攔截相關帶有@Cacheable、@CacheEvict、@CachePut注解的方法并執行相關緩存操作。

CacheInterceptor相關源碼:

@Nullableprivate Object execute(final CacheOperationInvoker invoker, Method method, CacheOperationContexts contexts) { if (contexts.isSynchronized()) { CacheOperationContext context = contexts.get(CacheableOperation.class).iterator().next(); //滿足條件執行 if (isConditionPassing(context, CacheOperationExpressionEvaluator.NO_RESULT)) { Object key = generateKey(context, CacheOperationExpressionEvaluator.NO_RESULT); Cache cache = context.getCaches().iterator().next(); try { //這里主要看RedisCache的get方法 return wrapCacheValue(method, cache.get(key, () -> unwrapReturnValue(invokeOperation(invoker)))); } catch (Cache.ValueRetrievalException ex) { // The invoker wraps any Throwable in a ThrowableWrapper instance so we // can just make sure that one bubbles up the stack. throw (CacheOperationInvoker.ThrowableWrapper) ex.getCause(); } } else { //不滿足直接執行相關方法 return invokeOperation(invoker); } } ...省略}

RedisCache相關代碼:

public synchronized <T> T get(Object key, Callable<T> valueLoader) { ValueWrapper result = get(key);//緩存中有值則返回 if (result != null) { return (T) result.get(); }//緩存中不存在則執行相關方法 T value = valueFromLoader(key, valueLoader); put(key, value); return value; }

注解使用:

package me.yanand.dao;import me.yanand.pojo.User;import org.springframework.cache.annotation.CacheEvict;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Component;@Componentpublic class UserDao { @Cacheable(cacheNames = 'users',key = '#root.targetClass+#name', unless = '#result eq null') public User getUser(String name){ return new User('張三',30); } @CacheEvict(cacheNames = 'users', key = '#root.targetClass+#name') public void delUser(String name){ }}

測試:

Spring cache整合redis代碼實例

通過postman觸發相關方法,現在我們連上redis查看緩存寫入情況

Spring cache整合redis代碼實例

這里我們看到key已經寫入,過期時間也存在

現在我們刪除緩存

Spring cache整合redis代碼實例

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 亚洲精品欧洲一区二区三区 | 国产成人cao在线 | 精品无码久久久久国产 | 国产成人理在线观看视频 | 亚洲日本aⅴ片在线观看香蕉 | 久久99热精品免费观看欧美 | 免费a级在线观看播放 | 亚洲精品日韩在线一区 | 国产欧美日韩中文久久 | 国产亚洲小视频 | 久久视频精品线视频在线网站 | 国产日本韩国不卡在线视频 | 欧美另类精品 | 全部免费a级毛片 | 成人在线免费观看网站 | 亚洲免费视频在线 | 德国女人一级毛片免费 | 欧美高清一区二区 | 久久影院在线 | 国内精品久久影院 | 在线成人免费看大片 | 欧美成人午夜毛片免费影院 | 一区二区三区在线视频观看 | 日韩毛片大全免费高清 | 性生活免费视频网站 | 80日本xxxxxxxxx| 免费一级毛片在级播放 | 国产高清在线精品一区 | 国产日韩欧美在线一二三四 | 国产精亚洲视频 | 一级高清毛片免费a级高清毛片 | 欧美精品一区二区三区在线 | 亚洲成人7777 | 日本三级s级在线播放 | 午夜日韩精品 | 日本午夜高清视频 | 亚洲成a人v| 韩日精品 | 欧美一级日韩在线观看 | 日韩在线一区二区三区免费视频 | 久热草在线 |