Synchronous And Asynchronous Caching

Published On: 26 December 2022.By .
  • General

As we discussed what is Caching and merits and demerits of it in my previous blog Caching-I. In this, we will learn about how to do Synchronous caching using annotations and Asynchronous caching using Caffeine.

Synchronous Caching :

We are doing the synchronous caching using the annotations with simple spring boot project.

  • Dependency to be used :
  • To enable the caching use @EnableCaching annotation to any of the configuration class
  • We can customize the cache manager bean like we can set the time after which the value will expire from the cache and we can also set the maximum size of the cache and many more things.
  • Here we will also set the name of the cache that will pass in the @Cacheable annotation.
  • Next step is to bind the behaviour of caching with its methods:

1. @Cacheable : annotation used for adding the entity in the cache.

2. @CachePut : annotation used for updating or adding the particular entity in the cache.

3. @CacheEvict : used to flush the cache.

When we hit this request, firstly it will call the service and stores our value in the cache name “Activity-cache” and when we call the request again then it fetches the value from the cache if it does not expire from the cache.

In this way we do the synchronous caching.

Asynchronous Caching :

We can also do the Asynchronous Caching by using annotation :

Why we prefer Caffeine Cache for Asynchronous Caching:

Caffeine is a high-performance Java caching library providing a near optimal hit rate.

A Caffeine is similar to ConcurrentMap, but not quite the same. The most fundamental difference is that a ConcurrentMap persists all elements that are added to it until they are explicitly removed. A Caffeine on the other hand is generally configured to evict entries automatically, in order to constrain its memory footprint. In some cases AsyncLoadingCache can be useful even if it doesn’t evict entries, due to its automatic cache loading.

  • Dependecy to be used :
  • We have to make the bean of the Asynchronous cache and we can set the size and the expiry time of the cache values
  • Now, we will autowire the object of the Async cache.
  • And we will store the value in the cache by using the .put method which accepts the 2 parameters- first is the I’d and the second is value to be cached.
  • To fetch the value from cache we have to pass the I’d in the .getIfPresent method.
In this way we do the Asynchronous caching using caffeine.

Related content

That’s all for this blog