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 ...
随机推荐
- mysql 在row模式下truncate 与 delete 二进制日志记录的差异
二进行日志的格式为row mysql> show variables like 'binlog_format'; +---------------+-------+ | Variable_nam ...
- mysql如何修改所有的definer
mysql中的definer是什么,有什么作用? 我们在mysql创建view.trigger.function.procedure.event时都会定义一个Definer=‘xxx’,类似如下: C ...
- 2015年百度之星初赛(1) --- C 序列变换
序列变换 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- P6 EPPM R16.1安装与配置指南(二)
P6 EPPM R16.1安装与配置指南(一) http://www.cnblogs.com/endv/p/5634620.html P6 EPPM R16.1安装与配置指南(二) 环境变量配置 新建 ...
- 【jQuery基础学习】06 jQuery表单验证插件-Validation
jQuery的基础部分前面都讲完了,那么就看插件了. 关于jQuery表单验证插件-Validation validation特点: 内置验证规则:拥有必填.数字.E-Mail.URL和信用卡号码等1 ...
- QQ视差特效和ListView侧滑删除
如图所示是效果图,当向下拉时,图片会被拉出来,松手后恢复.和ListView的侧滑删除 1.视差特效 首先图片是通过addHeaderView加上去的,所以在设置Adapter前先设置一个View ...
- CodeForces 149D Coloring Brackets
Coloring Brackets time limit per test: 2 seconds memory limit per test: 256 megabytes input: standar ...
- java四大域总结
最近学完了web部分,发现有些地方总是单个容易理解,可是把所有的放在一起来大杂烩,总是有那么几个知识点容易混淆.其实网上的资料已经够多了,虽然也不乏辛劳的搬运工.可是最终的目的不就是要我们自身理解吗? ...
- C#中的索引器原理
朋友们,还记得我们在C#语言开发中用到过索引器吗? 记得在获得DataGridView控件的某列值时:dgvlist.SelectedRows[0].Cells[0].Value; 记得在获得List ...
- 以Web Host的方式来寄宿Web API
一.新建一个Common的类库项目并新建一个测试用的Contact实体类 namespace Common { public class Contact { public string Id { ge ...