Which is best Spring Cache Abstraction or Hibernate EHCache? - spring

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.

Related

Can I use Websphere with Spring 3.1 and Ehcache?

I need to implement Ehcache in a project that uses Spring version 3.1 and Websphere. I tried to google something about this topic but I could'nt find anything. Have you guys ever used those three tools together? Thanks in advance.
As per EhCache docs,It will NOT detect Websphere Transaction Manager automatically,.
Automatically Detected Transaction Managers
Ehcache automatically detects and uses the following transaction
managers in the following order:
GenericJNDI (e.g. Glassfish, JBoss, JTOM and any others that register
themselves in JNDI at the standard location of
java:/TransactionManager
Weblogic (since 2.4.0)
Bitronix
Atomikos
No configuration is required; they work out of the box. The first found
is used.
And you can configure it as below.
If your Transaction Manager is not in the above list or you wish to
change the priority, provide your own lookup class based on an
implementation of net.sf.ehcache.transaction.manager.TransactionManagerLookup and
specify it in place of the DefaultTransactionManagerLookup in
ehcache.xml:
<transactionManagerLookup
class= "com.mycompany.transaction.manager.MyTransactionManagerLookupClass"
properties="" propertySeparator=":"/>
And to integrate & Use Spring with EhCache, refer this link
From Spring docs,
36.3 Declarative annotation-based caching For caching declaration, the abstraction provides a set of Java annotations:
#Cacheable triggers cache population #CacheEvict triggers cache eviction
#CachePut updates the cache without interfering with the method execution
#Caching regroups multiple cache operations to be applied on a
method
#CacheConfig shares some common cache-related settings at class-level

Spring Cache Abstraction for Write - Behind Caching Strategy

I am new to Spring cache abstraction. I have explored it using ehcache and apache ignite caching providers.
I want to know if spring cache abstraction supports the caching strategies of Write-behind and write-through.
Thanks,
bs
There is no direct support for cache-through in the declarative Spring abstraction.
And in a way it makes sense, since the abstraction lets you surround methods with caching related annotations. But with a cache-through pattern, the whole method would only be a cache interaction: a get for read, or a put for write. Not the if-then-else that the annotation abstracts.
However, if you use the CacheManager and Cache interfaces provided by Spring directly in your code, you can perfectly use them in a cache-through way.
Ignite cache has a notion of CacheStore interface that used in cases when there is a need to wire the cache with a persistent store (RDBMS, MongoDB, Hadoop, etc.). This interface provides write-through/write-behind and read-through semantics. Please refer to this documentation for more details.
Also I would recommend taking look at various examples that demonstrates how particular CacheStore implementations are used in Ignite. The examples are available in Ignite release bundles.

Difference between #CacheEvict and #TriggersRemove annotations

I want to use Ehcache in my portlet application. If I want to remove data from cache, it is better to use #CacheEvict or #TriggersRemove?
According to documentation it looks like #CacheEvict and #TriggersRemove annotations are very similar.
disclaimer: I am working on the Spring caching abstraction (amongst other things).
These are two annotations from two different projects. I don't know much about TriggersRemove but from what I can see, it is ehcache specific.
The caching abstraction in the Spring Framework is completely decoupled from the underlying infrastructure so you can use that with basically any caching library out there, including ehcache. If you want your code to be independent from the caching library you use, I'd advise not to use TriggersRemove. If that's not a problem for you, check the documentation of each solution and choose accordingly.

How exactly does one integrate a cache in his project (spring + hibernate)

I have a project that supports 10's of concurrent users.
My project is a spring + hibernate project with a MYSQL DB.
I would like to keep a cache for some of my entities (i.e. Player)
I have a couple of questions:
1) how do I exactly work with a cache (when i have one) ? if i have to persist a Player do i change the cache and immediately persist with hibernate?
2) Does spring support a cache mechanism ? if so how does one work with it?
Hibernate
Hibernate has Second level cache. To start work with you need to:
choose some cache provider (EHCache, Infinispan, ...).
configure cache region (and choose corresponding strategy depending on your situation)
enable cache for some entity
It is declarative, most of the time you do not need to change any application code.
Spring
Spring has cache abstraction. There are some common steps (choose chache provider, configure cache regions). But it is more general pourpose cache, not related to Hibernate entities and transactions. So you must do more work (annotate all necessary methods with annotations).
In general if your Player class is a Hibernate entity then it will be better to go with Hibernate cache. It may be not true if you have some special demands.
Hope it helps.

Is there a provider agnostic way of getting up to date cache statistics in Spring framework?

Spring provides a useful feature of Cache Abstraction
But what I could not find is a provider agnostic way to get live cache statistics. Essentially I just want to show a list of all the cache names and their corresponding keys with the count of hits, misses, and sizes (in kb) either on a web page or via JMX. I know Ehcache does provide this feature and if I use ehcache API inside the code I can get it (have already used it in the past). But I believe using Ehcache API inside the code takes away the whole notion of the Spring framework's cache abstraction.
The only common, provider-agnostic thing you have is CacheManager interface, which provides the following method:
Collection<String> getCacheNames()
It returns a collection of the caches known by the cache manager.

Resources