Quarkus cache annotations with explanation!

Quarkus cache annotations with explanation!

Table of contents

No heading

No headings in the article.

Quarkus - Hello world example using Kotlin!

In this article, you are going to learn some commonly used caching annotations in Quarkus:

  • @CacheResult: This annotation is used to cache the result of a method. The result is stored in the cache using the method parameters as the cache key. Subsequent calls to the method with the same parameters will return the cached result instead of executing the method again.

  •   import javax.cache.Cache;
      import javax.inject.Inject;
      import org.eclipse.microprofile.config.inject.ConfigProperty;
      import io.quarkus.cache.CacheResult;
    
      public class MyService {
    
          @Inject
          Cache<String, String> myCache;
    
          @ConfigProperty(name = "my.service.cache.duration")
          long cacheDuration;
    
          @CacheResult(cacheName = "myCache")
          public String getCachedData(String key) {
              // This method's result will be cached for 'cacheDuration' milliseconds
              return myCache.get(key);
          }
      }
    
  • @CacheInvalidate: This annotation is used to invalidate the cache entries that match a specific key or set of keys. It can be used to trigger cache invalidation based on some external event, such as a database update.

import io.quarkus.cache.CacheInvalidate;

public class MyService {

    @CacheInvalidate(cacheName = "myCache")
    public void invalidateCache(String key) {
        // This method will invalidate the cache entry with the given key
    }
}
  • @CacheInvalidateAll: This annotation is used to invalidate all cache entries associated with a specific cache name. It can be used to clear the cache when the underlying data changes.
import io.quarkus.cache.CacheInvalidateAll;

public class MyService {

    @CacheInvalidateAll(cacheName = "myCache")
    public void invalidateCache() {
        // This method will invalidate all entries in the cache
    }
}
  • @CacheKey: This annotation is used to specify which parameters of a method should be used as the cache key. By default, all parameters are used, but this annotation allows you to specify a subset of the parameters to use as the cache key.
import io.quarkus.cache.CacheResult;
import io.quarkus.cache.CacheKey;

public class MyService {

    @CacheResult(cacheName = "myCache")
    public String getCachedData(@CacheKey String key) {
        // This method's result will be cached using the provided key
        return getData(key);
    }
}
  • @CachePut: This annotation is used to update the cached value associated with a specific cache key. It can be used to update the cache when the underlying data changes.
import io.quarkus.cache.CachePut;
import io.quarkus.cache.CacheResult;

public class MyService {

    @CachePut(cacheName = "myCache", key = "{#key}")
    public void updateCache(String key, String value) {
        // This method will update the cached value associated with the specified key
        // If the key is not present in the cache, a new entry will be created
        myCache.put(key, value);
    }

    @CacheResult(cacheName = "myCache", key = "{#key}")
    public String getCachedData(String key) {
        // This method will retrieve the cached value associated with the specified key
        // If the key is not present in the cache, it will return null
        return myCache.get(key);
    }
}
  • @CacheResultGroup: This annotation is used to group cache results together based on a common key. This can be useful when you have multiple methods that generate related data, and you want to cache them together.
import io.quarkus.cache.CacheResult;
import io.quarkus.cache.CacheResultGroup;
import io.quarkus.cache.CacheKey;

public class MyService {

    @CacheResult(cacheName = "myCache", key = "{#userId}")
    public List<String> getCachedDataForUser(@CacheKey String userId) {
        // This method will retrieve the cached data associated with the specified user ID
        // If the data is not present in the cache, it will be retrieved from the database and cached
        return getDataFromDatabase(userId);
    }

    @CacheResultGroup(cacheName = "myCache", group = "allUsers", key = "{#prefix}")
    public List<String> getCachedDataForAllUsers(@CacheKey String prefix) {
        // This method will retrieve the cached data associated with all users whose ID starts with the specified prefix
        // If the data is not present in the cache, it will be retrieved from the database and cached
        return getDataFromDatabaseForAllUsers(prefix);
    }
}

These caching annotations are provided by Quarkus Cache Extension, which supports several caching providers such as Infinispan, Redis, and Caffeine.

I hope this helps, you!!

More such articles:

https://medium.com/techwasti

https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA

https://www.techwasti.com/

\==========================**=========================

If this article adds any value to you then please clap and comment.

Let’s connect on Stackoverflow, LinkedIn, & Twitter.

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!