一、简介

  在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);
}
}

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

ehcache整合spring注解方式的更多相关文章

  1. ehcache整合spring本地接口方式

    一.简介 ehcache整合spring,可以通过使用echache的本地接口,从而达到定制的目的.在方法中根据业务逻辑进行判断,从缓存中获取数据或将数据保存到缓存.这样让程序变得更加灵活. 本例子使 ...

  2. Ehcache 整合Spring 使用页面、对象缓存

    Ehcache 整合Spring 使用页面.对象缓存 Ehcache在很多项目中都出现过,用法也比较简单.一 般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布 ...

  3. spring注解方式在一个普通的java类里面注入dao

    spring注解方式在一个普通的java类里面注入dao @Repositorypublic class BaseDaoImpl implements BaseDao {这是我的dao如果在servi ...

  4. (转)使用Spring注解方式管理事务与传播行为详解

    http://blog.csdn.net/yerenyuan_pku/article/details/52885041 使用Spring注解方式管理事务 前面讲解了怎么使用@Transactional ...

  5. Ehcache学习总结(3)--Ehcache 整合Spring 使用页面、对象缓存

    Ehcache 整合Spring 使用页面.对象缓存 Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式 ...

  6. Spring Boot整合Mybatis(注解方式和XML方式)

    其实对我个人而言还是不够熟悉JPA.hibernate,所以觉得这两种框架使用起来好麻烦啊. 一直用的Mybatis作为持久层框架, JPA(Hibernate)主张所有的SQL都用Java代码生成, ...

  7. Ibatis,Spring整合(注解方式注入)

    applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...

  8. ActiveMQ学习总结(10)——ActiveMQ采用Spring注解方式发送和监听

    对于ActiveMQ消息的发送,原声的api操作繁琐,而且如果不进行二次封装,打开关闭会话以及各种创建操作也是够够的了.那么,Spring提供了一个很方便的去收发消息的框架,spring jms.整合 ...

  9. (转)Ehcache 整合Spring 使用页面、对象缓存

    Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...

随机推荐

  1. WebApi 集成 Swagger

    1. Swagger(俗称:丝袜哥)是什么东西? Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同 ...

  2. django book 阅读笔记

    一,django是一个十分优秀的python web的框架,那框架的是什么? 假设我们不使用框架来进行编写,我们要用如下的代码进行web脚本: #!/usr/bin/env python import ...

  3. LeeCode - Unique Binary Search Trees

    题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For e ...

  4. MEF核心笔记(6)让 MEF 拥抱 AOP

    场景: 最近推荐同事在项目中使用起了 MEF,用其构建一个插件式的多人开发框架,因为该框架不是让我去设计了,所以对于 MEF 和 IOC 等概念不是很了解的同事,便会出现各种问题.接入 AOP 便是其 ...

  5. Winform开发框架之统计图表的实现

    在前面的一些随笔中,介绍了不少我的Winform框架的特性,上篇随笔<Winform开发框架之通用高级查询模块>对其中的通用高级模块进了一个整理说明,本篇继续介绍Winform开发框架重要 ...

  6. 【C#】1.1 第1章学习要点

    分类:C#.VS2015 创建日期:2016-06-14 教材:十二五国家级规划教材<C#程序设计及应用教程>(第3版) 一.配套源程序(VS2015版)的运行截图 VS2015版的配套源 ...

  7. Android小项目合集(经典教程)包含十五个Android开发应用实例

    http://www.cnblogs.com/aimeng/archive/2012/03/28/2422435.html

  8. ASP.NET WebAPI 12 Action的执行

    Action的激活大概可以分为如下两个步骤:Action对应方法的调用,执行结果的协商.在WebAPI中由HttpActionInvoker(System.Web.Http.Controllers)进 ...

  9. sublimeCodeIntel 的配置

    在项目的根目录目录下建立.codeintel/config 但是在windows 需要进入dos 环境下建立.以点开头的文件夹和文件.资源管理器不允许创建点开头的文件或文件夹,但在命令提示符下是可以的 ...

  10. JavaScript基础10——node对象

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...