java 缓存ech_springboot配置缓存:echcache

1.pom.xml

org.springframework.boot

spring-boot-starter-cache

net.sf.ehcache

ehcache

2.配置文件echache.xml

xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"

timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" />

name="userCache"

maxElementsInMemory="1000"

eternal="false"

timeToIdleSeconds="300"

timeToLiveSeconds="300"

overflowToDisk="false"

memoryStoreEvictionPolicy="LRU">

class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"

properties="

replicateAsynchronously=true,

replicatePuts=true,

replicateUpdates=true,

replicateUpdatesViaCopy=true,

replicateRemovals=true " />

class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"

properties="bootstrapAsynchronously=true" />

name="listGo"

maxElementsInMemory="1000"

eternal="false"

timeToIdleSeconds="300"

timeToLiveSeconds="300"

overflowToDisk="false"

memoryStoreEvictionPolicy="LRU">

class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"

properties="

replicateAsynchronously=true,

replicatePuts=true,

replicateUpdates=true,

replicateUpdatesViaCopy=true,

replicateRemovals=true " />

class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"

properties="bootstrapAsynchronously=true" />

一个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 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()方法就会去数据库查询了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值