如何在不使用hibernate / spring拦截器的情况下为独立Java程序配置EHcache? [英] How to configure EHcache for standalone Java program without using hibernate / spring interceptors?

查看:96
本文介绍了如何在不使用hibernate / spring拦截器的情况下为独立Java程序配置EHcache?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以发布一个示例来为独立的Java应用程序配置 Ehcache 吗?

Can anyone please post an example to configure Ehcache for a standalone java application?

我有以下简单的requiremens:

I have the following simple requiremens:


  • 从数据库获取数据,

  • 格式化数据和

  • 写入文件

我正在使用 jdbctemplate.Query ,它正在快速执行,但从列表中检索需要相当长的时间。 列表持有大量数据(结果集)。

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).

有人可以建议如何解决这个问题吗?

Can anyone suggest how to overcome this problem?

推荐答案

这是一篇非常古老的帖子,但它似乎经常回来......

This is a very old post, but it seems to kick back regularly so....

您应该遵循Pascal的建议并阅读这些示例,但这里有一段示例代码可以帮助您入门(翻译自Scala,我没有完全检查语法)

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)


  1. 首先,将 net.sf.ehcache:ehcache:2.9.0 及其依赖项放在ClassPath中

  1. First, put net.sf.ehcache:ehcache:2.9.0 and its dependencies in your ClassPath

要创建缓存,它就像

CacheManager cacheMgr = CacheManager.newInstance();

//Initialise a cache if it does not already exist
if (cacheMgr.getCache("MyCache") == null) {
    cacheMgr.addCache("MyCache");
}


仅实例化CacheManager一旦进入你的代码并重复使用它。

Instantiate the CacheManager only once in your code and reuse it.


  1. 缓存的行为由名为<$的XML配置文件决定。 c $ c> ehcache.xml 必须在类路径中可用。您也可以以编程方式执行此操作。该文件可能看起来像

  1. 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



    <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

For details on the parameters, check http://ehcache.org/documentation/2.8/configuration/configuration


  1. 使用它

  1. 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();


尝试存储可序列化的对象,以便轻松溢出到存储设备。

Try storing serializable objects so you can easily overflow to a storage device.

这篇关于如何在不使用hibernate / spring拦截器的情况下为独立Java程序配置EHcache?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆