亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Spring cache整合redis代碼實(shí)例

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

Spring-Cache是Spring3.1引入的基于注解的緩存技術(shù),本質(zhì)上它并不是一個(gè)具體的緩存實(shí)現(xiàn),而是一個(gè)對(duì)緩存使用的抽象,通過(guò)Spring AOP技術(shù),在原有的代碼上添加少量的注解來(lái)實(shí)現(xiàn)將這個(gè)方法轉(zhuǎn)成緩存方法的效果。

本來(lái)想來(lái)個(gè)分析源碼,奈何水平有限,先從實(shí)戰(zhàn)搞起。

先引入依賴(lài):

<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

配置類(lèi):

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()//設(shè)置緩存超時(shí)時(shí)間 30分鐘.entryTtl(timeOut)//設(shè)置key序列化方式.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))//設(shè)置value序列化方式.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())).disableCachingNullValues(); return RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(config).transactionAware().build(); }}

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

CacheInterceptor相關(guān)源碼:

@Nullableprivate Object execute(final CacheOperationInvoker invoker, Method method, CacheOperationContexts contexts) { if (contexts.isSynchronized()) { CacheOperationContext context = contexts.get(CacheableOperation.class).iterator().next(); //滿(mǎn)足條件執(zhí)行 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 { //不滿(mǎn)足直接執(zhí)行相關(guān)方法 return invokeOperation(invoker); } } ...省略}

RedisCache相關(guān)代碼:

public synchronized <T> T get(Object key, Callable<T> valueLoader) { ValueWrapper result = get(key);//緩存中有值則返回 if (result != null) { return (T) result.get(); }//緩存中不存在則執(zhí)行相關(guān)方法 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){ }}

測(cè)試:

Spring cache整合redis代碼實(shí)例

通過(guò)postman觸發(fā)相關(guān)方法,現(xiàn)在我們連上redis查看緩存寫(xiě)入情況

Spring cache整合redis代碼實(shí)例

這里我們看到key已經(jīng)寫(xiě)入,過(guò)期時(shí)間也存在

現(xiàn)在我們刪除緩存

Spring cache整合redis代碼實(shí)例

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 京野结衣免费一区二区 | 欧美黑人特大巨黑吊 | 国产亚洲欧美在线观看的 | 亚洲精品成人一区 | 99视频精品免视3 | 成人免费动作大片黄在线 | 麻豆成人传媒一区二区 | 国产www| 激情综合婷婷 | 亚洲女初尝黑人巨高清在线观看 | 国产中文字幕第一页 | 国产99欧美精品久久精品久久 | 成人精品在线视频 | 在线你懂 | 中文字幕在线视频播放 | 日本特级爽毛片叫声 | 韩国毛片在线观看 | 国产精品视频无圣光一区 | 欧美一级黄色录相 | 欧美aaaaa一级毛片在线 | a级黄色网址 | 精品国产丝袜自在线拍国 | 久久视频6免费观看视频精品 | 色盈盈影院 | 黄网站在线观看永久免费 | 色视频免费网站 | 色婷婷狠狠干 | 一级爱做片免费观看久久 | 国产精品亚洲综合久久小说 | 国产videos| 国产综合视频 | 日韩黄色网页 | 亚洲欧洲日韩 | 欧美黑人成人www在线观看 | 欧美夜夜片a | 亚洲欧美小说色综合小 | 欧美黄色影视 | 久久综合中文字幕一区二区三区 | 国产福利小视频 | 国产毛片一级 | 日韩精品免费 |