📜 ⬆️ ⬇️

Caching in the Spring Framework 3.1

I may be mistaken, but it seems to me that the well-known Spring Framework reached its peak for version 2.5 (when the active use of annotations was implemented) and the “polishing” goes essentially - even the major release 3.0 is not much different from 2.5. The same can be said about the upcoming 3.1 - small improvements, phishchechki - but no more. However, one "fishechka" in 3.1 seemed to me especially interesting - this is caching.
image

In any application under development, we may at some point encounter a performance problem. One way to solve problems is caching. Naturally, you can cache the data returned from the database (Hibernate - the most used in java makes it so) - but much more interesting is the caching of the values ​​returned from the “services” level. Calculating such values ​​can be quite time-consuming, including many calls to the database, other services, calculations — and, accordingly, caching at this level can lead to more significant results.
In the Spring Framework 3.1, this can be done using the @Cacheable annotation .
In fact, service level caching could have been done in the Spring Framework before, just now it will be much easier.
Value caching

For example, if during the profiling you find out that a particular method is “stupid”, for example (as in the documentation), the book return method on the ISBN:

public Book findBook(ISBN isbn) {...}

Now, in order to cache the values ​​returned by this method, just add an annotation:

@Cacheable("books")
public Book findBook(ISBN isbn) {...}

This annotation will hang the interceptor itself, create a key by parameters, check if there is a value in the cache, if not, call the method and cache the result.
')
And it's all? Almost for completeness you need:
Configure application caching

This is done in the application context xml configuration file:

<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 www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/cache www.springframework.org/schema/cache/spring-cache.xsd">
<cache:annotation-driven />

and configure cache-manager — for example, the simplest one (using ConcurrentMap)

<bean class="org.springframework.cache.concurrent.ConcurrentCacheFactoryBean" p:name="default"/>
<bean class="org.springframework.cache.concurrent.ConcurrentCacheFactoryBean" p:name="books"/>




or ehcache (you can use other implementations):

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhcacheCacheManager" p:cache-manager="ehcache"/>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="ehcache.xml"/>

Cleaning cache

It is necessary to cache data carefully - it is always a potential source of errors when the data is updated, but the cache is not there and the application uses outdated data. Cache clearing can also be performed using the annotation - @CacheEvict:

@CacheEvict(value = "books", allEntries=true)
public void loadBooks(InputStream batch)


That's all. Simply? It seems yes. In fact, the caching mechanism is quite flexible, there are a number of additional features (such as writing your own annotations, generating keys, and much more. But the basic functionality looks so simple.

Source: https://habr.com/ru/post/113945/


All Articles