Class CachedStorageProvider<T,K>

java.lang.Object
dev.crafty.core.storage.CachedStorageProvider<T,K>
Type Parameters:
T - The type of object to store
K - The type of key used to identify objects
All Implemented Interfaces:
StorageProvider<T,K>

public class CachedStorageProvider<T,K> extends Object implements StorageProvider<T,K>
A decorator that adds caching to any storage provider. Uses Caffeine for high-performance caching.
Since:
1.0.0
  • Constructor Details

    • CachedStorageProvider

      public CachedStorageProvider(StorageProvider<T,K> delegate)
      Creates a new CachedStorageProvider with default cache settings.
      Parameters:
      delegate - The storage provider to delegate to
    • CachedStorageProvider

      public CachedStorageProvider(StorageProvider<T,K> delegate, com.github.benmanes.caffeine.cache.Caffeine<Object,Object> cacheBuilder, com.github.benmanes.caffeine.cache.Caffeine<Object,Object> collectionCacheBuilder)
      Creates a new CachedStorageProvider with custom cache settings.
      Parameters:
      delegate - The storage provider to delegate to
      cacheBuilder - The builder for the item cache
      collectionCacheBuilder - The builder for the collection cache
  • Method Details

    • save

      public CompletableFuture<Void> save(K key, T value)
      Saves an object to the storage.
      Specified by:
      save in interface StorageProvider<T,K>
      Parameters:
      key - The key to identify the object
      value - The object to save
      Returns:
      A CompletableFuture that completes when the operation is done
    • get

      public CompletableFuture<Optional<T>> get(K key)
      Retrieves an object from the storage.
      Specified by:
      get in interface StorageProvider<T,K>
      Parameters:
      key - The key of the object to retrieve
      Returns:
      A CompletableFuture that completes with the retrieved object, or empty if not found
    • getAll

      public CompletableFuture<Collection<T>> getAll()
      Retrieves all objects from the storage.
      Specified by:
      getAll in interface StorageProvider<T,K>
      Returns:
      A CompletableFuture that completes with a collection of all objects
    • delete

      public CompletableFuture<Void> delete(K key)
      Deletes an object from the storage.
      Specified by:
      delete in interface StorageProvider<T,K>
      Parameters:
      key - The key of the object to delete
      Returns:
      A CompletableFuture that completes when the operation is done
    • exists

      public CompletableFuture<Boolean> exists(K key)
      Checks if an object exists in the storage.
      Specified by:
      exists in interface StorageProvider<T,K>
      Parameters:
      key - The key to check
      Returns:
      A CompletableFuture that completes with true if the object exists, false otherwise
    • initialize

      public CompletableFuture<Void> initialize()
      Initializes the storage provider. This method should be called before using the provider.
      Specified by:
      initialize in interface StorageProvider<T,K>
      Returns:
      A CompletableFuture that completes when initialization is done
    • close

      public CompletableFuture<Void> close()
      Closes the storage provider and releases any resources. This method should be called when the provider is no longer needed.
      Specified by:
      close in interface StorageProvider<T,K>
      Returns:
      A CompletableFuture that completes when the provider is closed
    • clear

      public CompletableFuture<Void> clear()
      Clears all cached entries from both the item and collection caches.

      This method invalidates all entries in the internal caches, effectively resetting the cache state. It does not affect the underlying storage provider.

      Returns:
      a CompletableFuture that is already completed when the caches are cleared