<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<cache:annotation-driven />
...
</beans>
Code language: HTML, XML (xml)
Note the cache:annotation-driven
tag in above declaration enables the caching in given Spring project. @Cacheable
and @CacheEvict
which allow methods to trigger cache population or cache eviction. Let us take a closer look at each annotation: @Cacheable("persons")
public Person profile(Long personId) { ... }
Code language: Java (java)
In the above code snippet, method profile
is marked cacheable using @Cacheable
annotation. Also the method is associated with a cache named “persons
“. Whenever method profile is called, the Spring framework will check if cached entry is available in persons cache and returns the same without calling profile method. It is also possible to provide multiple cache names if you have multiple caches declared in your application. For example: @Cacheable({"persons", "profiles"})
public Person profile(Long personId) { ... }
Code language: Java (java)
In above code snippet, we provide two cache names persons and profiles. Spring framework will check in all the caches if entry is available for given method call with argument personId, if at least one cache is hit, then the associated value will be returned. allEntries
which indicates whether a cache-wide eviction needs to be performed rather then just an entry one (based on the key): @CacheEvict (value = "persons", allEntries=true)
public List<Person> listPersons()
Code language: Java (java)
This annotation is very useful when an entire cache region needs to be cleared out. The Spring framework will ignore any key specified in this scenario as it does not apply. hashCode()
reflects that. If that is not the case then for distributed or persistent environments, the strategy needs to be changed as the objects hashCode
is not preserved. In fact, depending on the JVM implementation or running conditions, the same hashCode
can be reused for different objects, in the same VM instance. To provide a different default key generator, one needs to implement the org.springframework.cache.KeyGenerator
interface. Once configured, the generator will be used for each declaration that does not specify its own key generation strategy. By default, all the method arguments are used in Key generation logic. In practice not all methods have only one argument or, worse yet, the parameters are not suitable as cache keys – take for example a variation of the method above: @Cacheable(value="persons", key="personId")
public Person profile(Long personId, Long groundId) { ... }
Code language: Java (java)
Here we are using just personId
in key generation ignoring groupId altogether. @Cacheable(value="persons", condition="personId > 50")
public Person profile(Long personId) { ... }
Code language: Java (java)
org.springframework.cache.concurrent
package. It allows one to use ConcurrentHashMap
as a backing Cache store. <!-- generic cache manager -->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentCacheFactoryBean" p:name="default"/>
<bean class="org.springframework.cache.concurrent.ConcurrentCacheFactoryBean" p:name="persons"/>
</set>
</property>
</bean>
Code language: HTML, XML (xml)
In above code snippet, we use SimpleCacheManager
class to create a CacheManager
. Note that we have created two caches in our application, one is default
and second is persons
. org.springframework.cache.ehcache
package. Again, to use it, one simply needs to declare the appropriate CacheManager
: <bean id="cacheManager" class="org.springframework.cache.ehcache.EhcacheCacheManager" p:cache-manager="ehcache"/>
<!-- Ehcache library setup -->
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="ehcache.xml"/>
Code language: HTML, XML (xml)
This setup bootstraps ehcache library inside Spring IoC (through bean ehcache) which is then wired into the dedicated CacheManager implementation. Note the entire ehcache-specific configuration is read from the resource ehcache.xml. Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
org.springframework.cache.ehcache.EhCacheCacheManager
NOT
org.springframework.cache.ehcache.EhcacheCacheManager
2 Corrections need to be made to the example above
the issues are:
1. EhCacheCacheManager (note the 2 C capitals)
2. p:cache-manager-ref="ehcache" (p:cache-manager-ref not p:cache-manager)
3. to use p:
I spent many trying to solve this.
Regards,
Rohid
How can we make it to reload all data into cache memory ??
Spring cache abstraction is broken, as it doesn't account for hash collisions, i.e. given below signature:
[code language="java"]
@Cacheable("myCache")
int a(int a, String s)
[/code]
a(1, "FB") will hash to the same key as a(1, "Ea"), when using the default key generator. The issue is that the cache abstraction doesn't take advantage of equality test. In other words, the key should be an object, which implements hashCode()/equals(), not a hash.
Hi Lukasz,
I am sure that using default key generation is not a good idea as it will always have a hashCode limitation. By all means we can create our own custom key using SePL which spring caching utilizes.
Regards
Niraj
Hi Viral,
This is an excellent post. Have you explored key generation strategies, will love to hear your experiences on them. I have written a similar post but I have used memcached as an example. I have used your post as reference. I hope you don't mind me putting this link here.
http://weblog4j.com/2013/05/31/simple-spring-memcached-spring-caching-abstraction-and-memcached/
Niraj
If my cache function has no arguments means, what will happen?
for example
@Cacheable(value="infortuniAttivitaDetail")
public List getInfortuniAttivitaDetail()
{
...
}
will it be a problem?
thanks
Hi Viral,
Do you have any example for spring cache with aspectj mode and compile time weaving?
I am using following configuration however caching is taking effect. Things work fine if I switch back to proxy mode...
Name of class is EhCacheCacheManager not this org.springframework.cache.ehcache.EhcacheCacheManager
and maven depency is
org.springframework
spring-context-support
3.2.2.RELEASE
http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/cache.html
Use [code language="java"]@org.springframework.cache.annotation.EnableCaching [/code] for Spring Java based configuration.
Hi viral
can you please give me an example to get data from database whileserver start up and store it in cache and we have to use when ever we want in programe by using spring cache can you please post an example or suggest me any example