java ehcache list_Spring ehcache 之"城市"缓存

/***/packagecom.chenlb.study.ehcache;importjava.util.List;importjava.util.Random;importjunit.framework.TestCase;importorg.apache.commons.logging.Log;importorg.apache.commons.logging.LogFactory;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;/*** 2007-8-2

*@authorchenlb

**/publicclassCityCacheTestextendsTestCase {protectedfinalLog logger=LogFactory.getLog(getClass());privateApplicationContext context;privateRandom random;/***@throwsjava.lang.Exception*/protectedvoidsetUp()throwsException {

context=newClassPathXmlApplicationContext(newString[] {"application*.xml"}

);

random=newRandom();

}/***@throwsjava.lang.Exception*/protectedvoidtearDown()throwsException {

}/*** Test method for {@linkcom.chenlb.study.ehcache.CityCache#getCitys(java.lang.Integer)}.*/publicvoidtestGetCitys() {

CityCache cityCache=(CityCache) context.getBean("cityCache");

Listcitys=cityCache.getCitys(1);

assertNotNull(citys);

assertTrue(citys.size()>0);for(IdAndName inn : citys) {

logger.info("省1 :"+inn.getName());

}

citys=cityCache.getCitys(2);

assertNotNull(citys);

assertTrue(citys.size()>0);for(IdAndName inn : citys) {

logger.info("省2 :"+inn.getName());

}

}publicvoidtestGetCitysWithLoop() {

CityCache cityCache=(CityCache) context.getBean("cityCache");

Listcitys, citys_2;intloopNum=5;inttotelTimes=0;do{

logger.info("===============第"+(6-loopNum)+"次循环===============");

citys=cityCache.getCitys(1);

assertNotNull(citys);

assertTrue(citys.size()>0);for(IdAndName inn : citys) {

logger.info("省1 :"+inn.getName());

}

citys_2=cityCache.getCitys(2);

assertNotNull(citys_2);

assertTrue(citys_2.size()>0);for(IdAndName inn : citys_2) {

logger.info("省2 :"+inn.getName());

}

loopNum--;try{inttimes=random.nextInt(800)+400;//400 - 1200 msThread.sleep(times);

totelTimes+=times;

logger.info(totelTimes+"ms 后, 间隔:"+times+"ms");

}catch(InterruptedException e) {//TODO Auto-generated catch blocke.printStackTrace();

}

}while(loopNum>0);

loopNum=5;do{//省2logger.info("===============第"+(6-loopNum)+"次循环===============");

citys=cityCache.getCitys(2);

assertNotNull(citys);

assertTrue(citys.size()>0);for(IdAndName inn : citys) {

logger.info("省2 :"+inn.getName());

}

loopNum--;try{inttimes=random.nextInt(400)+400;//400 - 800 msThread.sleep(times);

totelTimes+=times;

logger.info(totelTimes+"ms 后, 间隔:"+times+"ms");

}catch(InterruptedException e) {//TODO Auto-generated catch blocke.printStackTrace();

}

}while(loopNum>0);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring框架提供了多个缓存级别供开发者选择和配置。其中包括: 1. 类级别缓存:通过在类级别上使用`@CacheConfig`注解,可以为整个类的方法启用缓存。这意味着类中的所有方法都将使用相同的缓存配置。 2. 方法级别缓存:通过在方法上使用`@Cacheable`注解,可以为特定的方法启用缓存。这样,每次调用该方法时,Spring会首先检查缓存中是否存在相应的结果,如果存在,则直接返回缓存中的值,而不执行方法体。 3. 更新缓存:通过`@CachePut`注解,可以在方法执行后将结果存储到缓存中。这对于需要更新缓存的方法非常有用,例如在更新数据库记录后,同时更新缓存。 4. 清除缓存:通过`@CacheEvict`注解,可以清除指定的缓存。这对于需要在方法执行后清除缓存的情况非常有用,例如在删除数据库记录后,同时清除缓存。 需要注意的是,Spring框架本身并不提供具体的缓存实现,而是通过与各种缓存方案的整合来实现缓存功能。开发者可以根据自己的需求选择合适的缓存实现,如Caffeine、Guava Cache、Ehcache等。通过配置`CacheManager`组件,可以将这些缓存实现集成到Spring框架中。 综上所述,Spring框架提供了多个缓存级别,包括类级别缓存、方法级别缓存、更新缓存和清除缓存。开发者可以根据需要选择和配置适合的缓存实现。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [缓存-SpringCache](https://blog.csdn.net/weixin_68829137/article/details/127164634)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [最通俗的方法让你搞懂spring缓存机制](https://blog.csdn.net/qq_29235677/article/details/121685290)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值