有人可以发布一个示例来为独立的Java应用程序配置Ehcache
吗?
我有以下简单的要求:
我正在使用jdbctemplate.Query
,它正在快速执行,但从列表中检索需要相当长的时间。 List
持有大量数据(结果集)。
有谁能建议如何克服这个问题?
答案 0 :(得分:2)
这是一个非常古老的帖子,但它似乎经常回来......
您应该遵循Pascal的建议并阅读这些示例,但这里有一小段示例代码可以帮助您入门(从Scala翻译,我没有完全检查语法)
首先,将net.sf.ehcache:ehcache:2.9.0
及其依赖项放入ClassPath
要创建缓存,它就像
一样简单CacheManager cacheMgr = CacheManager.newInstance();
//Initialise a cache if it does not already exist
if (cacheMgr.getCache("MyCache") == null) {
cacheMgr.addCache("MyCache");
}
在代码中仅实例化一次CacheManager并重复使用。
ehcache.xml
的XML配置文件决定,该文件必须在类路径中可用。您也可以以编程方式执行此操作。该文件可能看起来像 <ehcache>
<diskStore path="java.io.tmpdir"/>
<cache name="MyCache"
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
>
<persistence strategy="localTempSwap"/>
</cache>
</ehcache>
有关参数的详细信息,请查看http://ehcache.org/documentation/2.8/configuration/configuration
使用
//use it
Cache cache = cacheMgr.getCache("MyCache");
//Store an element
cache.put(new Element("key", mySerializableObj));
//Retrieve an element
Element el = cache.get("key");
Serializable myObj = <Serializable>el.getObjectValue();
尝试存储可序列化的对象,以便轻松溢出到存储设备。
答案 1 :(得分:0)
检查 Code Samples and Recipes章节,了解有关与ehcache直接互动的更多信息。
食谱和代码示例
食谱和代码示例页面 包含食谱 - 简洁明了 特定用例的例子 - 和 一组有用的代码示例 你开始使用Ehcache了。
它们涵盖了几个用例并提供了几个代码示例,这正是您所要求的。