ehcache整合spring注解方式

时间:2022-11-03 04:39:27

一、简介

  在hibernate中就是用到了ehcache 充当缓存。spring对ehcache也提供了支持,使用也比较简单,只需在spring的配置文件中将ehcache的ehcache.xml文件配置进去即可。在spring中使用ehcache有两种方式,一种是使用spring提供的封装,使用注解的方式配置在某个方法上面,第一次调用该方法的时候,将该方法执行返回的数据缓存,当再次执行的时候如果时间没有变,就直接冲缓存获取,该方法不执行;另一种方式是获取到ehcache本地接口,直接使用ehcache本地接口进行数据缓存。第一种的方式使用简单,代码简洁,但灵活度不高,而第二种方式灵活度高,但是代码就比较到,需要自己去判断数据是否缓存,如何没有缓存就去获取上海并本放入缓;如果缓存了,就直接去缓存中获取。本例子是用的是第一种方式。

  工程使用maven构建,需要的依赖如下:

        <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>

二、示例代码

  ehcache.xml代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false"> <!-- 默认缓存配置 ,缓存名称为 default -->
<defaultCache maxElementsInMemory="50" eternal="false"
overflowToDisk="false" memoryStoreEvictionPolicy="LFU" />
<!-- 自定义缓存,名称为lt.ehcache -->
<cache name="lt.ecache" maxElementsInMemory="50" eternal="false"
overflowToDisk="false" memoryStoreEvictionPolicy="LFU" />
</ehcache>

  Spring-config-ehcache.xml代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <cache:annotation-driven />
<context:component-scan base-package="com.ehcache.test" /> <!-- 注解扫描路径 --> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcache" />
</bean> <bean id="ehcache"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml" />
</bean> </beans>

  ehcache.xml与Spring-config-ehcache.xml存放在系统目录下。

  相关代码如下:

  实体Employee.java

 package com.ehcache.test;

 import java.io.Serializable;

 public class Employee implements Serializable {
private static final long serialVersionUID = -6534180255882276966L;
private int id;
private String name;
private String designation; public Employee(int id, String name, String designation) {
super();
this.id = id;
this.name = name;
this.designation = designation;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} @Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", designation=" + designation + "]";
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getDesignation() {
return designation;
} public void setDesignation(String designation) {
this.designation = designation;
}
}

  EmployeeDAO.java

  

 package com.ehcache.test;

 import java.util.ArrayList;
import java.util.List; import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component; @Component("employeeDAO")
public class EmployeeDAO { // key 当前类,缓存对象为返回值list
@Cacheable(value = {"lt.ecache"}, key = "#root.targetClass")
public List<Employee> getEmployees() {
System.out.println("*** getEmployees() 已经调用 ***");
List<Employee> list = new ArrayList<Employee>(5);
list.add(new Employee(1, "Ben", "Architect"));
list.add(new Employee(2, "Harley", "Programmer"));
list.add(new Employee(3, "Peter", "BusinessAnalyst"));
list.add(new Employee(4, "Sasi", "Manager"));
list.add(new Employee(5, "Abhi", "Designer"));
return list;
} // key id,缓存数据为返回值Employee对象
@Cacheable(value = "lt.ecache", key = "#id")
public Employee getEmployee(int id, List<Employee> employees) {
System.out.println("*** getEmployee(): " + id + " ***");
Employee emp = null;
for (Employee employee : employees) {
if (employee.getId() == id) {
emp = employee;
}
}
return emp;
} // @CachePut会去替换缓存中的Employee对象为当前id对应的对象
@CachePut(value = "lt.ecache", key = "#id")
public Employee updateEmployee(int id, String designation, List<Employee> employees) {
System.out.println("*** updateEmployee() " + id + " ***");
Employee emp = null;
int i = 0;
for (Employee employee : employees) {
if (employee.getId() == id) {
employee.setDesignation(designation);
emp = employee;
}
}
System.out.println(emp);
return emp;
} //key为参数中Employee对象的id,缓存指定id对应的Employee对象
@Cacheable(value = "lt.ecache", key = "#employee.id")
public Employee addEmployee(Employee employee, List<Employee> employees) {
System.out.println("*** addEmployee() : " + employee.getId() + " ***");
employees.add(employee);
System.out.println(employee);
return employee;
} //key为参数中的id,移除缓存,移除指定id对应的Employee对象
@CacheEvict(value = "lt.ecache", key = "#id")
public Employee removeEmployee(int id, List<Employee> employees) {
System.out.println("*** removeEmployee() : " + id + " ***");
Employee emp = null;
int i = 0;
for (Employee employee : employees) {
if (employee.getId() == id) {
emp = employee;
} else {
i++;
}
}
employees.remove(i);
return emp;
} //key为当前类,移除缓存,移除employees列表对象
@CacheEvict(value = "lt.ecache", key = "#root.targetClass")
public List<Employee> removeAllEmployee(List<Employee> employees) {
System.out.println("*** removeAllEmployee() : ***");
employees.clear();
System.out.println(employees.size());
return employees;
} }

  lt.ecache为ehcache.xml中配置的缓存名称。对于要使用到缓存的方法必须使用@注解,同时还要将缓存对象在方法的最后return,否则会报错。

  类中使用到了Spring Expression Language(SpEL),其中相关说明如下

#root.methodName 被执行方法的名称
#root.method.name 被执行的方法
#root.target 被执行的目标对象 
#root.targetClass 被执行的目标对象的class
#root.args[0] 调用目标方法的时候目标方法里面的参数(可将参数列表类比成对象数组)的第一个
#root.caches[0].name 获取当前执行方法的缓存

Main.java

 package com.ehcache.test;

 import java.util.List;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config-ehcache.xml"); EmployeeDAO dao = (EmployeeDAO) context.getBean("employeeDAO"); System.out.println("-----------------------第1次调用----------------------------");
List<Employee> employees = dao.getEmployees();
System.out.println(employees.toString());
System.out.println("------------------------第2次调用---------------------------");
employees = dao.getEmployees();
System.out.println(employees.toString());
System.out.println("------------------------第3次调用---------------------------");
employees = dao.getEmployees();
System.out.println(employees.toString()); System.out.println("-------------------------第1次调用--------------------------");
Employee employee = dao.getEmployee(1, employees);
System.out.println(employee.toString());
System.out.println("-------------------------第2次调用--------------------------");
employee = dao.getEmployee(1, employees);
System.out.println(employee.toString()); System.out.println("------------------------- 对象更新--------------------------");
dao.updateEmployee(1, "已经更新的对象", employees);
System.out.println("-------------------------第1次调用--------------------------");
employee = dao.getEmployee(1, employees);
System.out.println(employee.toString());
System.out.println("-------------------------第2次调用--------------------------");
employee = dao.getEmployee(1, employees);
System.out.println(employee.toString()); System.out.println("------------------------- 添加对象--------------------------");
dao.addEmployee(new Employee(6, "555", "Designer5555"),employees);
System.out.println("-------------------------第1次调用--------------------------");
employee = dao.getEmployee(6, employees);
System.out.println(employee);
System.out.println("-------------------------第2次调用--------------------------");
employee = dao.getEmployee(6, employees);
System.out.println(employee.toString()); System.out.println("------------------------- 清除一个对象--------------------------");
System.out.println(employees.size());
employee = dao.removeEmployee(6, employees);
System.out.println("-------------------------第1次调用--------------------------");
employees = dao.getEmployees();
System.out.println(employees);
System.out.println(employees.size());
System.out.println("-------------------------第2次调用--------------------------");
employees = dao.getEmployees();
System.out.println(employees); System.out.println("------------------------- 清除所有--------------------------");
System.out.println(employees.size());
employees = dao.removeAllEmployee(employees);
System.out.println("-------------------------第1次调用--------------------------");
employees = dao.getEmployees();
System.out.println(employees);
System.out.println("-------------------------第2次调用--------------------------");
employees = dao.getEmployees();
System.out.println(employees);
}
}

  当对象缓存之后方法就不会执行,而是直接从缓存中获取。