Databases Reference
In-Depth Information
Using Spring cache abstraction with
Spring Data Redis
The cache abstraction of Spring Framework 3.1 applies caching to Java methods.
When a cached method is called, the cache abstraction will check from the cache if
the method has been called earlier by using the same parameters. If this is the case,
then the return value is fetched from the cache and the method is not executed.
Otherwise, the method is executed and its return value is stored in the cache.
The cache abstraction of Spring Framework 3.1 is explained in
more detail at http://static.springsource.org/spring/
docs/3.1.x/spring-framework-reference/html/cache.html .
Spring Data Redis provides an implementation of the Spring cache abstraction.
Using Redis as a cache has two benefits over using local caching implementations
such as Ehcache:
• It can be used as a centralized cache that is shared by each servlet container
or application server that runs our application. This reduces the overall
number of database queries, which reduces the load of our database server
and increases the performance of all the servers.
• The cache will not be emptied until we empty it. This means that we
can restart our servlet container or application server without losing the
information stored in the cache. After our server is restarted, it can take full
advantage of the cached information right away. There is no need to warm
up the cache.
This section describes how we can use Spring Data Redis for the purpose of adding a
caching support to an application that uses the JPA Criteria API. This application has
been originally introduced in Chapter 3 , Building Queries with Spring Data JPA . The
requirements of our caching example are as follows:
• The method calls to the method that finds the information of a single contact
from the database must be cached
• When the information of a contact is updated, the information stored in the
cache must be updated as well
• When a contact is deleted, the deleted contact must be removed from
the cache
 
Search WWH ::




Custom Search