spring boot 使用Ehcache

spring boot 使用Ehcache

1-引入maven依赖;

2-增加ehcache.xml

3-bootstrap.yml配置ehcache.xml的路径

4-启动类加注解@EnableCaching

5-使用处加注解@Cacheable(value = "testCache", key = "#user.name")

  Cacheable的value选择配置,默认是默认配置;key是缓存的key值,默认是入参,也可自定义,取其中的一个属性值;

  @CacheEvict(value = "testCache", allEntries = true)

   清空缓存

1-引入maven依赖

 

        <!--spring boot 缓存支持启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <!--Ehcahe-->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.10.1</version>
        </dependency>

 

2-增加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">

    <!-- 磁盘缓存位置 -->
    <diskStore path="java.io.tmpdir/ehcache"/>

    <!-- 默认缓存 -->
    <defaultCache
            maxEntriesLocalHeap="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxEntriesLocalDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </defaultCache>

    <cache name="testCache"
           maxElementsInMemory="1000"
           eternal="false"
           timeToIdleSeconds="3"
           timeToLiveSeconds="3"
           maxEntriesLocalDisk="10000000"
           overflowToDisk="false"
           memoryStoreEvictionPolicy="LRU"/>
</ehcache>

3-bootstrap.yml配置ehcache.xml的路径

#ehcache
spring.cache.ehcache.config: ehcache.xml

4-启动类加注解@EnableCaching

@SpringBootApplication
@EnableCaching
public class UserServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserServiceApplication.class, args);
    }
}

5-使用处加注解@Cacheable("testCache")

    @GetMapping(value = "/testEhcache")
    @ResponseBody
    @Cacheable("testCache")
    public int testEhcache(int num) {
        logger.info("入参:" + num);
        logger.info("执行:" + num);
        for (int i = 0, n = num; i < n; i++) {
            num += i;
        }
        logger.info("结果:" + num);
        return num;
    }

    @GetMapping(value = "/testEhcache2")
    @ResponseBody
    @Cacheable("testCache")
    public int testEhcache2(int num) {
        logger.info("入参:" + num);
        logger.info("执行:" + num);
        for (int i = 0, n = num; i < n; i++) {
            num += Math.random() * 10;
        }
        logger.info("结果:" + num);
        return num;
    }

 

posted @ 2019-04-16 10:33 路迢迢 阅读( ...) 评论( ...) 编辑 收藏
使用 Ehcache 缓存来配置 Spring Boot 的步骤如下: 1. 添加 Ehcache 依赖:在 pom.xml 文件中添加 Ehcache 的相关依赖。 ```xml <dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency> ``` 2. 创建 Ehcache 配置文件:在 src/main/resources 目录下创建一个 ehcache.xml 文件,并配置缓存的名称、容量、过期时间等信息。 ```xml <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.ehcache.org/v3" xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd"> <cache alias="myCache"> <expiry> <ttl unit="seconds">60</ttl> </expiry> <heap unit="entries">100</heap> </cache> </config> ``` 3. 配置 Spring Boot 应用程序:在 Spring Boot 的配置类上使用 @EnableCaching 注解开启缓存功能,并指定 Ehcache 的缓存管理器。 ```java import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.ehcache.EhCacheCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @EnableCaching public class CacheConfig { @Bean public EhCacheCacheManager cacheManager(CacheManager ehCacheManager) { return new EhCacheCacheManager(ehCacheManager); } } ``` 4. 使用缓存注解:在需要缓存的方法上使用 Spring 的缓存注解,比如 @Cacheable、@CachePut、@CacheEvict 等。 ```java @Service public class MyService { @Cacheable("myCache") public String getCachedData(String key) { // 从数据库或其他数据源中获取数据 return data; } } ``` 以上是使用 Ehcache 缓存配置 Spring Boot 的基本步骤。请根据你的具体需求进行相应的修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值