Ehcache 3.0, example of a eviction adviser? - spring

So I'm upgrading a spring boot project to use the latest version of ehcache (3.0). However, I can't seem to find what I want. I want to be able to set the eviction policy to LRU, however I can't seem to find any eviction policies in the docs. In the link they talk about the OddKeysEvictionAdvisor. If anyone can point me in the right direction it would be much appreciated.
http://www.ehcache.org/documentation/3.0/eviction-advisor.html

In Ehcache 3.0.x there is no configurable eviction policy.
The eviction advisor really is a way to hint that some cache mappings should be kept over others, and as the API shows, this does not include mapping metadata, only key and value.

Related

Ehcache vs BigMemoryMax for Distributed caching

I want to try Ehcache in distributed form. Can someone suggest me if EHcache (Not the BigMemoryMax) works in distributed mode.
I am looking for only open source product. I am not looking to buy a Bigmemory Max or Terracota server, which will give me a off heap access, which i dont need as of now.
If so, please provide the version, url and some guide/examples to implement distributed Ehcache.
Thanks...
Ehcache website is your best starting point:
Ehcache 2.10.x has open source clustering support with Terracotta 4.3.x
Ehcache 3.1.x has open source clustering support with Terracotta 5

Difference between different type of caches and their significance

What is the difference between the Ehcache and Guava Cache? In which scenarios would I use which type of caching?
Note: I am a developer working on Ehcache
The answer to your question depends on which features your cache needs to have inside your application.
Guava caches have all the basic caching features.
Ehcache has more advanced features - see 2.x line or upcoming 3.x line
Multi tier caching (disk in 2.x, offheap + disk in 3.x)
Clustering (2.x for now, soon in 3.x)
Refresh ahead or scheduled refresh (2.x for now)
JSR-107 API
So my advice is to look at your needs, play with both and then decide.

Does ehCache have any option of auto refreshing?

Does ehCache have any feature of auto refreshing? If yes, please let me know how to do that. If not, please let me know few possible and efficient ways in which ehCache refresh can be done.
There is no way for EhCache to know how the data that was inserted into to it, was produced. That part is totally application specific. There fore EhCache cannot refresh itself automatically- it can't pull the data, the data has to be pushed into it.
In order to do that you need to add another component to your application that will run at a scheduled time and while insert the appropriate data into EhCache.
Spring has awesome support for scheduled tasks. Check out this part of the documentation
ehCache has builtin eviction algorithms that you can use by setting timeToIdleSeconds and timetoLiveSeconds this will tell ehcache when to eject that data from the cache and force a reload of the data from your persistence data source. Read the following ehCache document on more detailed instruction on configuring TTL http://ehcache.org/documentation/configuration/data-life

Which is best Spring Cache Abstraction or Hibernate EHCache?

I am planning to implement the Cache in the web application.
I have already used the Hibernate EHCache in one of my previous web application.
Now a days, recently i came to know that in latest Spring release there is Cache Abstraction layer.
Hence i would like to is it just wrapper of Hibernate EHCache or it is Cache features provided by Spring itself.
If different then would like to know which is best or better option to use Cache ?
Also, i came to know from few articles that Hibernate EHCache not supporting clustering, is it ? Because clustering required for big Web Application.
I have used Spring 3.1 and Hibernate 3.3 in Web Application.
Hibernate and EHCache are different things.
EHCache is the cache provider, i.e. the actual implementation of the cache.
Hibernate can use multiple different providers for the L2 cache, including EHCache.
Spring Cache is a framework for caching method calls. It can use multiple different providers, including EHCache.
EHCache offers distribution (clustering) in several modes, from basic JMS-driven synchronization to Terracotta BigMemory distribution. Hibernate L2 cache should support clustering.
Spring Cache has nothing to do with Hibernate, but you can use Hibernate L2 cache and Spring Cache (method caching) in parallel with EHCache as the underlying provider for both.
Just to add, for database-level caching using hibernate, hibernate/JPA second-level cache seems more appropriate. Because it manages data caching at entity level at the persistence context level (JPA APIs), so there are very fewer chances of inconsistency in database and cache, since hibernate manages it well.
But if we use spring cache for database caching, then we always have to ensure every method operating on database need to be annotated with appropriate cache annotation (For example, #CacheEvict for remove), so there are chances of missing annotations and causing inconsistency.
Also, spring cache can only be used on public methods, since it backed my AOP (We can use AspectJ though to solve this).
Hibernate caching continues to work with Spring using EhCache (or another caching provider). It offers a 2nd level cache for storing em.find(key) entities and relationships (#Cache annotations or XML), as well as query caching if you q.setHint("org.hibernate.cacheable",true). These are integrated and well-documented. Spring caching is not needed for this.
But Spring provides method caching which is independent of Hibernate caching, and can still use EhCache as the caching provider (sharing EhCache with Hibernate). This can be a little confusing because the EhCache configurations are overlapping, but essentially Spring Caching allows you to independently cache the results of methods that are marked as #Cacheable.
Note 1: Spring/Method caching is not integrated with Hibernate. Results cached with method caching are not recognized or maintained by Hibernate.
Note 2: Hibernate Query caching is not recommended for highly distributed systems without a good understanding of how the caching is used. It increases the risk of deadlocks due to the use of a shared entity timestamp cache that Hibernate maintains to keep track of when the cached query results should be evicted.

Dynamically change ehcache configuration

I want to dynamically change the configuration of the EhCache instance we're using, specifically the maxElementsInMemory setting. We are using EhCache 1.5 and I can see that it is possible API-wise:
cache.getCacheConfiguration().setMaxElementsInMemory(num);
But the documentation of EhCache says:
After a Cache has been started its
configuration is not generally
changeable. However, since Ehcache
2.0, certain aspects of cache configuration can modified dynamically
at runtime...
So my question is: can I use this API in EhCache 1.5 or is it not guaranteed to work?
Thanks.
There are only certain properties you can modify (see Modifying Ehcache configuration at runtime), maxElementsInMemory should be one of them... for me that works in ehcache 2.4!
As per Ehcache 2.8, this is possible:
Cache cache = manager.getCache("sampleCache");
CacheConfiguration config = cache.getCacheConfiguration();
config.setTimeToIdleSeconds(60);
config.setTimeToLiveSeconds(120);
config.setmaxEntriesLocalHeap(10000);
config.setmaxEntriesLocalDisk(1000000);
source:
http://www.ehcache.org/documentation/2.8/configuration/configuration#dynamically-changing-cache-configuration

Resources