真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

SpringBoot用注解方式整合Redis

1.首先導(dǎo)入使用Maven導(dǎo)入jar包

?

成都創(chuàng)新互聯(lián)公司專業(yè)成都網(wǎng)站制作、做網(wǎng)站,集網(wǎng)站策劃、網(wǎng)站設(shè)計(jì)、網(wǎng)站制作于一體,網(wǎng)站seo、網(wǎng)站優(yōu)化、網(wǎng)站營銷、軟文發(fā)布平臺等專業(yè)人才根據(jù)搜索規(guī)律編程設(shè)計(jì),讓網(wǎng)站在運(yùn)行后,在搜索中有好的表現(xiàn),專業(yè)設(shè)計(jì)制作為您帶來效益的網(wǎng)站!讓網(wǎng)站建設(shè)為您創(chuàng)造效益。

????
????org.springframework.boot????
????spring-boot-starter-data-redis


????com.alibaba
????fastjson
????1.2.62

?

2.在application.properties配置信息

?

#?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

?

3.編寫Redis工具類

?

@Configuration
@ConditionalOnClass(RedisOperations.class)??????//系統(tǒng)中有RedisOperations類時(shí)
@EnableConfigurationProperties(RedisProperties.class)??//啟動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);
????}

}

?

4.SpringBoot有關(guān)緩存的幾個(gè)注解

@Cacheable:查詢

可選屬性:

cacheNames/value:指定緩存組件的名字;

key:緩存數(shù)據(jù)使用的key,可以用來指定。默認(rèn)即使用方法參數(shù)的值

keyGenerator:key的生成器,可以自己指定key的生成器的組件id

SpringBoot 用注解方式 整合 Redis

//自定義配置類配置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()?+"]";
????????????}
????????};
????}
}

SpringBoot 用注解方式 整合 Redis

cacheManager:指定緩存管理器;或者cacheResolver獲取指定解析器

condition:指定符合條件的情況下才緩存;如condition="#id>0"

unless:否定緩存,當(dāng)unless指定的條件為true,方法的返回值不會被緩存,可以獲取到結(jié)果進(jìn)行判斷;如unless="#result==null";

sync:是否使用異步模式

例如:

SpringBoot 用注解方式 整合 Redis

@Cacheable(value?=?"RedisInfo",?key?=?"#root.methodName+'['+#account+']'")
@ResponseBody
@RequestMapping("/RedisTest")
public?Result?findUserOrder(String?account)?throws?Exception{
????if(account!=null)?{
????????List>?list=orderFindGoods.findUserOrder(account);
????????return?Results.successWithData(list,?BaseEnums.SUCCESS.code(),?BaseEnums.SUCCESS.desc());
????}else?{
????????return?Results.failure();
????}
}

SpringBoot 用注解方式 整合 Redis

運(yùn)行項(xiàng)目查看結(jié)果

1.第一次訪問

SpringBoot 用注解方式 整合 Redis

?

?

?查看Druid連接信息

?SpringBoot 用注解方式 整合 Redis

?


網(wǎng)站題目:SpringBoot用注解方式整合Redis
文章轉(zhuǎn)載:http://weahome.cn/article/ieiedj.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部