关于EHcache的几点疑问

u013254099 2016-01-12 10:46:26
Ehcache保存在缓存中的数据,为什么每次访问前先Put,那么这有什么意义?
我既然用缓存,就希望put一次之后,下次取数据,就可以直接取了,看代码如下
/**
* ehcache 缓存工具类
*
* cacheName在ehcache.xml中配置
*/
public class EhcacheUtil {
public static CacheManager manager = CacheManager.create();

public static Object get(String cacheName, Object key) {
Cache cache = manager.getCache(cacheName);
if (cache != null) {
Element element = cache.get(key);
if (element != null) {
return element.getObjectValue();
}
}
return null;
}

public static void put(String cacheName, Object key, Object value) {
Cache cache = manager.getCache(cacheName);
if (cache != null) {
cache.put(new Element(key, value));
}
}

上面是一个公共类
为了取数据要先put,这样有什么意义呢?

这样直接取就没有结果


期待大神解答我的疑问
...全文
392 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
JJC001 2018-12-28
  • 打赏
  • 举报
回复
ehcache作为一级缓存有2种缓存方式,1种是内存缓存,1种是磁盘缓存。楼主使用的是内存缓存,服务重启后自然就没有了。引申一下:目前为了解决一级缓存在服务重启后缓存丢失这个问题呢,出现了一款开源软件叫J2Cache,有兴趣可以自己看下。
v512345 2017-05-01
  • 打赏
  • 举报
回复
楼主疑惑解开了么,我也遇到同样疑惑了?
一号码农 2016-01-12
  • 打赏
  • 举报
回复
你只是new 一个CacheManager ,没有指定配置的ehcache.xml public CacheManager manager = CacheManager.create(getClass().getClassLoader().getResource("ehcache.xml"));
u013254099 2016-01-12
  • 打赏
  • 举报
回复
引用 6 楼 qq_33715653 的回复:
懂你意思了,就是要持久写入是吧。改了一下你的配置文件 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" > <diskStore path="rteee"/> <defaultCache maxElementsInMemory="0" eternal="false" overflowToDisk="true" /> <cache name="mytest" maxElementsInMemory="0" eternal="false" overflowToDisk="true" diskPersistent="true" memoryStoreEvictionPolicy="LRU"> </cache> <cache name="userCache" maxElementsInMemory="50000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="300" overflowToDisk="false" memoryStoreEvictionPolicy="LRU" /> </ehcache>
按照你这个配置,下面报错了,什么情况
u013254099 2016-01-12
  • 打赏
  • 举报
回复
引用 2 楼 trocp 的回复:
原因是你put之后没有持久化(就是没有保存到磁盘的意思),数据只在内存中,main方法执行结束,JVM退出,内存数据丢失。 你可以试试1楼的方法,将数据持久化到磁盘文件
我还有个疑问,ehcache不是有两种缓存机制的吗 1.内存 2.磁盘 数据不可以存放在内存中吗,可以随时调用的?
u013254099 2016-01-12
  • 打赏
  • 举报
回复
引用 6 楼 qq_33715653 的回复:
懂你意思了,就是要持久写入是吧。改了一下你的配置文件 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" > <diskStore path="rteee"/> <defaultCache maxElementsInMemory="0" eternal="false" overflowToDisk="true" /> <cache name="mytest" maxElementsInMemory="0" eternal="false" overflowToDisk="true" diskPersistent="true" memoryStoreEvictionPolicy="LRU"> </cache> <cache name="userCache" maxElementsInMemory="50000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="300" overflowToDisk="false" memoryStoreEvictionPolicy="LRU" /> </ehcache>
运行直接报下面的错误 Exception in thread "main" java.lang.ExceptionInInitializerError at TestEhcache.main(TestEhcache.java:10) Caused by: java.lang.NullPointerException at EhcacheUtil.<clinit>(EhcacheUtil.java:15) ... 1 more
一号码农 2016-01-12
  • 打赏
  • 举报
回复
懂你意思了,就是要持久写入是吧。改了一下你的配置文件 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" > <diskStore path="rteee"/> <defaultCache maxElementsInMemory="0" eternal="false" overflowToDisk="true" /> <cache name="mytest" maxElementsInMemory="0" eternal="false" overflowToDisk="true" diskPersistent="true" memoryStoreEvictionPolicy="LRU"> </cache> <cache name="userCache" maxElementsInMemory="50000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="300" overflowToDisk="false" memoryStoreEvictionPolicy="LRU" /> </ehcache>
u013254099 2016-01-12
  • 打赏
  • 举报
回复
引用 4 楼 qq_33715653 的回复:
正常出来啊。
我知道你是先put后get的,我意思是put一次以后,以后我取数据都要先put岂不是没有意义了吗?不是应该put一次后,取数据直接get就可以了吗,我put以后,重新get就为null了,
一号码农 2016-01-12
  • 打赏
  • 举报
回复


正常出来啊。
u013254099 2016-01-12
  • 打赏
  • 举报
回复
引用 1 楼 qq_33715653 的回复:
你只是new 一个CacheManager ,没有指定配置的ehcache.xml

public CacheManager manager = CacheManager.create(getClass().getClassLoader().getResource("ehcache.xml"));


引用了也没用,这事我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"/>

<defaultCache
maxElementsInMemory="10000"
eternal="true"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>

<cache name="mytest"
maxElementsInMemory="100000"
eternal="true"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU">
</cache>

<cache name="userCache"
maxElementsInMemory="50000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="300"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"
/>

</ehcache>

oO临时工Oo 2016-01-12
  • 打赏
  • 举报
回复
原因是你put之后没有持久化(就是没有保存到磁盘的意思),数据只在内存中,main方法执行结束,JVM退出,内存数据丢失。 你可以试试1楼的方法,将数据持久化到磁盘文件

81,090

社区成员

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

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