莲都网站建设,黄骅市天气预报,东莞建网站公司案例,酒泉网站建设平台缓存在软件领域已经存在很长时间了。 它们是那些真正有用的东西之一#xff0c;一旦您开始使用它们#xff0c;您会想知道如果没有它们#xff0c;您是如何相处的#xff0c;所以似乎让Spring的家伙们只是在版本中向Spring核心添加缓存实现有点奇怪。 3.1。 我猜想以前没有… 缓存在软件领域已经存在很长时间了。 它们是那些真正有用的东西之一一旦您开始使用它们您会想知道如果没有它们您是如何相处的所以似乎让Spring的家伙们只是在版本中向Spring核心添加缓存实现有点奇怪。 3.1。 我猜想以前没有被视为优先事项此外在引入Java批注之前缓存的困难之一是将缓存代码与业务代码耦合在一起这通常会变得很混乱。 但是Spring的家伙现在已经基于几个注释设计了一个易于使用的缓存系统Cacheable和CacheEvict。 Cacheable批注的想法是您可以使用它来标记将存储在缓存中的方法返回值。 Cacheable批注可以应用于方法级别或类型级别。 在方法级别应用时将对带注释的方法的返回值进行缓存。 在类型级别应用时将缓存每个方法的返回值。 下面的代码演示了如何在类型级别应用Cacheable Cacheable(value employee)
public class EmployeeDAO {public Person findEmployee(String firstName, String surname, int age) {return new Person(firstName, surname, age);}public Person findAnotherEmployee(String firstName, String surname, int age) {return new Person(firstName, surname, age);}
} Cacheable批注采用三个参数value必填以及key和condition。 其中第一个值用于指定存储a方法的返回值的一个或多个缓存的名称。 Cacheable(value employee)public Person findEmployee(String firstName, String surname, int age) {return new Person(firstName, surname, age);} 上面的代码确保新的Person对象存储在“员工”缓存中。 缓存中存储的任何数据都需要一个密钥来快速检索。 默认情况下Spring使用注释方法的签名创建缓存密钥如上面的代码所示。 您可以使用Cacheable的第二个参数key覆盖它。 要定义自定义键请使用SpEL表达式。 Cacheable(value employee, key #surname)public Person findEmployeeBySurname(String firstName, String surname, int age) {return new Person(firstName, surname, age);} 在findEmployeeBySurname…代码中“surname”字符串是SpEL表达式表示“使用findEmployeeBySurname…方法的surname参数创建并创建密钥”。 最后的Cacheable参数是可选的条件参数。 同样这引用了SpEL表达式但是这次它指定了一个条件该条件用于确定是否将方法的返回值添加到缓存中。 Cacheable(value employee, condition #age 25)public Person findEmployeeByAge(String firstName, String surname, int age) {return new Person(firstName, surname, age);} 在上面的代码中我应用了可笑的业务规则即如果雇员不到25岁则仅缓存Person对象。 快速演示了如何应用某些缓存后接下来要做的就是看一看所有含义。 Testpublic void testCache() {Person employee1 instance.findEmployee(John, Smith, 22);Person employee2 instance.findEmployee(John, Smith, 22);assertEquals(employee1, employee2);} 上面的测试演示了最简单的缓存。 第一次调用findEmployee...结果尚未缓存因此将调用我的代码Spring将其返回值存储在缓存中。 在对findEmployee...的第二次调用中未调用我的代码Spring返回了缓存的值 因此局部变量employee1引用了与employee2相同的对象引用这意味着以下情况成立 assertEquals(employee1, employee2); 但是事情并非总是那么清晰。 记住在findEmployeeBySurname中我已经修改了缓存密钥以便使用surname参数创建密钥而在创建自己的密钥算法时要注意的事情是确保任何密钥都引用唯一的对象。 Testpublic void testCacheOnSurnameAsKey() {Person employee1 instance.findEmployeeBySurname(John, Smith, 22);Person employee2 instance.findEmployeeBySurname(Jack, Smith, 55);assertEquals(employee1, employee2);} 上面的代码找到了两个Person实例这些实例显然指向不同的员工 但是由于我只缓存姓氏因此Spring将返回对我第一次调用findEmployeeBySurname…时创建的对象的引用。 对于Spring来说这不是问题但是由于我的缓存键定义不佳。 当引用由将条件应用于Cachable注释的方法创建的对象时必须采取类似的措施。 在我的示例代码中我应用了仅缓存员工年龄在25岁以下的Person实例的任意条件。 Testpublic void testCacheWithAgeAsCondition() {Person employee1 instance.findEmployeeByAge(John, Smith, 22);Person employee2 instance.findEmployeeByAge(John, Smith, 22);assertEquals(employee1, employee2);} 在上面的代码中对employee1和employee2的引用是相等的因为在第二次调用findEmployeeByAge...时Spring返回其缓存的实例。 Testpublic void testCacheWithAgeAsCondition2() {Person employee1 instance.findEmployeeByAge(John, Smith, 30);Person employee2 instance.findEmployeeByAge(John, Smith, 30);assertFalse(employee1 employee2);} 同样在上面的单元测试代码中对employee1和employee2的引用引用了不同的对象在这种情况下John Smith已超过25岁。 这仅涉及Cacheable但是CacheEvict和清除缓存中的项目呢 另外还有一个问题就是在您的Spring配置中添加缓存并选择合适的缓存实现。 但是稍后会有更多……。 参考来自Captain Debugs Blog博客的JCG合作伙伴 Roger Hughes的Spring 3.1 Caching和Cacheable 。 翻译自: https://www.javacodegeeks.com/2012/09/spring-31-caching-and-cacheable.html