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 ...
随机推荐
- 【Android】记录反编译安卓程序步骤
主要是为了分析一个 App 里面用到的接口,以后移植 UWP 用. 1.http://jd.benow.ca/ 下载 JD-GUI. 2.https://github.com/pxb1988/dex2 ...
- spring注解配置启动过程
最近看起spring源码,突然想知道没有web.xml的配置,spring是怎么通过一个继承于AbstractAnnotationConfigDispatcherServletInitializer的 ...
- API的非向后兼容性无论如何通常代表着一种比较差的设计
不管一个类库或者工具方法实现多么的好,如果无法做到向后兼容性,通常会给用户带来很大的升级成本,很多对此的依赖如果希望在后续的升级和维护期间使用该类库的其他新增特性或者好处,将不得不推迟升级亦或是被迫接 ...
- weblogic 12c下jxls导出excel报错Could not initialize class org.apache.poi.xssf.usermodel.XSSFVMLDrawing
周一,开发反馈weblogic 12c下jxls导出excel报错,公司环境和UAT环境均报错,看日志如下: 2016-06-08 09:16:55,825 ERROR org.jxls.util.T ...
- 在Hadoop平台跑python脚本
1.开发IDE,我使用的是PyCharm. 2.运行原理 使用python写MapReduce的“诀窍”是利用Hadoop流的API,通过STDIN(标准输入).STDOUT(标准输出)在 ...
- SQL数据库基础(二)
数据类型: --类似于C#中的数据类型 Datetime 范围是:1753.1.1—— 9999.12.31 Smalldatetime 1900.1.1 ——2079.6.6 操作: ...
- osx的终端软件iterm2 之 修改外观 和 常用快捷键小结
1.修改外观:透明,自己配色,最好还有个透明的小背景,比如这样: 那么你就要这样修改: 2.快捷键小结 (1)⌘ + d 横着分屏 / ⌘ + shift + d 竖着分屏 : 适合多操作的时候 ( ...
- DOM应用实例(寻找房祖名)
在上一篇我讲到了DOM的一些总结,这一次我就用我前几天做的一个游戏demo来讲讲DOM的一些用法吧. 首先简单说说这个游戏,如下图所示(大家忽略样式/(ㄒoㄒ)/~~).当玩家点击开始后,只要选择了正 ...
- Smartforms常见问题
分类: 1.使用SFSY-FORMPAGES显示总页数的时候,如果页数大于9,将会在前10页显示成星号* 解决: 有时候这样做完之后,星号*是没有了,但是字体会有颠倒或者重叠的现象. 如果出了这个问题 ...
- Web打印控件
Lodop是什么? 有人说她是Web打印控件,因为她能打印.在浏览器中以插件的形式出现,用简单一行语句就把整个网页打印出来: 有人说她是打印编程接口,因为她介于浏览器和打印设备之间,是个通道和桥梁,几 ...