问题描述
如 EhCache文档中所述:
我想启动内存缓存,将其所有元素都保存在内存中,我该如何实现?
I would like that the memory cache start up will all its elements in memory, how can I achieve that?
这样做的原因是我们的网站对缓存进行了 lot 次访问,因此,我们第一次访问该网站时响应时间非常短.
The reason for this is that our website performs a lot of access to the cache, so the first time we visit the website it has very bad response time.
推荐答案
我假设所有缓存的元素都在DiskStore中,并且您希望它们在应用程序启动后立即进入内存.无论如何,使用BootStrapCacheLoader和BootstrapCacheLoaderFactory应该会有所帮助.
I am assuming that all the cached elements are in the DiskStore and you want them to be in-memory as soon as the application is started. In anycase using BootStrapCacheLoader and BootstrapCacheLoaderFactory should be helpful.
我只是想在启动应用程序后将DiskStore加载到内存中的位置
I am just giving idea where we load DiskStore into memeory after the application is started
您可以实现BootstrapCacheLoader,它将如下所示加载缓存元素.方法 BootstrapCacheLoader.load(Ehcache缓存) 的定义可以
You can implement BootstrapCacheLoader which will load the cache elements as below. Definition of the method BootstrapCacheLoader.load(Ehcache cache) can be
//CustomBootstrapCacheLoader implements BootstrapCacheLoader
List<?> keys = cache.getKeys();
if ((keys == null) || keys.isEmpty())
{
return;
}
for (Object key : keys)
{
Element el = cache.getQuiet(key);
cache.removeQuiet(key);
cache.putQuiet(el);
}
上面的方法从DiskCache读取元素,将其删除并放回原处,以便其保留在内存中,并删除了磁盘版本.
Above method reads the element from DiskCache, Removes it and Puts it back so that it stays in the memory and disk version is removed.
实施BootstrapCacheLoaderFactory以便
Implement BootstrapCacheLoaderFactory so that
public class CustomBootstrapCacheLoaderFactory extends BootstrapCacheLoaderFactor
{
.
.
@Override
public BootstrapCacheLoader createBootstrapCacheLoader(Properties properties)
{
CustomBootstrapCacheLoader loader = new CustomBootstrapCacheLoader();
loader.setAsynchronous(getAsyncFromProperty(properties));
return loader;
}
.
.
}
您可以使用下面的CustomBootstrapCacheLoaderFactory如下定义缓存配置
You can define cache configuration as below with CustomBootstrapCacheLoaderFactory as below
<cache
name="DummyCacheEl"
maxElementsInMemory="3500"
eternal="true"
overflowToDisk="false"
diskPersistent="true"
memoryStoreEvictionPolicy="LRU">
<bootstrapCacheLoaderFactory class="CustomBootstrapCacheLoaderFactory" properties="async=true"/>
</cache>
这篇关于将EhCache磁盘存储内容加载到内存中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!