1.maven添加redis依赖
org.springframework.boot
spring-boot-starter-data-redis
redis:
host: 127.0.0.1
port: 6379
timeout: 1000
pool:
max-active: 8
min-idle: 0
max-idle: 8
max-wait: -1
# Redis数据库索引(默认为0)
#spring.redis.database=0
# Redis服务器地址
#spring.redis.host=127.0.0.1
# Redis服务器连接端口
#spring.redis.port=6379
# Redis服务器连接密码(默认为空)
#spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
#spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
#spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
#spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
#spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
#spring.redis.timeout=1000
3.重写CacheManager,RedisTemplate
CacheManager中设置setDefaultExpiration缓存失效时间 貌似无效。
package com.imm.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
@Configuration
//@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 3600 * 12)
@EnableCaching
public class RedisAutoConfig extends CachingConfigurerSupport {
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
//设置缓存过期时间
//Map expires = new HashMap<>();
//expires.put("12h",3600 * 12L);
//expires.put("1h",3600 * 1L);
//expires.put("10m",60 * 5L);
//rcm.setExpires(expires);
//rcm.setDefaultExpiration(10); //秒
return rcm;
}
/*@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(factory);
RedisSerializer stringRedisSerializer = new StringRedisSerializer();//Long类型不可以会出现异常信息;
redisTemplate.setKeySerializer(stringRedisSerializer);
return redisTemplate;
}*/
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate
/**
* redis 关于LIST 的操作
* @param map
* @param isSql 1.使用sql 0.不使用sql
* @return
*/
public List
/**
* redis 关于MAP 的操作
* @param map
* @param isSql 1.使用sql 0.不使用sql
* @return
*/
public Map doRedisMap(Map map,int isSql){
// 从缓存中通过key获取信息
String key = "";
if(map.get("cacheKey")!=null){
key = map.get("cacheKey").toString();
}
Map newmap = new HashMap();
newmap = redisTemplate.opsForHash().entries(key);
// 缓存存在
long cacheSize = redisTemplate.opsForHash().size(key);
if (cacheSize>0) {
return newmap;
}
//判读获取list的方法
if(isSql==0){
newmap = sysdao.getMapById(map);
}else if(isSql==1){
newmap = sysdao.getMapBySql(map);
}
if(newmap !=null && newmap.size()>0){
// 插入缓存
redisTemplate.opsForHash().putAll(key,newmap);
//设置缓存失效时间
redisTemplate.expire(key, RedisTime.EXPIRETIME, TimeUnit.SECONDS);
}
return newmap;
}
6.RedisTemplate 删除缓存
/**
* redis 删除 操作
* 此处除了删除单条信息及对应缓存、包括父级所有缓存
* @param map
* @return
*/
public void doRedisDelete(Map map){
// 从缓存中通过key获取信息
String key = ""; //当前key
String keys = ""; //父一级key的前缀
if(map.get("cacheKey")!=null){
key = map.get("cacheKey").toString();
String[] keyArr = key.split("_");
keys = keyArr[0]+"_List";
}
boolean hasKey = redisTemplate.hasKey(key);
if (hasKey) {
redisTemplate.delete(key);
}
//获取父一级所有key
Set keySet = redisTemplate.keys(keys + "*");
redisTemplate.delete(keySet);
}
8. 存入非string类型的值,可能会出现\xAC\xED\x00\x05t\x00\x0。对取值数据没有影响。
9. 参考
https://www.cnblogs.com/EasonJim/p/7803067.html#autoid-2-6-0