1.引入Jedis依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.配置Jedis
# Redis數(shù)據(jù)庫索引(默認(rèn)為0)
spring.redis.database=0
# Redis服務(wù)器地址
spring.redis.host=127.0.0.1
# Redis服務(wù)器連接端口
spring.redis.port=6379
# Redis服務(wù)器連接密碼(默認(rèn)為空)
spring.redis.password=******
# 連接池最大連接數(shù)(使用負(fù)值表示沒有限制)
spring.redis.pool.max-active=13
# 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒有限制)
spring.redis.pool.max-wait=-1
# 連接池中的最大空閑連接
spring.redis.pool.max-idle=0
# 連接池中的最小空閑連接
spring.redis.pool.min-idle=0
# 連接超時(shí)時(shí)間(毫秒)
spring.redis.timeout=1800000
3.在@Configuration中配置Jedis
/**
* 緩存管理器
*
* @param redisTemplate
* @return
*/
@Bean
public CacheManager cacheManager(RedisTemplate<?, ?> redisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
// 設(shè)置緩存過期時(shí)間
cacheManager.setDefaultExpiration(10000);
return cacheManager;
}
/**
* RedisTemplate模板配置摩渺,設(shè)置數(shù)據(jù)存入 redis 的序列化方式
*
* @param redisConnectionFactory
* redis連接工廠
* @return RedisTemplate 對(duì)象
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 使用Jackson2JsonRedisSerialize 替換默認(rèn)序列化
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
// 設(shè)置value的序列化規(guī)則和 key的序列化規(guī)則
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
redisTemplate.setEnableDefaultSerializer(true);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
/**
* 實(shí)例化 HashOperations 對(duì)象,可以使用 Hash 類型操作
*
* @param redisTemplate
* @return
*/
@Bean
public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForHash();
}
/**
* 實(shí)例化 ValueOperations 對(duì)象,可以使用 String 操作
*
* @param redisTemplate
* @return
*/
@Bean
public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForValue();
}
/**
* 實(shí)例化 ListOperations 對(duì)象,可以使用 List 操作
*
* @param redisTemplate
* @return
*/
@Bean
public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForList();
}
/**
* 實(shí)例化 SetOperations 對(duì)象,可以使用 Set 操作
*
* @param redisTemplate
* @return
*/
@Bean
public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForSet();
}
/**
* 實(shí)例化 ZSetOperations 對(duì)象,可以使用 ZSet 操作
*
* @param redisTemplate
* @return
*/
@Bean
public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForZSet();
}
4.在Service層使用注解
@CacheConfig(cacheNames = "areaLinkageService") :cacheNames為key的名稱前綴丘跌,areaLinkageService會(huì)轉(zhuǎn)換成Bean的全路徑
@Cacheable(value="findProvince"):聲明使用緩存思恐,value會(huì)和參數(shù)一起做為key,當(dāng)緩存中存在的情況不會(huì)調(diào)用方法涛碑,而是直接從緩存中返回結(jié)果
@CachePut(value="findProvince"):該注解表示每次請(qǐng)求都會(huì)寫入緩存,但是不會(huì)從緩存中取數(shù)據(jù)
@CacheEvict(value="findProvince", allEntries=true) :注解表示清除指定緩存
詳細(xì)屬性參考:https://www.cnblogs.com/fashflying/p/6908028.html
5.在啟動(dòng)類上啟用緩存
@EnableCaching
public class TemplateApplication {...}