1.pom.
<dependency>
<groupId>org.spring work.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
2.配置文件echache.
<? version=\"1.0\" encoding=\"UTF-8\"?>
<ehcache ns:xsi=\"http://www.w3.org/2001/ Schema-instance\"
xsi:noNamespaceSchemaLocation=\"http://ehcache.org/ehcache.xsd\">
<diskStore path=\"java.io.tmpdir\" />
<defaultCache eternal=\"false\" maxElementsInMemory=\"1000\"
overflowToDisk=\"false\" diskPersistent=\"false\" timeToIdleSeconds=\"0\"
timeToLiveSeconds=\"600\" memoryStoreEvictionPolicy=\"LRU\" />
<cache
name=\"userCache\"
maxElementsInMemory=\"1000\"
eternal=\"false\"
timeToIdleSeconds=\"300\"
timeToLiveSeconds=\"300\"
overflowToDisk=\"false\"
memoryStoreEvictionPolicy=\"LRU\">
<!-- 配置缓存事件监听器 replicateAsynchronously 操作是否异步,默认值为true. replicatePuts 添加操作是否同步到集群内的其他缓存,默认为true.
replicateUpdates 更新操作是否同步到集群内的其他缓存,默认为true. replicateUpdatesViaCopy 更新之后的对象是否复制到集群中的其他缓存(true);
replicateRemovals 删除操作是否同步到集群内的其他缓存,默认为true. -->
<cacheEventListenerFactory
class=\"net.sf.ehcache.distribution.RMICacheReplicatorFactory\"
properties=\"
replicateAsynchronously=true,
replicatePuts=true,
replicateUpdates=true,
replicateUpdatesViaCopy=true,
replicateRemovals=true \" />
<!-- 初始化缓存,以及自动设置 -->
<bootstrapCacheLoaderFactory
class=\"net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory\"
properties=\"bootstrapAsynchronously=true\" />
</cache>
<cache
name=\"listGo\"
maxElementsInMemory=\"1000\"
eternal=\"false\"
timeToIdleSeconds=\"300\"
timeToLiveSeconds=\"300\"
overflowToDisk=\"false\"
memoryStoreEvictionPolicy=\"LRU\">
<!-- 配置缓存事件监听器 replicateAsynchronously 操作是否异步,默认值为true. replicatePuts 添加操作是否同步到集群内的其他缓存,默认为true.
replicateUpdates 更新操作是否同步到集群内的其他缓存,默认为true. replicateUpdatesViaCopy 更新之后的对象是否复制到集群中的其他缓存(true);
replicateRemovals 删除操作是否同步到集群内的其他缓存,默认为true. -->
<cacheEventListenerFactory
class=\"net.sf.ehcache.distribution.RMICacheReplicatorFactory\"
properties=\"
replicateAsynchronously=true,
replicatePuts=true,
replicateUpdates=true,
replicateUpdatesViaCopy=true,
replicateRemovals=true \" />
<!-- 初始化缓存,以及自动设置 -->
<bootstrapCacheLoaderFactory
class=\"net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory\"
properties=\"bootstrapAsynchronously=true\" />
</cache>
</ehcache>
一个cache代表一个Ecache对象的定义,当然可以定义多个cache对象,针对不同的cache进行配置就行了。
3.开启
@SpringBootApplication
@EnableCaching
public class PrintshopwebApplication {
public static void main(String[] args) {
SpringApplication.run(PrintshopwebApplication.class, args);
}
}
4.service层
//查询方法,值注入名为userCache的echache对象中
@Cacheable(key = \"1\" , value = \"userCache\")
public List<Product> selList() {
return productDao.selList();
}
//当执行删除方法是,清空userCache中的缓存内容
@CacheEvict(value=\"userCache\",allEntries=true,beforeInvocation=true)
public int delId(int productId) {
return productDao.delId(productId);
}
//根据id把缓存存到listGo中,缓存是以键值对的形式存在的
@Cacheable(key = \"#productId\" , value = \"listGo\")
public Product selId(int productId) {
return productDao.selId(productId);
}
这部分简单来说,你为了提升用户体验,查询所有产品信息,显示到页面,但是如果后台删除产品信息呢?CacheEvict就是在删除产品的时候,清空了userCache中的所有缓存内容,这样当用户再次刷新页面时,selList()方法就会去数据库查询了
继续阅读与本文标签相同的文章
上一篇 :
放弃手机押注新能源,这次董明珠大概做对了
下一篇 :
腾讯:火锅视频项目组未裁撤,只是并入腾讯视频
-
关注盲人群体 美团推出语音点外卖应用
2026-05-19栏目: 教程
-
卡特马克让制造走向“智造”,百年厨具品牌走向智能化
2026-05-19栏目: 教程
-
拼多多盘初涨逾2%,回应腾讯退出母公司股东行列为正常变化
2026-05-19栏目: 教程
-
Android 10在用户权限方面有何调整?现在真的更安全了吗?
2026-05-19栏目: 教程
-
Alex Kipman:视网膜投影技术将成为AR眼镜最佳代替方案
2026-05-19栏目: 教程
