以前对EhCache做过api的学习与测试,api就不多讲了,这次把在web下如何使用EhCache以及出现的问题说一下。
同时这篇还写了EhCache的监听。
在web下使用缓存,就是把常用的变动很少的数据放入缓存提高查询效率。如果需要查看实时的数据,需要把缓存清空再查询。
比如有一张字典表,定义了整个工程使用到了配置信息,这个基本上是不变的,所以可以用缓存来提高查询效率。
Dictionary,值得注意的是要实现Serializable接口,因为缓存可能要往硬盘写对象。
- package org.base.common.dictionary.bean;
- import java.io.Serializable;
- import java.util.Date;
- /**
- * 公用的字典表对象
- * @author blossom
- *
- */
- public class Dictionary implements Serializable{
- /**
- * 需要implements Serializable,因为ehcache需要往硬盘写对象
- */
- private static final long serialVersionUID = 1L;
- private String id;
- private String code_no;
- private String code_name;
- private String code_parent_no;
- private int show_order;
- private Date create_time;
- private String remark;
- private String create_user;
- private String valid;
- private String bak1;
- private String bak2;
- private String bak3;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getCode_no() {
- return code_no;
- }
- public void setCode_no(String code_no) {
- this.code_no = code_no;
- }
- public String getCode_name() {
- return code_name;
- }
- public void setCode_name(String code_name) {
- this.code_name = code_name;
- }
- public String getCode_parent_no() {
- return code_parent_no;
- }
- public void setCode_parent_no(String code_parent_no) {
- this.code_parent_no = code_parent_no;
- }
- public int getShow_order() {
- return show_order;
- }
- public void setShow_order(int show_order) {
- this.show_order = show_order;
- }
- public Date getCreate_time() {
- return create_time;
- }
- public void setCreate_time(Date create_time) {
- this.create_time = create_time;
- }
- public String getRemark() {
- return remark;
- }
- public void setRemark(String remark) {
- this.remark = remark;
- }
- public String getCreate_user() {
- return create_user;
- }
- public void setCreate_user(String create_user) {
- this.create_user = create_user;
- }
- public String getValid() {
- return valid;
- }
- public void setValid(String valid) {
- this.valid = valid;
- }
- public String getBak1() {
- return bak1;
- }
- public void setBak1(String bak1) {
- this.bak1 = bak1;
- }
- public String getBak2() {
- return bak2;
- }
- public void setBak2(String bak2) {
- this.bak2 = bak2;
- }
- public String getBak3() {
- return bak3;
- }
- public void setBak3(String bak3) {
- this.bak3 = bak3;
- }
- }
同时我定义了一个MyCacheObject用于公用的实现了Serializable
- package org.base.cache;
- import java.io.Serializable;
- import net.sf.ehcache.CacheEntry;
- import net.sf.ehcache.Element;
- /**
- *
- * @author blossom
- *
- */
- public class MyCacheObject implements Serializable{
- private static final long serialVersionUID = 1L;
- public Object object;
- public Object getObject() {
- return object;
- }
- public void setObject(Object object) {
- this.object = object;
- }
- public MyCacheObject(){}
- public MyCacheObject(Object object){
- this.object=(Serializable)object;
- }
- }
配置文件ehcache.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:noNamespaceSchemaLocation="ehcache.xsd"
- updateCheck="true" monitoring="autodetect"
- dynamicConfig="true">
- <diskStore path="java.io.tmpdir"/>
- <!-- JTA事务配置。class属性若为空,则默认会按照一个顺序寻找TransactionManager对象。
- 也可以自定义,需要实现接口net.sf.ehcache.transaction.manager.TransactionManagerLookup
- -->
- <!--
- <transactionManagerLookup class="net.sf.ehcache.transaction.manager.DefaultTransactionManagerLookup"
- properties="jndiName=java:/TransactionManager" propertySeparator=";"/>
- -->
- <!-- CacheManagerEventListener 缓存监听,根据需要自定义监听类-->
- <cacheManagerEventListenerFactory class="org.base.cache.MyCacheManagerEventListener" />
- <!-- Terracotta服务器集群配置,详细看文档 -->
- <!--
- <terracottaConfig url="localhost:9510"/>
- -->
- <defaultCache
- maxEntriesLocalHeap="10000"
- maxEntriesLocalDisk="20000"
- eternal="false"
- diskSpoolBufferSizeMB="20"
- overflowToDisk="true"
- diskPersistent="true"
- timeToIdleSeconds="30"
- timeToLiveSeconds="30">
- <!-- <terracotta/>-->
- </defaultCache>
- <!--
- 缓存名为myCommonCache,
- 这个缓存最多包含10000个元素在内存中,并将
- 闲置超过5分钟和存在超过10分钟的元素释放。
- 如果超过10000元素,将溢流到磁盘缓存,并且硬盘缓存最大数量是1000.
- 硬盘路径是定义的java.io.tmp。
- -->
- <cache name="myCommonCache"
- maxEntriesLocalHeap="500"
- maxEntriesLocalDisk="1000"
- eternal="false"
- diskSpoolBufferSizeMB="20"
- timeToIdleSeconds="15"
- timeToLiveSeconds="30"
- memoryStoreEvictionPolicy="LFU"
- transactionalMode="off">
- <persistence strategy="localTempSwap"/>
- </cache>
- <!--
- 缓存名为sampleCache2。
- 此缓存在内存中最大元素的数量是1000。
- 没有设置溢出到磁盘,所以1000就是这个缓存的最大值。
- 注意,当一个缓存eternal设置成true,那么TimeToLive
- 和timeToIdle江不起作用。
- <cache name="sampleCache2"
- maxEntriesLocalHeap="1000"
- eternal="true"
- memoryStoreEvictionPolicy="FIFO"/>
- -->
- <!--
- 缓存名为sampleCache3的。
- 这个缓存溢出会到磁盘。磁盘缓存存储在虚拟机重新启动前会持久有效。
- 磁盘的终止线程的时间间隔设置为3分钟,覆盖默认的2分钟。
- <cache name="sampleCache3"
- maxEntriesLocalHeap="500"
- eternal="false"
- overflowToDisk="true"
- diskPersistent="true"
- timeToIdleSeconds="300"
- timeToLiveSeconds="600"
- diskExpiryThreadIntervalSeconds="180"
- memoryStoreEvictionPolicy="LFU">
- </cache>
- -->
- </ehcache>
缓存管理类MyEhcacheManager
- package org.base.cache;
- import java.io.Serializable;
- import java.net.URL;
- import java.util.List;
- import org.base.cache.dao.MyEhcacheDao;
- import net.sf.ehcache.Cache;
- import net.sf.ehcache.CacheEntry;
- import net.sf.ehcache.CacheManager;
- import net.sf.ehcache.Element;
- import net.sf.ehcache.Status;
- /**
- * Ehcache缓存管理在web下的使用实例
- * @author blossom
- *
- */
- public class MyEhcacheManager {
- public static net.sf.ehcache.CacheManager cacheManager = null;
- private static String configPath="/ehcache.xml";//配置文件
- private MyEhcacheDao myEhcacheDao;//查询
- //缓存定义
- private static String CACHE_MYCOMMONCACHE="myCommonCache";//定义文件中配置的缓存
- //缓存中的Element定义
- private static String ELEMENT_MYCOMMONCACHE_DICTIONARY="dictionaryList";//缓存元素
- //实例化cacheManager,单例模式
- public static CacheManager getCacheManagerInstance(){
- if (cacheManager == null) {
- URL configUrl=null;
- configUrl = MyEhcacheManager.class.getClassLoader().getResource(configPath);
- cacheManager = CacheManager.create(configUrl);
- }
- return cacheManager;
- }
- public static net.sf.ehcache.CacheManager getCacheManager() {
- return getCacheManagerInstance();//单例缓存管理
- }
- public static void setCacheManager(net.sf.ehcache.CacheManager cacheManager) {
- MyEhcacheManager.cacheManager = cacheManager;
- }
- //添加新缓存
- public static void addCacheByName(String cacheName){
- if(cacheName==null||cacheName.trim().equals("")){
- System.out.println("cacheName is null");
- }else{
- if(getCacheManager().getCache(cacheName.trim())!=null){
- getCacheManager().removeCache(cacheName.trim());
- }
- getCacheManager().addCache(cacheName.trim());
- System.out.println(cacheName+ "重新添加");
- }
- }
- //得到cache对象
- public static Cache getCacheByName(String cacheName){
- Cache cache=null;
- if(cacheName==null||cacheName.trim().equals("")){
- System.out.println("cacheName is null");
- }else{
- if(getCacheManager().getCache(cacheName.trim())!=null){
- cache=getCacheManager().getCache(cacheName.trim());
- }
- }
- return cache;
- }
- //往缓存中添加元素
- public static void putElementToCache(String cacheName,String elementKey,Object elementValue){
- Cache cache=null;
- if(cacheName==null||cacheName.trim().equals("")){
- System.out.println("添加缓存元素失败,cacheName is null");
- }else if(elementKey==null||elementValue==null){
- System.out.println("添加缓存元素失败,elementKey or elementValue is null");
- }else{
- if(getCacheByName(cacheName.trim())!=null){//缓存存在
- cache=getCacheByName(cacheName.trim());
- }else{//缓存不存在
- addCacheByName(cacheName.trim());
- cache=getCacheByName(cacheName.trim());
- }
- //对cache对象添加Element
- Element element=null;
- if(cache.get(elementKey.trim())!=null){
- cache.remove(elementKey.trim());
- }
- element=new Element(elementKey.trim(),elementValue);
- cache.put(element);
- System.out.println("添加缓存元素:"+elementKey+"成功!");
- }
- }
- //从缓存中获取指定key的值
- public static Object getElementValueFromCache(String cacheName,String elementKey){
- Object result=null;
- Cache cache=null;
- if(cacheName==null||cacheName.trim().equals("")){
- System.out.println("获取缓存元素失败,cacheName is null");
- }else if(elementKey==null){
- System.out.println("获取缓存元素失败,elementKey is null");
- }else{
- if(getCacheByName(cacheName.trim())!=null){//缓存存在
- cache=getCacheByName(cacheName.trim());
- Element element=null;
- if(cache.get(elementKey.trim())!=null){
- element=cache.get(elementKey.trim());
- if(element.getObjectValue()==null){
- System.out.println("缓存中"+elementKey+" 的值为空.");
- }else{
- result=element.getObjectValue();
- }
- }else{
- System.out.println("缓存中"+elementKey+" 的Element值为空.");
- }
- }else{//缓存不存在
- System.out.println("获取缓存元素失败,缓存"+cacheName+" 为空.");
- }
- }
- return result;
- }
- /**
- * 把所有cache中的内容删除,但是cache对象还是保留.
- * Clears the contents of all caches in the CacheManager,
- * but without removing any caches.
- */
- public static void clearAllFromCacheManager(){
- if(getCacheManager()!=null){
- getCacheManager().clearAll();
- System.out.println("CacheManager was clearAll...");
- }
- }
- /**
- * 把所有cache对象都删除。慎用!
- * Removes all caches using removeCache(String) for each cache.
- */
- public static void removalAllFromCacheManager(){
- if(getCacheManager()!=null){
- getCacheManager().removalAll();
- System.out.println("CacheManager was removalAll...");
- }
- }
- //不用缓存时,要关闭,不然会占用cpu和内存资源。
- public static void shutdownCacheManager(){
- if(getCacheManager()!=null){
- getCacheManager().shutdown();
- System.out.println("CacheManager was shutdown...");
- }
- }
- //打印方法1,为了测试用
- public static void printCache(Cache cache){
- System.out.println("缓存状态: "+cache.getStatus().toString());
- if(cache==null){
- System.out.println("cache is null,no print info.");
- }else if(cache.getStatus().toString().equals(Status.STATUS_UNINITIALISED)){
- System.out.println("缓存状态: 未初始化"+cache.getStatus().toString());
- }else if(cache.getStatus().toString().equals(Status.STATUS_SHUTDOWN)){
- System.out.println("缓存状态: 已关闭"+cache.getStatus().toString());
- }else if(cache.getStatus().toString().equals(Status.STATUS_ALIVE)){
- if(cache.getKeys().size()==0){
- System.out.println(cache.getName()+" exits,but no value.");
- }else{
- for(int i=0;i<cache.getKeys().size();i++){
- Object thekey=cache.getKeys().get(i);
- Object thevalue=cache.get(thekey);
- System.out.println(cache.getName()+"--"+i+",key:"+thekey.toString()+",value:"+thevalue.toString());
- }
- }
- }
- }
- //打印方法2,为了测试用
- public static void printCacheByName(String cacheName){
- if(cacheName==null||cacheName.trim().equals("")){
- System.out.println("cacheName is null,no print info.");
- }else{
- if(getCacheManager().getCache(cacheName.trim())!=null){
- Cache cache=getCacheManager().getCache(cacheName.trim());
- printCache(cache);
- }else{
- System.out.println(cacheName+" --null");
- }
- }
- }
- public static void main(String[] sdfsf){
- Cache cache1=MyEhcacheManager.getCacheByName(MyEhcacheManager.CACHE_MYCOMMONCACHE);
- printCache(cache1);
- MyEhcacheManager.putElementToCache(MyEhcacheManager.CACHE_MYCOMMONCACHE, "111", "111haah");
- MyEhcacheManager.putElementToCache(MyEhcacheManager.CACHE_MYCOMMONCACHE, "222", "222haah");
- MyEhcacheManager.putElementToCache(MyEhcacheManager.CACHE_MYCOMMONCACHE, "333", "333haah");
- printCache(cache1);
- MyEhcacheManager.putElementToCache(MyEhcacheManager.CACHE_MYCOMMONCACHE, "111", "111的新值。");
- System.out.println(MyEhcacheManager.getElementValueFromCache(MyEhcacheManager.CACHE_MYCOMMONCACHE, "111"));
- printCache(cache1);
- clearAllFromCacheManager();
- printCache(cache1);
- removalAllFromCacheManager();
- printCache(cache1);
- shutdownCacheManager();
- }
- /*打印
- 缓存状态: STATUS_ALIVE
- 添加缓存元素:111成功!
- 添加缓存元素:222成功!
- 添加缓存元素:333成功!
- 缓存状态: STATUS_ALIVE
- 添加缓存元素:111成功!
- 111的新值。
- 缓存状态: STATUS_ALIVE
- CacheManager was clearAll...
- 缓存状态: STATUS_ALIVE
- CacheManager was removalAll...
- 缓存状态: STATUS_SHUTDOWN
- CacheManager was shutdown...
- */
- public MyEhcacheDao getMyEhcacheDao() {
- return myEhcacheDao;
- }
- public void setMyEhcacheDao(MyEhcacheDao myEhcacheDao) {
- this.myEhcacheDao = myEhcacheDao;
- }
- ///上面是对缓存操作的Demo,实际使用逻辑需要改变 /
- ///以下是web环境下对字典表的缓存操作/
- //得到字典表cache对象("myCommonCache")
- public Cache getMyCommonCache(){
- Cache cache=null;
- if(getCacheManager().getCache(MyEhcacheManager.CACHE_MYCOMMONCACHE)!=null){
- cache=getCacheManager().getCache(MyEhcacheManager.CACHE_MYCOMMONCACHE);
- }else{//如果缓存没有了,这里直接添加,因为在实际中我们不可能告诉应用没有了这个缓存就返回了。
- //而是应该先去查数据,放入缓存,然后再从缓存中取数据。
- //当然,如果缓存有数据,那就直接取数据就行了。
- getCacheManager().addCache(MyEhcacheManager.CACHE_MYCOMMONCACHE);
- }
- return cache;
- }
- //从mycommon缓存中获取字典表数据(这里只是简单的把所有值取出来了,实际应用可以更加细化)
- public List getDictionaryListFromMyCommonCache(String codeNo){
- codeNo=(codeNo==null||codeNo.trim().equals(""))?"":codeNo.trim();
- List dictionaryList=null;
- Cache cache=getMyCommonCache();//字典表公用缓存
- Element element=null;
- if(cache.get(MyEhcacheManager.ELEMENT_MYCOMMONCACHE_DICTIONARY)!=null){
- element=cache.get(MyEhcacheManager.ELEMENT_MYCOMMONCACHE_DICTIONARY);
- if(element.getObjectValue()==null){//值为空,需要从新添加到缓存
- List list=this.getMyEhcacheDao().querydictionaryByCodeNo(codeNo);//查询
- if(list!=null){
- MyCacheObject myCacheObject = new MyCacheObject(list);
- element=new Element((Serializable)MyEhcacheManager.ELEMENT_MYCOMMONCACHE_DICTIONARY,(Serializable)myCacheObject);
- //java.io.NotSerializableException:
- cache.put(element);
- System.out.println("缓存中的字典表元素为空,重新查询添加..");
- }
- dictionaryList=list;
- }else{//有值
- MyCacheObject myObj=(MyCacheObject)element.getObjectValue();
- dictionaryList=(List)myObj.getObject();
- System.out.println("缓存中存在字典表数据,直接获取到。");
- }
- }else{//获取Element空,需要从新添加到缓存
- List list=this.getMyEhcacheDao().querydictionaryByCodeNo("");//查询
- if(list!=null){
- MyCacheObject myCacheObject = new MyCacheObject(list);
- element=new Element((Serializable)MyEhcacheManager.ELEMENT_MYCOMMONCACHE_DICTIONARY,(Serializable)myCacheObject);
- cache.put(element);
- System.out.println("缓存中的字典表元素为空,重新查询添加......");
- }
- dictionaryList=list;
- }
- return dictionaryList;
- }
- /**
- * 有时需要强制从数据库查询最新的实际数据,所以需要刷新缓存,保证数据的实时性。
- * 这里的刷新是指把缓存中所有Element清除
- */
- public void refreshDictionaryListInMyCommonCache(){
- Cache cache=getMyCommonCache();//字典表公用缓存
- Element element=null;
- if(cache.get(MyEhcacheManager.ELEMENT_MYCOMMONCACHE_DICTIONARY)!=null){
- for(int i=0;i<cache.getKeys().size();i++){
- cache.remove(cache.getKeys().get(i));
- }
- }
- //如果某些缓存刷新后需要马上填充,就可以像下面直接放数据
- // if(this.getMyEhcacheDao().querydictionaryByCodeNo("")!=null){
- // List list=this.getMyEhcacheDao().querydictionaryByCodeNo("");//查询
- // MyCacheObject myCacheObject = new MyCacheObject(list);
- // element=new Element((Serializable)MyEhcacheManager.ELEMENT_MYCOMMONCACHE_DICTIONARY,(Serializable)myCacheObject);
- // cache.put(element);
- // System.out.println("已经强制刷新字典表缓存。。。");
- // }
- System.out.println("已经强制清空字典表所有缓存。。。");
- }
- ///
- }
监听类MyCacheManagerEventListener
- package org.base.cache;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Properties;
- import net.sf.ehcache.CacheException;
- import net.sf.ehcache.CacheManager;
- import net.sf.ehcache.Status;
- import net.sf.ehcache.event.CacheManagerEventListener;
- import net.sf.ehcache.event.CacheManagerEventListenerFactory;
- /**
- * ehcache监听自定义
- * @author blossom
- *
- */
- public class MyCacheManagerEventListener extends CacheManagerEventListenerFactory implements CacheManagerEventListener{
- private static MyCacheManagerEventListener myCacheManagerEventListener=null;
- public static MyCacheManagerEventListener getInstance(){
- if(myCacheManagerEventListener==null){
- myCacheManagerEventListener=new MyCacheManagerEventListener();
- }
- return myCacheManagerEventListener;
- }
- public void dispose() throws CacheException {
- // TODO Auto-generated method stub
- System.out.println("CacheManager was disposed.."+getTimeNow()+"(MyCacheManagerEventListener)");
- }
- public Status getStatus() {
- // TODO Auto-generated method stub
- return null;
- }
- public void init() throws CacheException {
- // TODO Auto-generated method stub
- System.out.println("CacheManager was inited.."+getTimeNow()+"(MyCacheManagerEventListener)");
- }
- public void notifyCacheAdded(String s) {
- // TODO Auto-generated method stub
- System.out.println(s+" was added.."+getTimeNow()+"(MyCacheManagerEventListener)");
- }
- public void notifyCacheRemoved(String s) {
- // TODO Auto-generated method stub
- System.out.println(s+" was removed.."+getTimeNow()+"(MyCacheManagerEventListener)");
- }
- @Override
- public CacheManagerEventListener createCacheManagerEventListener(
- CacheManager cachemanager, Properties properties) {
- // TODO Auto-generated method stub
- return getInstance();
- }
- public static String getTimeNow(){
- Date dd=new Date();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- return sdf.format(dd);
- }
- }
查询数据的公用dao
MyEhcacheDao
- package org.base.cache.dao;
- import java.util.List;
- public interface MyEhcacheDao {
- public List querydictionaryByCodeNo(String codeNo);
- }
MyEhcacheDaoImpl
- package org.base.cache.dao.impl;
- import java.util.ArrayList;
- import java.util.List;
- import org.base.MyHibernateDao;
- import org.base.cache.dao.MyEhcacheDao;
- public class MyEhcacheDaoImpl extends MyHibernateDao implements MyEhcacheDao {
- public List querydictionaryByCodeNo(String codeNo){
- List list=new ArrayList();
- if(codeNo==null||codeNo.trim().equals("")){
- System.out.println("参数codeNo为空,查询所有值。");
- String hql="select d from Dictionary d ";
- list=this.queryListHql(hql);
- }else{
- String hql="select d from Dictionary d where d.code_no like '"+codeNo.trim()+"%'";
- list=this.queryListHql(hql);
- }
- return list;
- }
- }
spring配置context_mycache.xml,注意除了dao的注入,还需要org.base.cache.MyEhcacheManager的注入,
org.base.cache.MyEhcacheManager在大多数情况其实相当于做了dao的代理。
- <?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:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
- <bean id="myEhcacheDaoTarget" class="org.base.cache.dao.impl.MyEhcacheDaoImpl">
- <property name="sessionFactory">
- <ref bean="sessionFactory"/>
- </property>
- </bean>
- <bean id="myEhcacheDao" parent="transactionProxyTemplate">
- <property name="target" ref="myEhcacheDaoTarget" />
- <property name="proxyInterfaces">
- <value>org.base.cache.dao.MyEhcacheDao</value>
- </property>
- </bean>
- <!-- myEhcacheManager实现注入,供其他地方调用 -->
- <bean id="myEhcacheManager" class="org.base.cache.MyEhcacheManager">
- <property name="myEhcacheDao">
- <ref bean="myEhcacheDao"/>
- </property>
- </bean>
- </beans>
///以下是对公用配置表的一个查询实例
DictionaryAction
- package org.base.common.dictionary.action;
- import java.util.List;
- import org.base.MyBaseAction;
- import org.base.cache.MyEhcacheManager;
- /**
- * 字典表缓存实例
- * @author blossom
- *
- */
- public class DictionaryAction extends MyBaseAction {
- private static final long serialVersionUID = 1L;
- private MyEhcacheManager myEhcacheManager;
- public String execute() throws Exception{
- return SUCCESS;
- }
- //查询字典表数据(注意缓存是怎么使用的)
- public String queryDictionary() throws Exception{
- String codeNo="";
- if(this.getValueFromRequest("codeNo")!=null){
- codeNo=(String)this.getValueFromRequest("codeNo");
- }
- //数据的查询通过缓存来获取
- List list=this.getMyEhcacheManager().getDictionaryListFromMyCommonCache(codeNo);
- this.setValueToRequest("dictionaryList", list);
- return SUCCESS;
- }
- //刷新字典表缓存
- public String refreshDictionaryInCache() throws Exception{
- this.getMyEhcacheManager().refreshDictionaryListInMyCommonCache();
- this.getPrintWriter().println("字典表缓存已刷新!");
- return NONE;
- }
- public MyEhcacheManager getMyEhcacheManager() {
- return myEhcacheManager;
- }
- public void setMyEhcacheManager(MyEhcacheManager myEhcacheManager) {
- this.myEhcacheManager = myEhcacheManager;
- }
- }
struts_dictionary.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <package name="dictionary" extends="base-struts-default" namespace="/dictionary">
- <!-- 首页 -->
- <action name="queryDictionary" class="org.base.common.dictionary.action.DictionaryAction" method="queryDictionary">
- <result name="success">/jsp/dictionary/dictionary_list.jsp</result>
- </action>
- <action name="refreshDictionaryInCache" class="org.base.common.dictionary.action.DictionaryAction" method="refreshDictionaryInCache">
- </action>
- </package>
- </struts>
dictionary_list.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"%>
- <%@ taglib prefix="s" uri="/struts-tags"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <%@ page import="java.util.*"%>
- <%@page import="java.text.*"%>
- <%@ page import="org.base.common.dictionary.bean.Dictionary"%>
- <%
- String path = request.getContextPath();
- %>
- <%
- List list=null;
- if(request.getAttribute("dictionaryList")!=null){
- list=(List)request.getAttribute("dictionaryList");
- }
- %>
- <html>
- <head>
- <meta name="author" content="lushuaiyin">
- <meta name="description" content="http://blog.csdn.net/lushuaiyin">
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>uploadFile</title>
- <link rel="stylesheet" type="text/css" href="<%=path%>/resource/css/table.css" />
- <script src="<%=path%>/script/jquery-1.7.1.min.js" type="text/javascript"></script>
- </head>
- <body>
- <input type="hidden" value="<%=path%>" id="path">
- <table align="center" width="80%" class="table2">
- <tr>
- <td><input type="text" value="" id="codeNo"/></td>
- </tr>
- <tr>
- <td><input type="button" value="查询" onclick="chaxun()"/></td>
- <td><input type="button" value="刷新缓存" onclick="refreshcache()"/></td>
- </tr>
- </table>
- <table id="mytable1" align="center" width="80%" class="table1">
- <tr>
- <th>code_no</th>
- <th>code_name</th>
- <th>create_time</th>
- <th>create_user</th>
- </tr>
- <%
- if(list!=null){
- for(int i=0;i<list.size();i++){
- Dictionary vo=(Dictionary)list.get(i);
- String code_no="",code_name="",create_time="",create_user="";
- code_no=vo.getCode_no()==null?"":vo.getCode_no().trim();
- code_name=vo.getCode_name()==null?"":vo.getCode_name().trim();
- Date date=vo.getCreate_time();
- SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- create_time=sdf.format(date);
- create_user=vo.getCreate_user()==null?"":vo.getCreate_user().trim();
- if(i%2==0){
- %>
- <tr class="interval">
- <td><%=code_no%></td>
- <td><%=code_name%></td>
- <td><%=create_time%></td>
- <td><%=create_user%></td>
- </tr>
- <%
- }else{
- %>
- <tr>
- <td><%=code_no%></td>
- <td><%=code_name%></td>
- <td><%=create_time%></td>
- <td><%=create_user%></td>
- </tr>
- <%
- }
- }
- }
- %>
- </table>
- </body>
- </html>
- <script type="text/javascript">
- var path=document.getElementById("path").value;
- function chaxun(){
- var codeNo=document.getElementById("codeNo").value;
- var url=path+"/dictionary/queryDictionary.action?codeNo="+codeNo;
- window.location.href=url;
- }
- function refreshcache(){
- var url=path+"/dictionary/refreshDictionaryInCache.action";
- $.ajax({
- type: "POST",
- url: url,
- success: callbackrefreshcache
- });
- }
- function callbackrefreshcache(data){
- alert(data);
- window.location.reload();
- }
- </script>
///测试打印
2013-5-1 15:26:29 org.apache.catalina.core.AprLifecycleListener init
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.6.0_06\bin;.;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files\Java\jdk1.6.0_06\jre\bin;C:/Program Files/Java/jre1.6.0_06/bin/client;C:/Program Files/Java/jre1.6.0_06/bin;C:/Program Files/Java/jre1.6.0_06/lib/i386;D:\app\lushuaiyin\product\11.2.0\client_1\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Java\jdk1.6.0_06\bin;C:\Program Files\TortoiseSVN\bin;D:\MySql\bin;C:\Program Files\QuickTime\QTSystem\;C:\Windows\System32;C:\Program Files\VanDyke Software\Clients\;D:\ChinaDevelopmentBankAct\eclipse;
2013-5-1 15:26:30 org.apache.coyote.http11.Http11Protocol init
信息: Initializing Coyote HTTP/1.1 on http-8080
2013-5-1 15:26:30 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 674 ms
2013-5-1 15:26:30 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2013-5-1 15:26:30 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/6.0.35
2013-5-1 15:26:30 org.apache.catalina.startup.HostConfig deployDescriptor
信息: Deploying configuration descriptor frame.xml
2013-5-1 15:26:30 org.apache.catalina.loader.WebappClassLoader validateJarFile
信息: validateJarFile(D:\ChinaDevelopmentBankAct\workSpace\frame\webapp\WEB-INF\lib\servlet-api-2.5.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
2013-5-1 15:26:31 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
CacheManager was inited..2013-05-01 15:26:34(MyCacheManagerEventListener)
myCommonCache was added..2013-05-01 15:26:35(MyCacheManagerEventListener)
2013-5-1 15:26:39 org.activiti.engine.impl.ProcessEngineImpl <init>
信息: ProcessEngine default created
TestServlet init---
2013-5-1 15:26:42 org.apache.catalina.startup.HostConfig deployDescriptor
信息: Deploying configuration descriptor host-manager.xml
2013-5-1 15:26:42 org.apache.catalina.startup.HostConfig deployDescriptor
信息: Deploying configuration descriptor manager.xml
2013-5-1 15:26:42 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory docs
2013-5-1 15:26:42 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory examples
2013-5-1 15:26:42 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: contextInitialized()
2013-5-1 15:26:42 org.apache.catalina.core.ApplicationContext log
信息: SessionListener: contextInitialized()
2013-5-1 15:26:42 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2013-5-1 15:26:42 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8009
2013-5-1 15:26:42 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/44 config=null
2013-5-1 15:26:42 org.apache.catalina.startup.Catalina start
信息: Server startup in 12418 ms
参数codeNo为空,查询所有值。
Hibernate: select dictionary0_.id as id3_, dictionary0_.code_no as code2_3_, dictionary0_.code_name as code3_3_, dictionary0_.code_parent_no as code4_3_, dictionary0_.show_order as show5_3_, dictionary0_.create_time as create6_3_, dictionary0_.remark as remark3_, dictionary0_.create_user as create8_3_, dictionary0_.valid as valid3_, dictionary0_.bak1 as bak10_3_, dictionary0_.bak2 as bak11_3_, dictionary0_.bak3 as bak12_3_ from LSY_DICTIONARY dictionary0_
缓存中的字典表元素为空,重新查询添加......
缓存中存在字典表数据,直接获取到。
参数codeNo为空,查询所有值。
Hibernate: select dictionary0_.id as id3_, dictionary0_.code_no as code2_3_, dictionary0_.code_name as code3_3_, dictionary0_.code_parent_no as code4_3_, dictionary0_.show_order as show5_3_, dictionary0_.create_time as create6_3_, dictionary0_.remark as remark3_, dictionary0_.create_user as create8_3_, dictionary0_.valid as valid3_, dictionary0_.bak1 as bak10_3_, dictionary0_.bak2 as bak11_3_, dictionary0_.bak3 as bak12_3_ from LSY_DICTIONARY dictionary0_
缓存中的字典表元素为空,重新查询添加......
已经强制清空字典表所有缓存。。。
参数codeNo为空,查询所有值。
Hibernate: select dictionary0_.id as id3_, dictionary0_.code_no as code2_3_, dictionary0_.code_name as code3_3_, dictionary0_.code_parent_no as code4_3_, dictionary0_.show_order as show5_3_, dictionary0_.create_time as create6_3_, dictionary0_.remark as remark3_, dictionary0_.create_user as create8_3_, dictionary0_.valid as valid3_, dictionary0_.bak1 as bak10_3_, dictionary0_.bak2 as bak11_3_, dictionary0_.bak3 as bak12_3_ from LSY_DICTIONARY dictionary0_
缓存中的字典表元素为空,重新查询添加......