`
cywhoyi
  • 浏览: 418203 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring Cacheable

阅读更多

缓存已经在我们的系统中成为性能提升最重要的方式,页面级缓存、系统级缓存、数据缓存、数据库内置缓存等等一些列缓存操作,今天要告诉大家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);
}

 

都是同一个对象

0
5
分享到:
评论

相关推荐

    spring_cacheable_demo_v1.zip

    Spring Cacheable 是 Spring 框架中的一个特性,主要用于实现应用程序的缓存功能,从而提高性能和效率。在这个"spring_cacheable_demo_v1.zip"压缩包中,我们可能找到一个基于Spring的简单缓存示例,它展示了如何...

    Spring Cache的基本使用与实现原理详解

    Spring Cache 是Spring框架提供的一种缓存抽象,从Spring 3.1版本开始引入,目的是为了简化应用程序中的缓存管理,实现缓存透明化。通过在方法上添加特定注解,如@Cacheable、@CacheEvict等,可以轻松地启用缓存功能...

    3.1、spring boot redis注解缓存Cacheable (value) 1

    spring boot redis 注解缓存Cacheable (value) 1 spring boot redis 注解缓存是基于spring boot 框架和redis缓存机制的结合,实现了缓存机制的自动化管理。下面将详细介绍spring boot redis 注解缓存的实现机制和...

    Spring Cache手动清理Redis缓存

    Spring Cache手动清理Redis缓存 Spring Cache是Spring框架中的一种缓存机制,它可以将缓存数据存储在Redis中。然而,在某些情况下,我们需要手动清理Redis缓存,以便释放内存空间或更新缓存数据。在本文中,我们将...

    Spring缓存机制实例代码

    Spring缓存机制是Spring框架提供的一种高级特性,用于在应用程序中实现数据的缓存,从而提高性能和减少数据库的负载。它允许开发者在不修改业务逻辑的情况下,轻松地在应用的不同层次引入缓存功能。本文将深入探讨...

    spring-cache(通过key值更新缓存)

    在Spring框架中,缓存是提高应用程序性能的重要手段。Spring Cache是一个抽象层,它允许开发者在不关注具体缓存实现的情况下,轻松地在应用程序中添加缓存功能。本篇文章将详细探讨如何通过key值更新Spring Cache中...

    spring + redis使用@Cacheable,@CachePut,@CacheEvict

    本篇文章将深入探讨如何使用Spring与Redis集成,并详细解释`@Cacheable`、`@CachePut`和`@CacheEvict`这三个关键的注解,它们是Spring缓存管理的核心组件。 首先,我们需要在项目中引入Spring Data Redis库,它提供...

    java之SpringCache之@Cacheable注解的说明使用

    关于Spring Cache 在Spring3.1以后增加了一项happy的技术点,就是基于注解来实现缓存的技术。他的本质上不是具体的缓存方案实现,而是一个对缓存的抽象,让我们通过注解,实现少量的代码,达到实现缓存的方案。 ...

    详解Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用

    Spring 缓存注解 @Cacheable、@CachePut、@CacheEvict 使用详解 Spring 框架提供了三个缓存注解:@Cacheable、@CachePut 和 @CacheEvict,这三个注解可以帮助开发者简化缓存的使用,提高应用程序的性能。在本文中,...

    Spring集成Redis进行数据缓存

    Spring MVC提供了一些注解来实现缓存控制,包括`@Cacheable`、`@CacheEvict`、`@CachePut`和`@Caching`。 1. **@Cacheable**: 这个注解用在方法上,表示该方法的返回结果应该被缓存。可以通过`value`指定缓存的名称...

    spring缓存机制-根据condition加入缓存(三)

    `@Cacheable`是Spring缓存中最常用的注解,用于标记一个方法,表示该方法的返回结果应该被缓存。它的基本用法如下: ```java @Cacheable(value = "myCache", key = "#id") public MyObject findById(String id) { ...

    一种angular的方法级的缓存注解(装饰器)

    因为使用过spring中的cache功能,感觉es中如果有spring cacheable注解就好了,在spring中注解使用如下: @Cacheable(value="'accountCache_'+#userName")// 缓存名叫 accountCache_USERNAME public Account

    spring简单的缓存

    本示例将聚焦于“Spring简单的缓存”实现,帮助开发者了解如何在Spring框架中集成和使用缓存功能。 Spring框架提供了强大的缓存抽象,支持多种缓存机制,如 EhCache、Redis、Hazelcast 和 Infinispan 等。在Spring...

    Spring 缓存

    在 Spring 中,需要使用 `@Cacheable` 注解对指定的方法进行缓存,例如: ```java @Override @Cacheable(value="testCache") public List&lt;StudentModel&gt; getAllStudents() { return this.studentMapper....

    gigaspaces-spring-cacheable:GigaSpaces XAP的Spring Cache抽象的实现

    gigaspaces-spring-cacheable GigaSpaces XAP的实现。 这为Spring应用程序中的方法提供了对@Cacheable批注的支持。 要将GigaSpaces用作缓存提供程序,应定义一个GigaSpacesCacheManager bean并将其注册为Spring缓存...

    在Spring体系中使用redis.spring集成redis缓存

    在Spring体系中,Redis被广泛用作数据缓存,以提高应用程序的性能和响应速度。Spring框架提供了多种方式来集成Redis,其中最常用的是通过`Spring-data-redis`模块。这个模块提供了对Redis操作的高级抽象,使得在...

    Spring Boot实战派(源码)

    - 集成Redis、Hazelcast等缓存系统,使用`@Cacheable`、`@CacheEvict`进行缓存管理。 13. **国际化的支持** - `MessageSource`接口和`@MessageSource`注解实现多语言支持。 14. **邮件服务** - 使用...

    Spring Data JPA从入门到精通

    内容包括整体认识JPA、JPA基础查询方法、定义查询方法、注解式查询方法、@Entity实例里面常用注解详解、JpaRepository扩展...SpringDataRedis实现cacheable的实践、IntelliJIDEA加快开发效率、SpringDataREST简单介绍等...

    spring mvc + spring + hibernate 全注解整合开发视频教程 04

    同时,我们还会关注性能优化,如缓存机制(如使用`@Cacheable`注解实现缓存)和数据访问优化等。 总的来说,这个视频教程04将带领我们进一步了解如何通过注解实现Spring、Spring MVC和Hibernate的无缝整合,提高...

    SpringDataRedis的jar包.rar

    通过`@Cacheable`, `@CacheEvict`, `@CachePut`等注解,可以方便地控制缓存的存取和更新。 8. **消息支持**:Spring Data Redis提供了对Redis Pub/Sub(发布/订阅)的支持,允许开发构建基于消息的应用程序,实现...

Global site tag (gtag.js) - Google Analytics