缓存已经在我们的系统中成为性能提升最重要的方式,页面级缓存、系统级缓存、数据缓存、数据库内置缓存等等一些列缓存操作,今天要告诉大家spring其实也提供缓存,当然它只支持单点缓存,所以被忽略,局限性比较大,其实N多种框架都是带有缓存。不过最近JavaOne最近一次的大会上,提到如何办到单机下性能最优化,其实包括国内点评网在内的以java为核心技术的互联网公司为了省成本,其实也在最大限度发挥单机的下java服务性能指标。
谈到spring的缓存想到的是两个注解@Cacheable and @CacheEvict
@Cacheable能够提供类级别以及颗粒度达到方法级别
类级别
@Cacheable(value = "employee") public class EmployeeDAO { public Person findEmployee(String firstName, String surname, int age) {
@Cacheable(value = "employee", key = "#surname") public Person findEmployeeBySurname(String firstName, String surname, int age) { return new Person(firstName, surname, age); }
return new Person(firstName, surname, age); } public Person findAnotherEmployee(String firstName, String surname, int age) { return new Person(firstName, surname, age); } }
上述的Person即将被cache
你可以同SpEL的方式指定cache的key值,学习SpEL可以查看我的另一篇博文 http://cywhoyi.iteye.com/blog/1945771
@Cacheable(value = "employee", condition = "#age < 25") public Person findEmployeeByAge(String firstName, String surname, int age) { return new Person(firstName, surname, age); }
其实这样的方式非常简单,比其一般的我们设计SimpleCache,采用LRU、FIFO的算法控制缓存时效加上弱引用对象而减少内存的压榨来得更加便捷。
接下来让我们进行比较测试
@Test public void testCache() { Person employee1 = instance.findEmployee("John", "Smith", 22); Person employee2 = instance.findEmployee("John", "Smith", 22); assertEquals(employee1, employee2); }
true,下面的employee2就是来源于cache
@Test public void testCacheWithAgeAsCondition2() { Person employee1 = instance.findEmployeeByAge("John", "Smith", 30); Person employee2 = instance.findEmployeeByAge("John", "Smith", 30); assertFalse(employee1 == employee2); }
因为都大于22所以来源于的对象都是新产生的
@Test public void testCacheWithAgeAsCondition() { Person employee1 = instance.findEmployeeByAge("John", "Smith", 22); Person employee2 = instance.findEmployeeByAge("John", "Smith", 22); assertEquals(employee1, employee2); }
都是同一个对象
相关推荐
缓存是实际工作中非经常常使用的一种提高性能的方法, 我们会在很多场景下来...下面这篇文章主要给大家介绍了关于Spring Cache的基本使用与实现原理的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
主要介绍了Spring Cache手动清理Redis缓存,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
一个springcacheable例子,页面使用layer一个springcacheable例子,页面使用layer
主要介绍了Spring缓存机制实例代码,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
通过spring-cache对方法进行缓存,同时验证如何通过key更新指定的缓存!
关于Spring Cache 在Spring3.1以后增加了一项happy的技术点,就是基于注解来实现缓存的技术。他的本质上不是具体的缓存方案实现,而是一个对缓存的抽象,让我们通过注解,实现少量的代码,达到实现缓存的方案。 ...
3.1、spring boot redis注解缓存Cacheable (value) 1
主要介绍了详解Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用,非常具有实用价值,需要的朋友可以参考下
spring + redis使用@Cacheable,@CachePut,@CacheEvict详细步骤
因为使用过spring中的cache功能,感觉es中如果有spring cacheable注解就好了,在spring中注解使用如下: @Cacheable(value="'accountCache_'+#userName")// 缓存名叫 accountCache_USERNAME public Account
gigaspaces-spring-cacheable GigaSpaces XAP的实现。 这为Spring应用程序中的方法提供了对@Cacheable批注的支持。 要将GigaSpaces用作缓存提供程序,应定义一个GigaSpacesCacheManager bean并将其注册为Spring缓存...
内容包括整体认识JPA、JPA基础查询方法、定义查询方法、注解式查询方法、@Entity实例里面常用注解详解、JpaRepository扩展...SpringDataRedis实现cacheable的实践、IntelliJIDEA加快开发效率、SpringDataREST简单介绍等...
兼容Spring Cacheable 设计 由于缓存通常应用在不重复异动的资料上,所以不应该每次都向向第三方缓存服务获取资料,而是通过MQ Service广播和订阅消息功能,发布重新发布的事件,将对应的资料清除,大大提高系统效能...
兼容Spring Cacheable 设计 使用RDBS做任务持久化 使用Message Queue Service发布任务更新 可移除参考Java类为执行对象 以职位组,职位名称注册任务处理器 使用 任务管理服务 一,配置调度程序 任务管理专案和任务...
SpringMVC集成Redis进行缓存管理,SpringMVC基于接口编程的实例,从代码中学习Spring是怎么集成缓存。
3. Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用 3 3.1. @Cacheable 4 3.2. @CachePut 6 3.3. @CacheEvict 7 3.4. @Caching 8 3.5. 使用自定义注解 8 4. 使用Lettuce 操作redis单机版和redis集群 9
spring缓存中用conditon限制放入缓存的数据。
spring-modules-cache.jar spring-modules-cache.jar
在Spring体系中使用redis.zip spring集成redis缓存
介绍spring与ehcache缓存结合,使用spring注解访问,使用guava包中的缓存,封装guava缓存,为应用程序使用。类似于自定义的方式