redis二级缓存设置-SpringBoot

1.pom.xml导入依赖

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
</dependency>

2.bootstrap.yml开启缓存,配置数据库和redis基本信息

mybatis:
  configuration:
    cache-enabled: true
datasource:
  druid:
spring:
  redis:

3.mapper.xml添加缓存映射

<mapper ......>

        <cache type=".....RedisCache"/>

或者

mapper.class

@CacheNamespace(implementation = RedisCache.class)

4.RedisCache.class缓存操作类

public class RedisCache implements Cache {
    private static final Logger logger = LoggerFactory.getLogger(RedisCache.class);
    //缓存对象唯一标识
    private final String id; 
    //用于事务性缓存操作的读写锁
    private static ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); 
    //操作数据缓存
    private  RedisTemplate redisTemplate;  
    //缓存对象的是失效时间,30分钟
    private static final long EXPRIRE_TIME_IN_MINUT = 30;
    //构造方法---把对象唯一标识传进来
    public RedisCache(String id){
        if(id == null){
            throw new IllegalArgumentException("缓存对象id是不能为空的");
        }
        this.id = id;
    }
    @Override
    public String getId() {
        return this.id;
    }
    //给模板对象RedisTemplate赋值,并传出去
    private RedisTemplate getRedisTemplate(){
        if(redisTemplate == null){    //每个连接池的连接都要获得RedisTemplate
            redisTemplate = ApplicationContextHolder.getBean("redisTemplate");
        }
        return redisTemplate;
    }
    //保存缓存对象的方法
    @Override
    public void putObject(Object key, Object value) {
        try{
            RedisTemplate redisTemplate = getRedisTemplate();
            //使用redisTemplate得到值操作对象
            ValueOperations operation = redisTemplate.opsForValue();
            //使用值操作对象operation设置缓存对象
            operation.set(key,value,EXPRIRE_TIME_IN_MINUT, TimeUnit.MINUTES);  
            logger.debug("缓存对象保存成功");
        }catch (Throwable t){
            logger.error("缓存对象保存失败"+t);
        } 
    }
    //获取缓存对象的方法
    @Override
    public Object getObject(Object key) {
        try {
            RedisTemplate redisTemplate = getRedisTemplate();
            ValueOperations operations = redisTemplate.opsForValue();
            Object result = operations.get(key);
            logger.debug("获取缓存对象");
            return result;
        }catch (Throwable t){
            logger.error("缓存对象获取失败"+t);
            return null;
        }
    }
    //删除缓存对象
    @Override
    public Object removeObject(Object key) {
        try{
            RedisTemplate redisTemplate = getRedisTemplate();
            redisTemplate.delete(key);
            logger.debug("删除缓存对象成功!");
        }catch (Throwable t){
            logger.error("删除缓存对象失败!"+t);
        }
        return null;
    }
    //清空缓存对象
    //当缓存的对象更新了的化,就执行此方法
    @Override
    public void clear() {
        RedisTemplate redisTemplate = getRedisTemplate();
        //回调函数
        redisTemplate.execute((RedisCallback)collection->{
            collection.flushDb();
            return  null;
        });
        logger.debug("清空缓存对象成功!");
    }
    //可选实现的方法
    @Override
    public int getSize() {
        return 0;
    } 
    @Override
    public ReadWriteLock getReadWriteLock() {
        return readWriteLock;
    }
 }

5.ApplicationContextHolder注入缓存Bean

@Component
public class ApplicationContextHolder implements ApplicationContextAware {
    private static ApplicationContext ctx;
    @Override
    //向工具类注入applicationContext
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ctx = applicationContext; 
    }
    public static ApplicationContext getCtx(){
        return ctx;
    }
    public static <T> T getBean(Class<T> tClass){
        return ctx.getBean(tClass);
    }
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name){
        return (T) ctx.getBean(name);
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章