Interface SyncCache<C>

  • Type Parameters:
    C - The native cache implementation
    All Superinterfaces:
    Cache<C>
    All Known Implementing Classes:
    AbstractMapBasedSyncCache, JCacheSyncCache

    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
    See Also:
    Cache, AsyncCache
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      default AsyncCache<C> async()
      This method returns an async version of this cache interface implementation.
      <T> java.util.Optional<T> get​(java.lang.Object key, io.micronaut.core.type.Argument<T> requiredType)
      Resolve the given value for the given key.
      <T> T get​(java.lang.Object key, io.micronaut.core.type.Argument<T> requiredType, java.util.function.Supplier<T> supplier)
      Resolve the given value for the given key.
      default <T> java.util.Optional<T> get​(java.lang.Object key, java.lang.Class<T> requiredType)
      Resolve the given value for the given key.
      default <T> T get​(java.lang.Object key, java.lang.Class<T> requiredType, java.util.function.Supplier<T> supplier)
      Resolve the given value for the given key.
      default java.util.concurrent.ExecutorService getExecutorService()  
      void invalidate​(java.lang.Object key)
      Invalidate the value for the given key.
      void invalidateAll()
      Invalidate all cached values within this cache.
      void put​(java.lang.Object key, java.lang.Object value)
      Cache the specified value using the specified key.
      default <T> T putIfAbsent​(java.lang.Object key, java.util.function.Supplier<T> value)
      Cache the supplied value using the specified key if it is not already present.
      <T> java.util.Optional<T> putIfAbsent​(java.lang.Object key, T value)
      Cache the specified value using the specified key if it is not already present.
    • Method Detail

      • get

        @NonNull
        <T> java.util.Optional<T> get​(@NonNull
                                      java.lang.Object key,
                                      @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
                  java.lang.Object key,
                  @NonNull
                  io.micronaut.core.type.Argument<T> requiredType,
                  @NonNull
                  java.util.function.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> java.util.Optional<T> putIfAbsent​(@NonNull
                                              java.lang.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
                                  java.lang.Object key,
                                  @NonNull
                                  java.util.function.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
                 java.lang.Object key,
                 @NonNull
                 java.lang.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
                        java.lang.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
                          java.lang.Object key,
                          @NonNull
                          java.lang.Class<T> requiredType,
                          @NonNull
                          java.util.function.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> java.util.Optional<T> get​(@NonNull
                                              java.lang.Object key,
                                              @NonNull
                                              java.lang.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 java.util.concurrent.ExecutorService getExecutorService()
        Returns:
        The executor service used to construct the default asynchronous cache.
      • async

        @NonNull
        default 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