Class DefaultSyncCache

java.lang.Object
io.micronaut.cache.caffeine.DefaultSyncCache
All Implemented Interfaces:
Cache<com.github.benmanes.caffeine.cache.Cache>, SyncCache<com.github.benmanes.caffeine.cache.Cache>

@EachBean(CacheConfiguration.class) public class DefaultSyncCache extends Object implements SyncCache<com.github.benmanes.caffeine.cache.Cache>

A default SyncCache implementation based on Caffeine

Since Caffeine is a non-blocking in-memory cache the SyncCache.async() method will return an implementation that runs operations in the current thread.

Since:
1.0
Author:
Graeme Rocher
  • Constructor Details

    • DefaultSyncCache

      public DefaultSyncCache(DefaultCacheConfiguration cacheConfiguration, io.micronaut.context.ApplicationContext applicationContext, io.micronaut.core.convert.ConversionService conversionService)
      Construct a sync cache implementation with given configurations.
      Parameters:
      cacheConfiguration - The cache configurations
      applicationContext - The application context
      conversionService - To convert the value from the cache into given required type
    • DefaultSyncCache

      @Inject public DefaultSyncCache(CacheConfiguration cacheConfiguration, io.micronaut.context.ApplicationContext applicationContext, io.micronaut.core.convert.ConversionService conversionService)
      Construct a sync cache implementation with given configurations.
      Parameters:
      cacheConfiguration - The cache configurations
      applicationContext - The application context
      conversionService - To convert the value from the cache into given required type
  • Method Details

    • getCacheInfo

      public org.reactivestreams.Publisher<CacheInfo> getCacheInfo()
      Specified by:
      getCacheInfo in interface Cache<com.github.benmanes.caffeine.cache.Cache>
      Returns:
      The cache information.
    • getName

      public String getName()
      Specified by:
      getName in interface Cache<com.github.benmanes.caffeine.cache.Cache>
      Returns:
      The name of the cache
    • getNativeCache

      public com.github.benmanes.caffeine.cache.Cache getNativeCache()
      Specified by:
      getNativeCache in interface Cache<com.github.benmanes.caffeine.cache.Cache>
      Returns:
      The native cache implementation
    • get

      public <T> Optional<T> get(Object key, io.micronaut.core.type.Argument<T> requiredType)
      Description copied from interface: SyncCache
      Resolve the given value for the given key.
      Specified by:
      get in interface SyncCache<com.github.benmanes.caffeine.cache.Cache>
      Type Parameters:
      T - The concrete type
      Parameters:
      key - The cache key
      requiredType - The required type
      Returns:
      An optional containing the value if it exists and is able to be converted to the specified type
    • get

      public <T> T get(Object key, io.micronaut.core.type.Argument<T> requiredType, Supplier<T> supplier)
      Description copied from interface: SyncCache
      Resolve the given value for the given key. If the value is not found the specified Supplier will be invoked and the return value cached.
      Specified by:
      get in interface SyncCache<com.github.benmanes.caffeine.cache.Cache>
      Type Parameters:
      T - The concrete type
      Parameters:
      key - The cache key
      requiredType - The required type
      supplier - The supplier that should be invoked if the value is not found
      Returns:
      An optional containing the value if it exists and is able to be converted to the specified type
    • invalidate

      public void invalidate(Object key)
      Description copied from interface: SyncCache
      Invalidate the value for the given key.
      Specified by:
      invalidate in interface SyncCache<com.github.benmanes.caffeine.cache.Cache>
      Parameters:
      key - The key to invalid
    • invalidateAll

      public void invalidateAll()
      Description copied from interface: SyncCache
      Invalidate all cached values within this cache.
      Specified by:
      invalidateAll in interface SyncCache<com.github.benmanes.caffeine.cache.Cache>
    • put

      public void put(@NonNull @NonNull Object key, @Nullable @Nullable Object value)

      Cache the specified value using the specified key. If the value is null, it will call invalidate(Object) passing the key

      Specified by:
      put in interface SyncCache<com.github.benmanes.caffeine.cache.Cache>
      Parameters:
      key - the key with which the specified value is to be associated
      value - the value to be associated with the specified key
    • putIfAbsent

      public <T> Optional<T> putIfAbsent(Object key, T value)
      Description copied from interface: SyncCache

      Cache the specified value using the specified key if it is not already present.

      Specified by:
      putIfAbsent in interface SyncCache<com.github.benmanes.caffeine.cache.Cache>
      Type Parameters:
      T - The concrete type
      Parameters:
      key - the key with which the specified value is to be associated
      value - the value to be associated with the specified key
      Returns:
      An optional of the existing value or Optional.empty() if the specified value parameter was cached
    • putIfAbsent

      public <T> T putIfAbsent(Object key, Supplier<T> value)
      Description copied from interface: SyncCache

      Cache the supplied value using the specified key if it is not already present.

      Specified by:
      putIfAbsent in interface SyncCache<com.github.benmanes.caffeine.cache.Cache>
      Type Parameters:
      T - The concrete type
      Parameters:
      key - the key with which the specified value is to be associated
      value - the value supplier to be associated with the specified key
      Returns:
      An optional of the existing value or the new value returned by the supplier
    • buildCache

      protected com.github.benmanes.caffeine.cache.Cache buildCache(CacheConfiguration cacheConfiguration)
      Build a cache from the given configurations.
      Parameters:
      cacheConfiguration - The cache configurations
      Returns:
      cache