关于一个缓存操作的例子!!!
我现在使用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?应该有大小的啊?
请高手指点。在线等