ehcache整合spring本地接口方式
一、简介
ehcache整合spring,可以通过使用echache的本地接口,从而达到定制的目的。在方法中根据业务逻辑进行判断,从缓存中获取数据或将数据保存到缓存。这样让程序变得更加灵活。
本例子使用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-custom.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: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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config />
<context:component-scan base-package="com.ehcache.custom" /> <!-- 注解扫描路径 --> <bean id="cacheService" class="com.ehcache.custom.CacheService">
<property name="cachename" value="lt.ecache"></property> <!-- ehcache.xml中配置的缓存名称 -->
<property name="ehCacheCacheManager" ref="ehCacheCacheManager"></property>
</bean> <bean id="ehCacheCacheManager" 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-custom.xml存放在系统类路径src目录下。
实体Employee.java如下
package com.ehcache.custom;
import java.io.Serializable;
public class Employee implements Serializable {
private static final long serialVersionUID = -4341595236940308296L;
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.custom; import java.util.ArrayList;
import java.util.List; import javax.annotation.Resource; import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils; @Component("employeeDAO")
public class EmployeeDAO { @Resource
private CacheService cacheService; private String listKey = "employeeList"; /**
* 获取对象列表
*
* @return
*/
public List<Employee> getEmployees() {
// 从缓存中获取
List<Employee> list = (List<Employee>) cacheService.get(listKey);
if (CollectionUtils.isEmpty(list)) { // 缓存中没有数据
System.out.println("*** 缓存中没有数据 已经调用 ***");
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"));
this.cacheService.put(listKey, list);// 存放在缓存
}else{
System.out.println("*** 缓存中数据 已经存在 ***");
}
return list;
} /**
* 获取指定的对象
*
* @param id
* 对象id
* @param employees
* @return
*/
public Employee getEmployee(int id, List<Employee> employees) {
// 从缓存中获取
Employee emp = (com.ehcache.custom.Employee) cacheService.get(id);
if (emp == null) {// 缓存中对象不存在
System.out.println("*** 缓存中对象不存在: " + id + " ***");
for (Employee employee : employees) {
if (employee.getId() == id) {
emp = employee;
}
}
this.cacheService.put(id, emp);// 保存到缓存
}else{
System.out.println("*** 缓存中对象存在: " + id + " ***");
}
return emp;
} /**
* 更新对象
*
* @param id
* 对象id
* @param designation
* @param employees
*
*/
public void updateEmployee(int id, String designation,
List<Employee> employees) {
for (Employee employee : employees) {
if (employee.getId() == id) {
employee.setDesignation(designation);
this.cacheService.put(id, employee);// 保存更新后的对象到缓存
}
}
} /**
* 添加对象
*
* @param employee
* 要添加的对象
* @param employees
*
*/
public void addEmployee(Employee employee, List<Employee> employees) {
employees.add(employee);
this.cacheService.put(employee.getId(), employee);// 保存更新后的对象到缓存
} /**
* 删除对象
*
* @param id
* Employee对象id
* @param employees
* @return
*/
public void removeEmployee(int id, List<Employee> employees) {
int i = 0;
for (Employee employee : employees) {
if (employee.getId() != id) {
i++;
} else {
break;
}
}
this.cacheService.evict(id);// 删除缓存中的数据
employees.remove(i);
} /**
* 删除多个对象
*
* @param employees
* @return
*/
public List<Employee> removeAllEmployee(List<Employee> employees) {
System.out.println("*** removeAllEmployee() : ***");
employees.clear();
System.out.println(employees.size());
this.cacheService.evict(listKey);// 删除缓存中的数据
return employees;
} }
CacheService.java代码如下:
package com.ehcache.custom; import org.springframework.cache.Cache;
import org.springframework.cache.Cache.ValueWrapper;
import org.springframework.cache.ehcache.EhCacheCacheManager; /**
* 封装springcache
*
*/
public class CacheService { /**
* 缓存管理对象
*/
private EhCacheCacheManager ehCacheCacheManager; /**
* 缓存名称
*/
private String cachename; public EhCacheCacheManager getEhCacheCacheManager() {
return ehCacheCacheManager;
} public void setEhCacheCacheManager(EhCacheCacheManager ehCacheCacheManager) {
this.ehCacheCacheManager = ehCacheCacheManager;
} public String getCachename() {
return cachename;
} public void setCachename(String cachename) {
this.cachename = cachename;
} /**
* 获取进行操作的Cache对象
*
* @return
*/
private Cache getCache() {
return this.ehCacheCacheManager.getCache(this.cachename);
} /**
* 获取对象
*
* @param key
* 对象对应的key值
* @return
*/
public Object get(Object key) {
ValueWrapper valueWrapper = getCache().get(key);
if (valueWrapper != null) {
return getCache().get(key).get();
}
return valueWrapper;
} /**
* 缓存对象
*
* @param key
* @param value
*/
public void put(Object key, Object value) {
getCache().put(key, value);
} /**
* 如果key对应的缓存数据存在则删除
* @param key
*/
public void evict(Object key){
getCache().evict(key);
} /**
* 该方法获取的就是 net.sf.ehcache.Cache对象
*
* @return net.sf.ehcache.Cache对象
*/
public Object getNativeCache() {
return getCache().getNativeCache();
} }
该类主要是对spring中的ehcache进行封装。
Main.java代码如下:
package com.ehcache.custom; 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-custom.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("------------------------- 获取对象--------------------------");
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("------------------------- 清除一个对象--------------------------");
dao.removeEmployee(2, employees);
System.out.println("-------------------------第1次调用--------------------------");
employees = dao.getEmployees();
System.out.println(employees);
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本地接口方式的更多相关文章
- ehcache整合spring注解方式
一.简介 在hibernate中就是用到了ehcache 充当缓存.spring对ehcache也提供了支持,使用也比较简单,只需在spring的配置文件中将ehcache的ehcache.xml文件 ...
- Ehcache 整合Spring 使用页面、对象缓存
Ehcache 整合Spring 使用页面.对象缓存 Ehcache在很多项目中都出现过,用法也比较简单.一 般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布 ...
- Ehcache学习总结(3)--Ehcache 整合Spring 使用页面、对象缓存
Ehcache 整合Spring 使用页面.对象缓存 Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式 ...
- (转)Ehcache 整合Spring 使用页面、对象缓存
Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...
- Ehcache 整合Spring 使用页面、对象缓存(转载)
Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...
- Ehcache 整合Spring 使用页面、对象缓存(转)
Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...
- Ehcache 整合Spring 使用页面、对象缓存(1)
转自:http://www.cnblogs.com/hoojo/archive/2012/07/12/2587556.html Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以 ...
- Ehcache整合spring配置
为了提高系统的运行效率,引入缓存机制,减少数据库访问和磁盘IO.下面说明一下ehcache和spring整合配置. 1. 需要的jar包 slf4j-api-1.6.1.jar ehcache-c ...
- Ehcache学习总结(2)--Ehcache整合spring配置
首先需要的maven依赖为: [html] view plain copy <!--ehcache--> <dependency> <groupId>com.goo ...
随机推荐
- servlet、filter、listener、interceptor之间的区别和联系
一.概念 1.servlet:servlet是一种运行服务器端的java应用程序,具有独立于平台和协议的特性,并且可以动态的生成web页面,它工作在客户端请求与服务器响应的中间层. 2.filter: ...
- C#调用webservice 时如何传递实体对象
在webservice端公开一个实体类,然后实例化,赋值,然后再给到webservice,可以实现,但是,即使调用端和service端的实体类完全一致,你也要重新实例化service端的,重新赋值,将 ...
- Python入门笔记(15):对文件的操作(1)
一.文件对象 我理解的文件对象就是一个接口,通过这个接口对文件进行相关操作. <Python 核心编程>上说的很晦涩,这里没有深刻理解到,希望有人能解释给我听. >>> ...
- 【Bootstrap基础学习】02 Bootstrap的布局组件应用示例
字体图标的应用示例 <button type="button" class="btn btn-default"> <span class=&q ...
- USE “schema_name” in PostgreSQL
http://timmurphy.org/tag/mysql/ http://timmurphy.org/2009/11/17/use-schema_name-in-postgresql/ ===== ...
- 关于spring配置文件properties的问题
我遇到的问题是我将properties放在src下面的包中不能被spring扫描到,会报配置文件找不到的错误.但是如果放在src目录下就能够被spring扫描到,现在还不知道为什么这样,记个笔记,留到 ...
- [PE结构分析] 11.资源表结构
资源表是一个树形结构,可以设置成2的31次方的层数,Windows 使用了3级: 类型->名称->语言 其中涉及到四个结构: Data Description Resource Direc ...
- Treap树的基础知识
原文 其它较好的的介绍:堆排序 AVL树 树堆,在数据结构中也称Treap(事实上在国内OI界常称为Traep,与之同理的还有"Tarjan神犇发明的"Spaly),是指有一个随 ...
- Metronic 使用到的开源插件汇总
Metronic 是一套完整的 UI 模板,但不仅仅是模板,更应该说是一个 UI 框架.它除了提供了大量网页模板,也提供了非常多的 UI 组件,并且应用了众多 jQuery 插件.通过这些资源的整合, ...
- winform(数据导出、TreeView的使用)
一.数据导出:目标: 将数据库的数据导出成Excel工作表或是Word文档 基本步骤: 1.首先将数据库中的数据封装成实体类 2.写好查询数据的方法,在主窗体中调用查看所有的数据 3.利用saveFi ...