Mybatis 使用Redis 作为 二级缓存

Mybatis 开启二级缓存,使用 Redis 去实现 cache 进行缓存处理,这样就不用一直去请求数据库,降低数据库的压力,并且查询也会快一点
设置mybatis 开启二级缓存

<setting name="cacheEnabled" value="true"/> 

Redis 工具类:

public class JedisPoolUtil {

    private JedisPoolUtil() {
    }

    public static volatile JedisPool jedisPool = null;


    public static JedisPool getJedisPoolInstance(){
        if(null == jedisPool){
            synchronized (JedisPoolUtil.class) {
                if(null == jedisPool){
                    JedisPoolConfig poolConfig = new JedisPoolConfig();
//                    poolConfig.setMaxActive(1000);
                    poolConfig.setMaxIdle(32);
//                    poolConfig.setMaxWait(1000*100);
                    poolConfig.setTestOnBorrow(true);
                    jedisPool = new JedisPool(poolConfig ,"127.0.0.1",6379,100,"root");
                }
            }
        }
        return jedisPool;
    }
}

RedisCache: 实现cache


public class RedisCache implements Cache {
    private static final Logger logger = LoggerFactory.getLogger(RedisCache.class);

    private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
    // cache instance id
    private final String id;


    public RedisCache(String id) {
        this.id = id;
    }


    @Override
    public String getId() {
        return id;
    }





    @Override
    public void putObject(Object key, Object value) {
        try {
            JedisPool jedisPoolInstance = JedisPoolUtil.getJedisPoolInstance();
            logger.debug("put cached query result from redis");
           jedisPoolInstance.getResource().set(key.toString(),value.toString());
        } catch (Throwable t) {
            logger.error("Redis put failed, fail over to db", t);
        }
    }

    @Override
    public Object getObject(Object key) {
        try {
            JedisPool jedisPoolInstance = JedisPoolUtil.getJedisPoolInstance();
            logger.debug("Get cached query result from redis");
            return jedisPoolInstance.getResource().get(key.toString());
        } catch (Throwable t) {
            logger.error("Redis get failed, fail over to db", t);
            return null;
        }
    }

    @Override
    public Object removeObject(Object key) {
        try {
            JedisPool jedisPoolInstance = JedisPoolUtil.getJedisPoolInstance();
            logger.debug("remove cached query result from redis");
            return jedisPoolInstance.getResource().del(key.toString());
        } catch (Throwable t) {
            logger.error("Redis remove failed, fail over to db", t);
            return null;
        }
    }


    @Override
    public void clear() throws CacheException {
        try {
            JedisPool jedisPoolInstance = JedisPoolUtil.getJedisPoolInstance();
            logger.debug("clear cached query result from redis");
            jedisPoolInstance.getResource().flushDB();
        } catch (Throwable t) {
            logger.error("Redis clear failed, fail over to db", t);
        }
    }

    @Override
    public int getSize() {
        return 0;
    }


    @Override
    public ReadWriteLock getReadWriteLock() {
        return readWriteLock;
    }
}

在xml中使用 :

    <cache type="cn.jeeweb.core.cache.RedisCache">
        <property name="eviction" value="LRU"/>
        <property name="flushInterval" value="60000000"/>
        <property name="size" value="2048"/>
        <property name="readOnly" value="false"/>
    </cache>

最后,在过程中出现过一些小的错误,希望记录下来

Caused by: java.lang.ClassCastException: cn.jeeweb.core.cache.RedisCache cannot be cast to org.apache.ibatis.cache.Cache

这个错误,是因为项目中时用 shiro 做权限框架,导致导入cache包时导入错误,应该导入 org.apache.ibatis.cache.Cache 包才对

Caused by: java.lang.NoSuchMethodException: cn.jeeweb.core.cache.RedisCache.<init>(java.lang.String)

这个是因为cache 需要一个构造id

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值