ehcache缓存过期时间和注解的使用。

一、过期时间的测试:

    我们配置一个spring整合ehcache的例子,使用自动注解缓存的方式,进行测试。timeToIdleSeconds表示最大空闲的时间,timeToLiveSeconds表示最大存活时间。

   例1结果:5秒间隔内不过期,超过10秒一定过期,就不测试了,占用篇幅!备注:缓存的过期是访问后才过期!

 <cache name="cacheTest1"
        maxElementsInMemory="1000"
        eternal="false"
        overflowToDisk="false"
        timeToIdleSeconds="5"
        timeToLiveSeconds="10"/>

   例2 最大闲置5秒,最大存活0。结论:当存活时间不设置(0默认),只要闲置时间内访问,可以无限存活。

        
    <cache name="cacheTest2"
        maxElementsInMemory="1000"
        eternal="false"
        overflowToDisk="false"
        timeToIdleSeconds="5"
        timeToLiveSeconds="0"/>
    
    /**
     * 时间的测试2
     * unless="#result!=null; 表示返回null的值不缓存
     */
    @Cacheable(value="cacheTest2",key="#param",unless="#result==null")
    public String getTime2(String param) {
        Long time = System.currentTimeMillis();
        return time.toString();
    }
    
    /**时间2的清除*/
    @CacheEvict(value="cacheTest2",key="#param")
    public void clearTime2(String param){
    }
    

测试结果:

	/**
	 *  2.ehcache配置,闲置5秒,存活0秒
	 */
	@Test
	public void test2() throws InterruptedException{
		MyEhcacheAutoService bean = context.getBean(MyEhcacheAutoService.class);
		String time3 = bean.getTime2("shijian");
		System.out.println(time3);
		Thread.sleep(4000);
		String time4 = bean.getTime2("shijian");
		System.out.println(time4);//4秒时间,缓存不变
		Thread.sleep(4000);
		String time5 = bean.getTime2("shijian");
		System.out.println(time5);//4+4秒时间,缓存不变
		Thread.sleep(4000);
		bean.clearTime2("shijian");
		String time6 = bean.getTime2("shijian");
		System.out.println(time6);//4+4+4秒时间,如果不清除缓存不变,但是被清除了,会变
	}

  例3:我们知道当设置了存活时间,存活时间到了一定过期,那么假如把闲置时间设置0呢?

<cache name="cacheTest22" maxElementsInMemory="1000" eternal="false"
		overflowToDisk="false" timeToIdleSeconds="0" timeToLiveSeconds="5" />
	/**
	 *  2.ehcache配置,闲置时间0秒,存活5秒
	 */
	@Test
	public void test22() throws InterruptedException{
		MyEhcacheAutoService bean = context.getBean(MyEhcacheAutoService.class);
		String time1 = bean.getTime22("shijian");
		System.out.println(time1);
		Thread.sleep(4000);
		String time2 = bean.getTime22("shijian"); //不变
		System.out.println(time2);
		Thread.sleep(4000);
		String time3 = bean.getTime22("shijian"); //变
		System.out.println(time3);
	}

结论:1、2个时间都不为0,那么只要到过期时间必定过期,否则看是否闲置时间内。

          2、如果存活时间为0,代表永久存活。即只要看在闲置时间间隔内访问即可。

          3、如果闲置时间为0,可以想成代表永久活跃,基本就没意义了,只要看存活时间到期即可。

          4、2个参数,一个为0,只看另外一个。2个都不为0,2个条件都要满足!【只看此条】

二、ehcache在整合到spring中的几个注解

         @Cacheable(value="cacheTest1",key="#param")  //请求后会缓存,查询用,value是配置的cache名,key就表示key

         @CacheEvict(value="cacheTest2",key="#param") //请求后会删除。删除用!

         @CachePut(value="cacheTest3",unless="#result==null")//请求后方法肯定执行,并且存缓存,更新用! unless除了结果不等于空。(#result 代表返回结果)

 问题1、那么不设置key,默认的key是什么?  答案:是一个空数组

	@Test
	public void test0(){
		MyEhcacheAutoService bean = context.getBean(MyEhcacheAutoService.class);
		bean.getTime0();
		List<Element> all = EhcacheUtil.getAll("cacheTest0");//备注:此Util是用API操作,它可以和注解互通。
		for (int i = 0; i < all.size(); i++) {
			Object objectKey = all.get(i).getObjectKey();
			System.out.println(objectKey);   //SimpleKey []         
		}
	}

问题2、key有那些?

      常用的key的形式:#xxx  方法参数名。(如果是对象,可以xxx.id的形式)

                                  #root.target  实例对象

                                  #root.methodName  方法名称

问题3、如何拼接几个参数?  (采用单引号的拼接形式)

    /**
     * 时间的测试3
     */
    @Cacheable(value="cacheTest3",key="#param1+'_'+#param2",unless="#result==null")
    public String getTime3(String param1,String param2) {
        Long time = System.currentTimeMillis();
        return time.toString();
    }

其他:一个参数做key不能为空。但是拼接时,只要有1个不为空即可做为key。

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
使用 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 的基本步骤。请根据你的具体需求进行相应的修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值