企业级框架____Ehcache缓存框架(Ehcache和Spring的整合)
来源:互联网 发布:如何退出淘宝客推广 编辑:程序博客网 时间:2024/06/07 19:56
//======整合结构图
//==创建项目添加spring依赖和Rhcache的jar包
jdbc的数据源jar包 这是spring启动必备的一个
ehcache.包和它依赖的包sf4j
Ehcache的结构
//==配置spring的applicationContext.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:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:c="http://www.springframework.org/schema/c"xmlns:cache="http://www.springframework.org/schema/cache"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"><!-- springconfigStart --><!-- 使用注解的方式装配置bean --><context:annotation-config /><context:component-scan base-package="com.frame"></context:component-scan><!-- 配置dbcp数据源 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /><property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl" /><property name="username" value="oracle" /><property name="password" value="123456" /></bean><!-- 开启spring的缓存注解 --> <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml"></property> </bean> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="ehcache"></property> </bean> <cache:annotation-driven cache-manager="cacheManager" /><!-- springconfigEnd --></beans>
配置jdbc文件
#log4j输出选项#log4j.rootLogger=info,stdout,filelog4j.rootLogger=info,file#输出到控制台#log4j.appender.stdout=org.apache.log4j.ConsoleAppender#log4j.appender.stdout.ImmediateFlush=true #log4j.appender.stdout.layout=org.apache.log4j.PatternLayout #log4j.appender.stdout.layout.ConversionPattern=-----------------------------------------------------------------------------------%nLevel:[%p]%nTime:[%d]%nClass:[%c]%nMessage:[%m]%n#写入到根目录log4j.appender.file=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.file.file=${logpath}/logs/log_info.loglog4j.appender.file.encoding=UTF-8log4j.appender.file.DatePattern='.'yyyy-MM-ddlog4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH\:mm\:ss,SSS}_%t method\:%l%n%m%n
<?xml version="1.0" encoding="UTF-8"?><ehcache> <!-- 指定一个文件目录,当EhCache把数据写到硬盘上时,将把数据写到这个文件目录下 --> <diskStore path="java.io.tmpdir"/> <!-- 设定缓存的默认数据过期策略 --> <defaultCache maxElementsInMemory="10000" eternal="false" overflowToDisk="true" timeToIdleSeconds="10" timeToLiveSeconds="20" diskPersistent="false" diskExpiryThreadIntervalSeconds="120"/> <cache name="simpleCache" maxElementsInMemory="1000" eternal="false" overflowToDisk="true" timeToIdleSeconds="10" timeToLiveSeconds="20"/> </ehcache>
//配置student实体类
package com.frame.student.bean;/** * Student entity. @author MyEclipse Persistence Tools */public class Student implements java.io.Serializable {/** * SRRID */private static final long serialVersionUID = -5076741984769526094L;// Fieldsprivate String stuid;private String stuname;private String stupwd;private String createtime;// Constructors/** default constructor */public Student() {}/** minimal constructor */public Student(String stuid) {this.stuid = stuid;}/** full constructor */public Student(String stuid, String stuname, String stupwd,String createtime) {this.stuid = stuid;this.stuname = stuname;this.stupwd = stupwd;this.createtime = createtime;}// Property accessorspublic String getStuid() {return this.stuid;}public void setStuid(String stuid) {this.stuid = stuid;}public String getStuname() {return this.stuname;}public void setStuname(String stuname) {this.stuname = stuname;}public String getStupwd() {return this.stupwd;}public void setStupwd(String stupwd) {this.stupwd = stupwd;}public String getCreatetime() {return this.createtime;}public void setCreatetime(String createtime) {this.createtime = createtime;}}//配置dao和实现类
package com.frame.student.dao;import com.frame.student.bean.Student;public interface StudentDao {/** * 测试下ehcache的缓存机制 * @param param * @return */public Student testEcache(String stuid);}
package com.frame.student.dao;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Repository;import com.frame.student.bean.Student;@Repositorypublic class StudentDaoImpl implements StudentDao{//或者模拟假数据@Cacheable(value = "simpleCache", key = "#stuid")@Overridepublic Student testEcache(String stuid) {System.out.println("开始访问数据库");Student sdf=new Student();sdf.setStuid(stuid);return sdf;}}
//ehcache简单使用
package com.frame.base.ehcache;import net.sf.ehcache.Cache;import net.sf.ehcache.CacheManager;import net.sf.ehcache.Element;public class TestCache {public static void main(String[] args) throws Exception {//初始化Ehcahce对象CacheManager cacheManager = new CacheManager();//加载自定义cache对象Cache cache = cacheManager.getCache("simpleCache");//把集合放入缓存 存放键值对集合类似mapcache.put(new Element("user", "zhangsan"));//取出集合根据key获取值System.out.println("key=user value=:"+cache.get("user").getObjectValue());//更新集合的key=user的值cache.put(new Element("user", "lisi"));System.out.println("key=user value=:"+cache.get("user").getObjectValue());//获取缓存中的原素个数System.out.println("集合个数:"+cache.getSize());//移除cache某个值cache.remove("user");System.out.println("集合个数:"+cache.getSize());// 关闭当前CacheManager对象cacheManager.shutdown();}}
//测试注解型缓存机制
package com.frame.student.test;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.frame.student.dao.StudentDao;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations={"classpath:applicationContext.xml"})public class StudentTest {@Resourceprivate StudentDao dao;@Testpublic void getUserbyId() throws Exception{String stuid="10086";System.out.println("ehcache.xml配置的超时时间为10秒");System.out.println("第一次调用:"+dao.testEcache(stuid).getStuid());Thread.sleep(2000);System.out.println("2秒后调用:"+dao.testEcache(stuid).getStuid());Thread.sleep(9000);System.out.println("9秒后调用:"+dao.testEcache(stuid).getStuid());Thread.sleep(11000);System.out.println("11秒后调用:"+dao.testEcache(stuid).getStuid());}}
//源码: http://pan.baidu.com/s/1pLuQre7
阅读全文
0 0
- 企业级框架____Ehcache缓存框架(Ehcache和Spring的整合)
- 企业级框架____Ehcache缓存框架的简介
- spring整合Ehcache缓存框架
- java缓存框架---spring+ehcache整合
- mybatis和ehcache缓存框架整合
- MyBatis的一级缓存和二级缓存 以及 mybatis和ehcache缓存框架整合
- mybatis中整合ehcache缓存框架的使用
- 【MyBatis框架】查询缓存-二级缓存-整合ehcache
- 【MyBatis框架】查询缓存-二级缓存-整合ehcache
- 【MyBatis框架】查询缓存-二级缓存-整合ehcache
- springmvc、jpa、spring、mongodb和ehcache整合框架demo
- 缓存框架EhCache整合shiro权限验证(基于spring整合)
- Spring MVC学习总结(7)——Spring MVC整合Ehcache缓存框架
- spring和ehcache整合,实现基于注解的缓存实现
- Ehcache结合Spring框架缓存配置
- Spring整合EhCache缓存
- Spring整合Ehcache缓存
- spring整合ehcache缓存
- java异常和错误类
- eclipse项目转as项目中遇到Unable to execute dex: Multiple dex files define Lcom/loopj/Android/http/AsyncHttpC
- 《GPU高性能编程CUDA实战》—— 《笔记三》——点积运算
- /var/run/yum.pid 已被锁定
- c#中sender关键字有何作用
- 企业级框架____Ehcache缓存框架(Ehcache和Spring的整合)
- 《利用Python进行数据分析》笔记---第9章数据聚合与分组运算
- POJ
- 排序总结
- ||和&&都遵循“短路”原理,以及js中typeof
- 矩阵的之字型遍历
- STM32 时钟树及SystemInit()函数理解
- 测试额我认为<
- caffe finetune predict and classify the lung nodule( 肺结节的分类)