Interface SyncCache<C>

Type Parameters:
C - The native cache implementation
All Superinterfaces:
Cache<C>
All Known Implementing Classes:
AbstractMapBasedSyncCache, DefaultSyncCache, EhcacheSyncCache, HazelcastSyncCache, InfinispanSyncCache, JCacheSyncCache, NoOpSyncCache

public interface SyncCache<C> extends Cache<C>

A synchronous API for accessing cache values that is useful for in-memory caching implementations.

Caching implementations that require blocking IO should implement the getExecutorService() method to provide an executor service to offload the operations to. If the cache natively supports asynchronous operations, override the async() method to provide a more customized asynchronous solution.

Implementers of this interface should mark the implementation as Blocking if a blocking operation is required to read or write cache values

Since:
1.0
Author:
Graeme Rocher
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    default @NonNull AsyncCache<C>
    This method returns an async version of this cache interface implementation.
    <T> @NonNull Optional<T>
    get(@NonNull Object key, @NonNull io.micronaut.core.type.Argument<T> requiredType)
    Resolve the given value for the given key.
    <T> T
    get(@NonNull Object key, @NonNull io.micronaut.core.type.Argument<T> requiredType, @NonNull Supplier<T> supplier)
    Resolve the given value for the given key.
    default <T> @NonNull Optional<T>
    get(@NonNull Object key, @NonNull Class<T> requiredType)
    Resolve the given value for the given key.
    default <T> T
    get(@NonNull Object key, @NonNull Class<T> requiredType, @NonNull Supplier<T> supplier)
    Resolve the given value for the given key.
    default @Nullable ExecutorService
     
    void
    invalidate(@NonNull Object key)
    Invalidate the value for the given key.
    void
    Invalidate all cached values within this cache.
    void
    put(@NonNull Object key, @NonNull Object value)
    Cache the specified value using the specified key.
    default <T> T
    putIfAbsent(@NonNull Object key, @NonNull Supplier<T> value)
    Cache the supplied value using the specified key if it is not already present.
    <T> @NonNull Optional<T>
    putIfAbsent(@NonNull Object key, T value)
    Cache the specified value using the specified key if it is not already present.

    Methods inherited from interface io.micronaut.cache.Cache

    getCacheInfo, getName, getNativeCache
  • Method Details

    • get

      @NonNull <T> @NonNull Optional<T> get(@NonNull @NonNull Object key, @NonNull @NonNull io.micronaut.core.type.Argument<T> requiredType)
      Resolve the given value for the given key.
      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

      <T> T get(@NonNull @NonNull Object key, @NonNull @NonNull io.micronaut.core.type.Argument<T> requiredType, @NonNull @NonNull Supplier<T> supplier)
      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.
      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
    • putIfAbsent

      @NonNull <T> @NonNull Optional<T> putIfAbsent(@NonNull @NonNull Object key, @NonNull T value)

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

      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

      @NonNull default <T> T putIfAbsent(@NonNull @NonNull Object key, @NonNull @NonNull Supplier<T> value)

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

      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
    • put

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

      Cache the specified value using the specified key.

      Parameters:
      key - the key with which the specified value is to be associated
      value - the value to be associated with the specified key
    • invalidate

      void invalidate(@NonNull @NonNull Object key)
      Invalidate the value for the given key.
      Parameters:
      key - The key to invalid
    • invalidateAll

      void invalidateAll()
      Invalidate all cached values within this cache.
    • get

      default <T> T get(@NonNull @NonNull Object key, @NonNull @NonNull Class<T> requiredType, @NonNull @NonNull Supplier<T> supplier)
      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.
      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
    • get

      @NonNull default <T> @NonNull Optional<T> get(@NonNull @NonNull Object key, @NonNull @NonNull Class<T> requiredType)
      Resolve the given value for the given key.
      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
    • getExecutorService

      @Nullable default @Nullable ExecutorService getExecutorService()
      Returns:
      The executor service used to construct the default asynchronous cache.
    • async

      @NonNull default @NonNull AsyncCache<C> async()

      This method returns an async version of this cache interface implementation.

      The default behaviour will execute the operations in the same thread if null is returned from getExecutorService(). If an executor service is returned, the operations will be offloaded to the provided executor service.

      Returns:
      The AsyncCache implementation for this cache