成都一家集口碑和實(shí)力的網(wǎng)站建設(shè)服務(wù)商,擁有專業(yè)的企業(yè)建站團(tuán)隊(duì)和靠譜的建站技術(shù),10余年企業(yè)及個(gè)人網(wǎng)站建設(shè)經(jīng)驗(yàn) ,為成都上千家客戶提供網(wǎng)頁設(shè)計(jì)制作,網(wǎng)站開發(fā),企業(yè)網(wǎng)站制作建設(shè)等服務(wù),包括成都營銷型網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),同時(shí)也為不同行業(yè)的客戶提供成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)的服務(wù),包括成都電商型網(wǎng)站制作建設(shè),裝修行業(yè)網(wǎng)站制作建設(shè),傳統(tǒng)機(jī)械行業(yè)網(wǎng)站建設(shè),傳統(tǒng)農(nóng)業(yè)行業(yè)網(wǎng)站制作建設(shè)。在成都做網(wǎng)站,選網(wǎng)站制作建設(shè)服務(wù)商就選創(chuàng)新互聯(lián)。
???? ???? org.springframework.boot ???? ????spring-boot-starter-data-redis ???? com.alibaba ????fastjson ????1.2.62
#?Redis數(shù)據(jù)庫索引(默認(rèn)為0)?? spring.redis.database=0?? #?Redis服務(wù)器地址?? spring.redis.host=localhost #?Redis服務(wù)器連接端口?? spring.redis.port=6379?? #?Redis服務(wù)器連接密碼(默認(rèn)為空)?? spring.redis.password=123456 #?連接池最大連接數(shù)(使用負(fù)值表示沒有限制)?? spring.redis.pool.max-active=200?? #?連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒有限制)?? spring.redis.pool.max-wait=-1?? #?連接池中的最大空閑連接?? spring.redis.pool.max-idle=10? #?連接池中的最小空閑連接?? spring.redis.pool.min-idle=0?? #?連接超時(shí)時(shí)間(毫秒)?? spring.redis.timeout=1000ms
@Configuration @ConditionalOnClass(RedisOperations.class)??????//系統(tǒng)中有RedisOperations類時(shí) @EnableConfigurationProperties(RedisProperties.class)??//啟動(dòng)RedisProperties這個(gè)類 @EnableCaching //?www.1b23.com public?class?RedisConfig?extends?CachingConfigurerSupport?{ ????@Autowired ????RedisTemplate?redisTemplate; ????//?配置緩存管理器 ????@Bean ????public?RedisCacheManager?cacheManager(RedisConnectionFactory?connectionFactory)?{ ????????LettuceConnectionFactory?jedisConnectionFactory?=?(LettuceConnectionFactory)?redisTemplate.getConnectionFactory(); ????????jedisConnectionFactory.setDatabase(2);??????//指定dbindex ????????redisTemplate.setConnectionFactory(jedisConnectionFactory); ????????jedisConnectionFactory.resetConnection(); ????????RedisCacheConfiguration?config?=?RedisCacheConfiguration.defaultCacheConfig() ????????????????.entryTtl(Duration.ofSeconds(60*20))?//?20分鐘緩存失效 ????????????????//?設(shè)置key的序列化方式 //????????????????.entryTtl(Duration.ofSeconds(10)) ????????????????.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new?StringRedisSerializer())) ????????????????//?設(shè)置value的序列化方式 ????????????????.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new?FastJsonRedisSerializer(Object.class))) ????????????????//?不緩存null值 ????????????????.disableCachingNullValues(); ????????RedisCacheManager?redisCacheManager?=?RedisCacheManager.builder(connectionFactory) ????????????????.cacheDefaults(config) ????????????????.transactionAware() ????????????????.build(); ????????return?redisCacheManager; ????} }
package?com.FireService.config; import?java.nio.charset.Charset; import?org.springframework.data.redis.serializer.RedisSerializer; import?org.springframework.data.redis.serializer.SerializationException; import?com.alibaba.fastjson.JSON; import?com.alibaba.fastjson.parser.ParserConfig; import?com.alibaba.fastjson.serializer.SerializerFeature; public?class?FastJsonRedisSerializer?implements?RedisSerializer ?{ ????public?static?final?Charset?DEFAULT_CHARSET?=?Charset.forName("UTF-8"); ????private?Class ?clazz; ????static?{ ????????ParserConfig.getGlobalInstance().addAccept("com.FireService"); ????} ????public?FastJsonRedisSerializer(Class ?clazz)?{ ????????super(); ????????this.clazz?=?clazz; ????} ????@Override ????public?byte[]?serialize(T?t)?throws?SerializationException?{ ????????if?(null?==?t)?{ ????????????return?new?byte[0]; ????????} ????????return?JSON.toJSONString(t,?SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET); ????} ????@Override ????public?T?deserialize(byte[]?bytes)?throws?SerializationException?{ ????????if?(null?==?bytes?||?bytes.length?<=?0)?{ ????????????return?null; ????????} ????????String?str?=?new?String(bytes,?DEFAULT_CHARSET); ????????return?(T)?JSON.parseObject(str,?clazz); ????} }
@Cacheable:查詢
可選屬性:
cacheNames/value:指定緩存組件的名字;
key:緩存數(shù)據(jù)使用的key,可以用來指定。默認(rèn)即使用方法參數(shù)的值
keyGenerator:key的生成器,可以自己指定key的生成器的組件id
//自定義配置類配置keyGenerator @Configuration public?class?MyCacheConfig?{ ????@Bean("myKeyGenerator") ????public?KeyGenerator?keyGenerator(){ ????????return?new?KeyGenerator()?{ ????????????@Override ????????????public?Object?generate(Object?target,?Method?method,?Object...?params)?{ ????????????????return?method.getName()+"["+?Arrays.asList(params).toString()?+"]"; ????????????} ????????}; ????} }
cacheManager:指定緩存管理器;或者cacheResolver獲取指定解析器
condition:指定符合條件的情況下才緩存;如condition="#id>0"
unless:否定緩存,當(dāng)unless指定的條件為true,方法的返回值不會(huì)被緩存,可以獲取到結(jié)果進(jìn)行判斷;如unless="#result==null";
sync:是否使用異步模式
例如:
@Cacheable(value?=?"RedisInfo",?key?=?"#root.methodName+'['+#account+']'") @ResponseBody @RequestMapping("/RedisTest") public?Result?findUserOrder(String?account)?throws?Exception{ ????if(account!=null)?{ ????????List
運(yùn)行項(xiàng)目查看結(jié)果
1.第一次訪問
?查看Druid連接信息
?