关于一个缓存操作的例子!!!

wangzhuoyan 2009-07-13 03:58:54
我现在使用ehcache,做个小例子,我的配置是这样的
ehcache.xml:

<cache name="sampleCache" eternal="false" overflowToDisk="true"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU">
Java测试类中: TestCache
public static void main(String[] args) throws Exception {
Student st = new Student(0, "kaka");
// 使用默认配置文件创建CacheManager
CacheManager manager = CacheManager.create();

// 通过manager可以生成指定名称的Cache对象
Cache cache = manager.getCache("sampleCache");
Element element = new Element("date", st.getName());
cache.put(element);
element = cache.get("date");
System.out.println("从cache中取回元素"+element.getValue());
System.out.println("缓存中的对象数"+cache.getSize());
System.out.println("得到缓存对象占用内存的大小"+cache.getMemoryStoreSize());
这段代码执行完以后,控制台输出:从cache中取回元素kaka
缓存中的对象数1
得到缓存对象占用内存的大小0
我的测试类2 GetCache
CacheManager manager = CacheManager.create();

Cache cache = manager.getCache("sampleCache");
//从cache中取回元素
Element result = cache.get("date");
21: System.out.println(":::::::"+result.getValue());

为什么一直报错,提示21行
"main" java.lang.NullPointerException

1.难道我没写入到缓存中,还是我的操作有问题。

2.为什么得到缓存对象占用内存的大小0?应该有大小的啊?
请高手指点。在线等



...全文
81 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
marf_cn 2009-07-16
  • 打赏
  • 举报
回复
result.getValue()); 这错了,result是null。
CacheManager 设置为单例
wangzhuoyan 2009-07-15
  • 打赏
  • 举报
回复
那我现在怎么改呢?
IniY0_0 2009-07-15
  • 打赏
  • 举报
回复
静态的貌似就是eternal="true" , timeToIdleSeconds="300" timeToLiveSeconds="600"这两个属性都失效了
wangzhuoyan 2009-07-15
  • 打赏
  • 举报
回复
把cache做成静态的吧?支持吗?怎么做成静态的
少龙123 2009-07-15
  • 打赏
  • 举报
回复
CacheManager manager = CacheManager.create();

我估计是create这个方法每次运行的时候重新分配了一片内存

把cache做成静态的吧
beijingzhengzhiwen 2009-07-15
  • 打赏
  • 举报
回复
不清楚,帮顶
wangzhuoyan 2009-07-14
  • 打赏
  • 举报
回复
分不够,还是问题难啊,没人回答啊,加分试试
wangzhuoyan 2009-07-14
  • 打赏
  • 举报
回复
改大了,也不行,一样的错误
wangzhuoyan 2009-07-14
  • 打赏
  • 举报
回复
再来
IniY0_0 2009-07-13
  • 打赏
  • 举报
回复
timeToLiveSeconds="600" 改大一点看看
eternal="true"
wangzhuoyan 2009-07-13
  • 打赏
  • 举报
回复
没人回答,晕
wangzhuoyan 2009-07-13
  • 打赏
  • 举报
回复
在线等,帮助

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧