如何在不使用休眠/弹簧拦截器的情况下为独立 Java 程序配置 EHcache?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3694861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure EHcache for standalone Java program without using hibernate / spring interceptors?
提问by ari
Can anyone please post an example to configure Ehcache
for a standalone java application?
任何人都可以发布一个示例来配置Ehcache
独立的 Java 应用程序吗?
I have the following simple requiremens:
我有以下简单的要求:
- getting data from database,
- formatting that data and
- writing to file
- 从数据库中获取数据,
- 格式化该数据和
- 写入文件
I am using jdbctemplate.Query
, it is executing quickly but the retrieval from list is taking quite a while. List
is holding large volume of data (resultset).
我正在使用jdbctemplate.Query
,它正在快速执行,但从列表中检索需要很长时间。List
持有大量数据(结果集)。
Can anyone suggest how to overcome this problem?
谁能建议如何克服这个问题?
回答by Pascal Thivent
Check the Code Samples and Recipeschapter for more information on direct interaction with ehcache.
检查的代码示例和配方一章,与直接的Ehcache互动的更多信息。
Recipes and Code Samples
The Recipes and Code Samples page contains recipes - short concise examples for specific use cases - and a set of code samples that will help you get started with Ehcache.
食谱和代码示例
食谱和代码示例页面包含食谱 - 特定用例的简短示例 - 以及一组将帮助您开始使用 Ehcache 的代码示例。
They cover several use cases and provide several code samples, which is just what you're asking for.
它们涵盖了几个用例并提供了几个代码示例,这正是您所要求的。
回答by Bruno Grieder
This is a very old post, but it seems to kick back regularly so....
这是一个非常古老的帖子,但它似乎定期回退,所以......
You should follow Pascal's advice and read those samples, but here is a snip of sample code to get you started (translated from Scala, I did not fully check the syntax)
您应该遵循 Pascal 的建议并阅读这些示例,但这里有一段示例代码可以帮助您入门(从 Scala 翻译,我没有完全检查语法)
First, put
net.sf.ehcache:ehcache:2.9.0
and its dependencies in your ClassPathTo create a cache, it is as simple as
CacheManager cacheMgr = CacheManager.newInstance(); //Initialise a cache if it does not already exist if (cacheMgr.getCache("MyCache") == null) { cacheMgr.addCache("MyCache"); }
首先,将
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"); }
Instantiate the CacheManager only once in your code and reuse it.
仅在您的代码中实例化 CacheManager 一次并重用它。
- The behaviour of your cache is dictated by an XML configuration file called
ehcache.xml
which must be available on your classpath. You can also do it programatically. The file could look like
- 缓存的行为由名为的 XML 配置文件决定,该文件
ehcache.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>
For details on the parameters, check http://ehcache.org/documentation/2.8/configuration/configuration
详细参数请查看http://ehcache.org/documentation/2.8/configuration/configuration
Use it
//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();
用它
//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();
Try storing serializable objects so you can easily overflow to a storage device.
尝试存储可序列化的对象,以便您可以轻松地溢出到存储设备。