As said in EhCache documentation:
In practice this means that persistent in-memory cache will start up with all of its elements on disk. [...] So, the Ehcache design does not load them all into memory on start up, but lazily loads them as required.
I would like that the memory cache start up will all its elements in memory, how can I achieve that?
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.
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.
I am just giving idea where we load DiskStore into memeory after the application is started
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);
}
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.
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;
}
.
.
}
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With