1.Mybatis整合ehcache
1.1 导入相关依赖
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.0</version>
</dependency>
1.2 核心接口
CacheManager:缓存管理器
Cache:缓存对象,缓存管理器内可以放置若干cache,存放数据的实质,所有cache都实现了Ehcache接口
Element:单条缓存数据的组成单位
1.3 src:ehcache.xml
hibernate整合ehcache的步奏以及使用 1、导入ehcache依赖、hibernate-ehcache的依赖
2、导入相关配置文件ehcache.xml 3、hibernate。cfg.xml中开启二级缓存(三个配置)
4、在需要缓存的实体类xxx.hbm.xml 配置加上 默认可以缓存单条记录 当要缓存导条记录的时候,需要代码手动开启二级缓存
Query.setcacheAble(true)
ssm中整合ehcache
(1) 导入相关依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!--mybatis与ehcache整合-->
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.1.0</version>
</dependency>
<!--ehcache依赖-->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.0</version>
</dependency>
(2) 修改日志配置,因为ehcache使用了Slf4j作为日志输出
日志我们使用slf4j,并用log4j来实现。SLF4J不同于其他日志类库,与其它有很大的不同。
SLF4J(Simple logging Facade for Java)不是一个真正的日志实现,而是一个抽象层( abstraction layer),
它允许你在后台使用任意一个日志类库。
<!-- log4j2日志配置相关依赖 -->
<log4j2.version>2.9.1</log4j2.version>
<log4j2.disruptor.version>3.2.0</log4j2.disruptor.version>
<slf4j.version>1.7.13</slf4j.version>
<!-- log4j2日志相关依赖 -->
<!-- log配置:Log4j2 + Slf4j -->
<!-- slf4j核心包-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
<scope>runtime</scope>
</dependency>
<!--核心log4j2jar包-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
<!--用于与slf4j保持桥接-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j2.version}</version>
</dependency>
<!--web工程需要包含log4j-web,非web工程不需要-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>${log4j2.version}</version>
<scope>runtime</scope>
</dependency>
<!--需要使用log4j2的AsyncLogger需要包含disruptor-->
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>${log4j2.disruptor.version}</version>
</dependency>
(3) 在Resource中添加一个ehcache.xml的配置文件
(4) 在applicationContext.xml中加入chache配置
<!-- 使用ehcache缓存 -->
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
<property name="shared" value="true"></property>
</bean>
<!-- 默认是cacheManager -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="cacheManagerFactory"/>
</bean>
(5)mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓存
(5.1)开启mybatis的二级缓存
<!--设置mybaits对缓存的支持-->
<property name="configurationProperties">
<props>
<!-- 全局映射器启用缓存 *主要将此属性设置完成即可-->
<prop key="cacheEnabled">true</prop>
<!-- 查询时,关闭关联对象即时加载以提高性能 -->
<prop key="lazyLoadingEnabled">false</prop>
<!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指 定),不会加载关联表的所有字段,以提高性能 -->
<prop key="aggressiveLazyLoading">true</prop>
</props>
</property>
(5.2) 在XxxMapper.xml中配置cache
<!-- 以下两个<cache>标签二选一,第一个可以输出日志,第二个不输出日志 -->
<!--<cache type="org.mybatis.caches.ehcache.LoggingEhcache" />-->
<!--<cache type="org.mybatis.caches.ehcache.EhcacheCache" />-->
<!--eviction="FIFO" 回收策略为先进先出-->
<!--flushInterval="60000" 自动刷新时间60s-->
<!--size="512" 最多缓存512个引用对象-->
<!--readOnly="true" 只读-->
<cache type="org.mybatis.caches.ehcache.EhcacheCache">
<property name="timeToIdleSeconds" value="3600"/>
<property name="timeToLiveSeconds" value="3600"/>
<property name="maxEntriesLocalHeap" value="1000"/>
<property name="maxEntriesLocalDisk" value="10000000"/>
<property name="memoryStoreEvictionPolicy" value="LRU"/>
</cache>
(5.3) 可以通过select标签的useCache属性打开或关闭二级缓存
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" useCache="false"></select>
2.Mybatis整合Redis
注意事项:
1、spring多配置文件的引入方式
2、pom中对properties文件的编译
3、加一个spring-xxx.xml一定记得在spring-Context.xml导入
(1) redis常用类
(1.1) Jedis
jedis就是集成了redis的一些命令操作,封装了redis的java客户端
(1.2) JedisPoolConfig
Redis连接池
(1.3) ShardedJedis
基于一致性哈希算法实现的分布式Redis集群客户端
实现 mybatis 的二级缓存,一般来说有如下两种方式: 1) 采用 mybatis 内置的 cache 机制。 2)
采用三方 cache 框架, 比如ehcache, oscache 等等.
(2) 添加jar依赖
(2.1) 添加redis相关依赖
<!-- redis与spring的整合依赖 -->
<redis.version>2.9.0</redis.version>
<redis.spring.version>1.7.1.RELEASE</redis.spring.version>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${redis.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${redis.spring.version}</version>
</dependency>
版本冲突问题:
1. ClassNotFoundException : redis/client/util/geoUtils 说这个类找不到。
2. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisTemplate' defined in class path resource [applicationContext.xml]
说创建 redisTemplate bean 对象时失败了。
redis.client 2.9.0 ---- spring-data-redis 1.7.1.RELEASE
redis.client 2.9.0 ---- spring-data-redis 1.7.2.RELEASE 这两个是可以使用的
(2.2) log4j2配置:Log4j2 + Slf4j (如上面整合ehcache相同)
(2.3) jackson
(3)spring + redis 集成实现缓存功能(与mybatis无关)
(3.1) 添加两个redis的配置文件,并将redis.properties和spring-redis.xml配置到applicationContext.xml文件中
注1:将redis.properties导入到applicationContext.xml文件中
spring中引入第二个属性文件会出现“找不到某个配置项”错误,这是因为spring只允许有一个<context:property-placeholder/>
<!--引入一个属性文件的写法-->
<context:property-placeholder ignore-unresolvable="true" location="classpath:jdbc.properties" />
<!--引入两个或多个属性文件的写法-->
<context:property-placeholder ignore-unresolvable="true" location="classpath:jdbc.properties,classpath:redis.properties" />
注2:通过import标签将spring-redis.xml导入到applicationContext.xml文件中
<!--导入redis配置-->
<import resource="spring-redis.xml"/>
(3.2) 将redis缓存引入到mybatis中
a. 创建mybatis的自定义缓存类“RedisCache”,必须实现org.apache.ibatis.cache.Cache接口
RedisCache.java
b. 静态注入中间类“RedisCacheTransfer”,解决RedisCache中RedisTemplate的静态注入,从而使MyBatis实现第三方缓存
RedisCacheTransfer.java
c. spring与mybatis整合文件中开发二级缓存
d. 在XxxMapper.xml中添加自定义cache功能
<cache type="com.zking.zf.redis.RedisCache" eviction="LRU" flushInterval="6000000" size="1024" readOnly="false"/>
type:指定redis的二级缓存类的全路径名
eviction:二级缓存算法(FIFO/LRU/SOFT/WEAK)
flushInterval:刷新间隔,间隔多长时间清空缓存,被动触发非定时器轮询
size:缓存大小。每个缓存可以存储 1024 个列表或对象的引用
readOnly:缓存将作为“读/写”缓存,意味着获取的对象不是共享的且对调用者是安全的。不会有其它的调用者或线程潜在修改。
注意: pom对properties文件的加载!!!
资料:
MyBatis内置的二级缓存算法 Mybatis的所有Cache算法都是基于装饰器模式对PerpetualCache扩展增加功能。
1)FIFO:先入先出,基于LinkedList实现; 2)LRU:最近最少使用,基于LinkedHashMap实现,在put的时候,自动移除最少使用缓存对象;
3)SOFT:对Cache的value进行SoftReference包装;当缓存对象是Soft reference可达时,gc会向系统申请更多内存,而不是直接回收它,当内存不足的时候才回收它;
4)WEAK:对Cache的value进行WeakReference包装;WeakReference不会强制对象保存在内存中。它拥有比较短暂的生命周期,允许你使用垃圾回收器的能力去权衡一个对象的可达性。在垃圾回收器扫描它所管辖的内存区域过程中,一旦gc发现对象是weakReference可达,就会把它放到ReferenceQueue中,等下次gc时回收它。