package com.mytest.web.dao.imp; import java.io.Serializable; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.ArrayListHandler; import org.apache.log4j.Logger; import com.mytest.init.info.InitCacheManager; import com.mytest.init.info.InitConnection; import com.mytest.web.dao.ObjectDao; @SuppressWarnings("all") public class StudentDao implements ObjectDao<Object[], Object>, Serializable { private Logger log=Logger.getLogger(StudentDao.class); private QueryRunner run = new QueryRunner(InitConnection.getDs(), true); /** * 取得数据集 * @param "sql"XML文件中查找到的SQL语句 * @param "t"XML文件中查找到的SQL语句对应的参数 * @param "o"数据操作类型1、是查询操作2、添加、删除、修改 */ public List<Object[]> getRs(Object sql, Object[] t, int o) { List<Object[]> stulist = new ArrayList<Object[]>(); Cache cm = InitCacheManager.getCacheManager().getCache("DataCache"); try { switch (o) { case 1: if (t != null) { if (cm.get("datalist") == null) { stulist = run.query(sql.toString(), new ArrayListHandler(), t); cm.put(new Element("datalist", (Serializable)stulist)); } else { stulist = (List) cm.get("datalist"); } } else { log.info("is in cache = "+cm.isKeyInCache("aa")); log.info("cache == "+cm); if (!cm.isKeyInCache("aa")) { stulist = run.query(sql.toString(), new ArrayListHandler()); cm.put(new Element("aa",(ArrayList<Object[]>)stulist)); cm.flush(); log.info("aaaaaaaaaaaaaaaaaa"+sql); } else { stulist = (List<Object[]>) cm.get("aa").getValue(); System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); } List<Object> lj=cm.getKeys(); System.out.println("size=="+lj.size()); for(int i=0;i<lj.size();i++){ System.out.println(lj.get(i)); } } break; case 2: if (t != null) { stulist.add(new Object[] { run.update(sql.toString(), t) }); } else { stulist.add(new Object[] { run.update(sql.toString()) }); } log.info(sql); break; } } catch (SQLException e) { e.printStackTrace(); } return stulist; } }
代码中有个List对象,存放到缓存中,可以下面取的时候,发现size=0,也就是说List没有存放进去
下面是ehcache.xml配置的内容?
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false" monitoring="autodetect" dynamicConfig="true"> <diskStore path="java.io.tmpdir" /> <cache name="DataCache" eternal="false" overflowToDisk="true" maxBytesLocalHeap="10000" timeToIdleSeconds="10000" timeToLiveSeconds="30" /> </ehcache>
请大家帮个忙!谢谢!
引用来自“红薯”的答案