springboot+緩存

本文详细介绍如何在SpringBoot项目中集成缓存功能,包括配置依赖、启用全局缓存、使用@Cacheable注解以及ehcache的具体配置。通过实例演示,帮助开发者快速掌握SpringBoot缓存的使用技巧。

**

springboot+緩存

1:pom文件配置依賴

org.springframework.boot
spring-boot-starter-cache

2:在springboot的启动类上,加上此注解@EnableCaching,全局开缓存,默认缓存框架是ConcurrentMap.
3:在mapper文件中使用
@Cacheable(value=“users”,key="#id")
public User getUserById(Integer id);
ehcache配置
1:pom.xml加入依赖

net.sf.ehcache
ehcache

2:建立ehcache.xml配置文件

<diskStore path="java.io.tmpdir"/>
<defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        maxElementsOnDisk="10000000"
        diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU">
    <persistence strategy="localTempSwap"/>
</defaultCache>
<cache name="users"
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        maxElementsOnDisk="10000000"
        diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU">
    <persistence strategy="localTempSwap"/>
</cache>

3:application.yml中配置路徑
spring:
cache:
ehcache:
config: ehcache.xm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值