Skip to content

Latest commit

 

History

History
59 lines (50 loc) · 2.17 KB

README-CN.org

File metadata and controls

59 lines (50 loc) · 2.17 KB

English Readme

mybatis redis 缓存

1.引入包

<dependency>
    <groupId>com.github.liuanxin</groupId>
    <artifactId>mybatis-redis-cache</artifactId>
    <version>1.2.7</version>
</dependency>

2.把一个类放入 spring 的上下文

import com.github.liuanxin.caches.MybatisRedisCache;
import com.github.liuanxin.caches.RedisContextUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

@Configuration
@ConditionalOnClass({ MybatisRedisCache.class, RedisTemplate.class })
public class MybatisCacheConfig {
    @Bean
    public RedisContextUtils setupCacheContext() {
        return new RedisContextUtils();
    }
}

3.在配置文件中设置缓存

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xxx">
    <!--
        type : 基础缓存类型, 默认是 PerpetualCache(就是一个 HashMap)
        flushInterval : 缓存刷新间隔. 单位: 毫秒. 默认情况是不设置, 也就是没有刷新间隔, 缓存仅仅调用语句时刷新
        size : 缓存条数, 默认是 1024
        readOnly : 是否只读, 默认是 false
        blocking : 操作时是否阻塞, 默认是 false
        eviction : 排除算法缓存类型. 默认是 LRU
            LRU   Least Recently Used 最近最少使用的: 移除最长时间不被使用的对象
            FIFO  First In First Out 先进先出: 按对象进入缓存的顺序来移除它们
            SOFT  软引用: 移除基于垃圾回收器状态和软引用规则的对象
            WEAK  弱引用: 更积极地移除基于垃圾收集器状态和弱引用规则的对象
    -->
    <cache type="com.github.liuanxin.caches.MybatisRedisCache" />

    ......
</mapper>

或者在持久层标 @CacheNamespace(implementation = MybatisRedisCache.class) 注解

存入缓存的实体需要实现 java.io.Serializable 接口