版权声明:[ws - 兮的博客] - 空间专属,未经声明不得私自转载 https://blog.csdn.net/qq_41463655/article/details/82320998
缓存说明:缓存中有查询的数据,就缓存中取,没有就数据库中取并同步缓存
顺序: 查询 ---> 二级缓存 ---> 一级缓存 ---> 数据库
------------------------- 一级缓存 -----------------------------
***
一级缓存是 session 级别的
默认开启
无法配置
数据的变更 会更新缓存 并清除之前 session 的缓存
------------------------- 二级缓存 -----------------------------
sqlMapConfig.xml 配置文件中
<settings>
<!-- 延迟加载的总开关-->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- aggressiveLazyLoading 设置成false才是启用延迟加载 -->
<setting name="aggressiveLazyLoading" value="false"/>
<!-- 开启二级缓存,在mybatis中只要缓存的配置都指的是二级缓存 -->
<setting name="cacheEnabled" value="true"/>
</settings>
对应映射文件中
implements Serializable 对应的实体类继承Serializable 生成 uuid
<!-- 当前映射文件开启二级缓存 -- 使用ehcache框架
不设置type默认使用自带二级缓存策略 -->
<!-- <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> -->
了解即可:
select:useCache:是否启用二级缓存true是启用false是禁用,默认是true
select:flushCache:是否刷新缓存(清掉缓存)true是刷新 false不刷新,默认是true
insert:flushCache:是否刷新缓存(清掉缓存)true是刷新 false不刷新,默认是true
update:flushCache:是否刷新缓存(清掉缓存)true是刷新 false不刷新,默认是true
创建 ehcache.xml 二级缓存配置文件
## 需要ehcache依赖包 mybatis-ehcache-1.0.2.jar + ehcache-core-2.6.5.jar ##
<ehcache>
<!-- Sets the path to the directory where cache .data files are created.
If the path is a Java System Property it is replaced by
its value in the running VM.
The following properties are translated:
user.home - User's home directory
user.dir - User's current working directory
java.io.tmpdir - Default temp file path -->
<diskStore path="java.io.tmpdir"/>
<!--Default Cache configuration. These will applied to caches programmatically created through
the CacheManager.
The following attributes are required for defaultCache:
maxInMemory - Sets the maximum number of objects that will be created in memory
eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
is never expired.
timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
if the element is not eternal. Idle time is now - last accessed time
timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
if the element is not eternal. TTL is now - creation time
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
has reached the maxInMemory limit.
-->
<!--
maxElementsInMemory:缓存对象个数
eternal:缓存是否永久有效
timeToIdleSeconds:闲置时间
timeToLiveSeconds:有效时间
overflowToDisk:内存满了就储在硬盘
MaxElementsOnDisk:硬盘上的最大时间
diskPersisent:硬盘上是否永久有效
memoryStoreEvctionPolicy:LRC(最近最少使用),FIFO(先进先出)
-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
<!--Predefined caches. Add your cache configuration settings here.
If you do not have a configuration for your cache a WARNING will be issued when the
CacheManager starts
The following attributes are required for defaultCache:
name - Sets the name of the cache. This is used to identify the cache. It must be unique.
maxInMemory - Sets the maximum number of objects that will be created in memory
eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
is never expired.
timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
if the element is not eternal. Idle time is now - last accessed time
timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
if the element is not eternal. TTL is now - creation time
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
has reached the maxInMemory limit.
-->
<!-- Sample cache named sampleCache1
This cache contains a maximum in memory of 10000 elements, and will expire
an element if it is idle for more than 5 minutes and lives for more than
10 minutes.
If there are more than 10000 elements it will overflow to the
disk cache, which in this configuration will go to wherever java.io.tmp is
defined on your system. On a standard Linux system this will be /tmp"
-->
<cache name="sampleCache1"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/>
<!-- Sample cache named sampleCache2
This cache contains 1000 elements. Elements will always be held in memory.
They are not expired. -->
<cache name="sampleCache2"
maxElementsInMemory="1000"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
/> -->
<!-- Place configuration for your caches following -->
</ehcache>