一。本地缓存概述
本地缓存的作用:
1.减少redis的访问压力
2.加快程序的访问速度
二。本地缓存ehcache使用步骤
1.pom添加依赖
<!--本地缓存ehcache-->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.9.2</version>
</dependency>
2.application.yml添加缓存配置文件cache.xml(放在resources目录下) 默认resources目录好像也可以不写
spring:
cache:
ehcache:
config: ehcache.xml
3.ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<!--本地缓存如果超过指定大小会存储在文件中,文件路径在这里指定,如果部署linux环境就是 path="/home/cache"-->
<diskStore path="Java.io.tmpdir"/>
<!--
eternal="false"意味着该缓存会死亡
maxElementsInMemory="900"缓存的最大数目
overflowToDisk="false" 内存不足时,是否启用磁盘缓存,如果为true则表示启动磁盘来存储,如果为false则表示不启动磁盘
diskPersistent="false"
timeToIdleSeconds="0" 当缓存的内容闲置多少时间销毁
timeToLiveSeconds="60" 当缓存存活多少时间销毁(单位是秒,如果我们想设置2分钟的缓存存活时间,那么这个值我们需要设置120)
memoryStoreEvictionPolicy="LRU" /> 自动销毁策略
-->
<!--
name:缓存名称。
maxElementsInMemory:缓存最大个数。
eternal:对象是否永久有效,一但设置了,timeout将不起作用。
timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。
diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
maxElementsOnDisk:硬盘最大缓存个数。
diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
clearOnFlush:内存数量最大时是否清除。
-->
<defaultCache eternal="false"
maxElementsInMemory="900"
overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
timeToLiveSeconds="60"
memoryStoreEvictionPolicy="LRU"/>
<!-- 这里的 users 缓存空间是为了下面的 demo 做准备 -->
<cache
name="killgoodDetail"
eternal="false"
maxElementsInMemory="5000"
overflowToDisk="true"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="86400"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
<!--<diskStore>==========当内存缓存中对象数量超过maxElementsInMemory时,将缓存对象写到磁盘缓存中(需对象实现序列化接口) -->
<!--<diskStore path="">==用来配置磁盘缓存使用的物理路径,Ehcache磁盘缓存使用的文件后缀名是*.data和*.index -->
<!--name=================缓存名称,cache的唯一标识(ehcache会把这个cache放到HashMap里) -->
<!--maxElementsOnDisk====磁盘缓存中最多可以存放的元素数量,0表示无穷大 -->
<!--maxElementsInMemory==内存缓存中最多可以存放的元素数量,若放入Cache中的元素超过这个数值,则有以下两种情况 -->
<!--1)若overflowToDisk=true,则会将Cache中多出的元素放入磁盘文件中 -->
<!--2)若overflowToDisk=false,则根据memoryStoreEvictionPolicy策略替换Cache中原有的元素 -->
<!--eternal==============缓存中对象是否永久有效,即是否永驻内存,true时将忽略timeToIdleSeconds和timeToLiveSeconds -->
<!--timeToIdleSeconds====缓存数据在失效前的允许闲置时间(单位:秒),仅当eternal=false时使用,默认值是0表示可闲置时间无穷大,此为可选属性 -->
<!--即访问这个cache中元素的最大间隔时间,若超过这个时间没有访问此Cache中的某个元素,那么此元素将被从Cache中清除 -->
<!--timeToLiveSeconds====缓存数据在失效前的允许存活时间(单位:秒),仅当eternal=false时使用,默认值是0表示可存活时间无穷大 -->
<!--即Cache中的某元素从创建到清楚的生存时间,也就是说从创建开始计时,当超过这个时间时,此元素将从Cache中清除 -->
<!--overflowToDisk=======内存不足时,是否启用磁盘缓存(即内存中对象数量达到maxElementsInMemory时,Ehcache会将对象写到磁盘中) -->
<!--会根据标签中path值查找对应的属性值,写入磁盘的文件会放在path文件夹下,文件的名称是cache的名称,后缀名是data -->
<!--diskPersistent=======是否持久化磁盘缓存,当这个属性的值为true时,系统在初始化时会在磁盘中查找文件名为cache名称,后缀名为index的文件 -->
<!--这个文件中存放了已经持久化在磁盘中的cache的index,找到后会把cache加载到内存 -->
<!--要想把cache真正持久化到磁盘,写程序时注意执行net.sf.ehcache.Cache.put(Element element)后要调用flush()方法 -->
<!--diskExpiryThreadIntervalSeconds==磁盘缓存的清理线程运行间隔,默认是120秒 -->
<!--diskSpoolBufferSizeMB============设置DiskStore(磁盘缓存)的缓存区大小,默认是30MB -->
<!--memoryStoreEvictionPolicy========内存存储与释放策略,即达到maxElementsInMemory限制时,Ehcache会根据指定策略清理内存 -->
<!--共有三种策略,分别为LRU(Least Recently Used 最近最少使用)、LFU(Less Frequently Used最不常用的)、FIFO(first in first out先进先出) -->
4.主启动类添加注解@EnableCaching
@SpringBootApplication
@EnableCaching
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class);
}
}
5.java配置文件
@Configuration
public class EhCacheConfig {
@Primary
@Bean//(name = "ehCacheManager")
public CacheManager cacheManager(EhCacheManagerFactoryBean bean) {
CacheManager cm = bean.getObject();
return cm;
}
@Bean
public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {
EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean();
cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
cacheManagerFactoryBean.setShared(true);
// 设置完属性后,cacheManagerFactoryBean会执行afterProertiesSet()方法,
// 所以不能在这里直接执行cacheManagerFactoryBean.getObject(),直接执行的话,因为在afterPropertiesSet()方法之前执行,所以:getObject()会得到null值
return cacheManagerFactoryBean;
}
}
6.代码中使用
public class TestSerice {
@Autowired
private CacheManager cacheManager;
public void test() {
//得到商品详情的缓存管理器(killgoodDetail和xml定义的缓存name 博爱吃一直)
Cache killgoodsCache = cacheManager.getCache("killgoodDetail");
//放置值
killgoodsCache.putIfAbsent(new Element("killgoodDetail","11"));
//查询值
killgoodsCache.get("killgoodDetail");
}
}
三。本地缓存 caffeine
pom引入
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.9.3</version>
</dependency>
版权声明:本文为weixin_44635157原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。