Class MySqlStorageProvider<T>

java.lang.Object
dev.crafty.core.storage.AbstractStorageProvider<T,String>
dev.crafty.core.storage.providers.MySqlStorageProvider<T>
Type Parameters:
T - The type of object to store
All Implemented Interfaces:
StorageProvider<T,String>

public class MySqlStorageProvider<T> extends AbstractStorageProvider<T,String>
A storage provider that stores objects in a MySQL database. Objects are serialized to JSON before storage.
Since:
1.0.0
  • Constructor Details

    • MySqlStorageProvider

      public MySqlStorageProvider(Class<T> valueType, String tableName, String connectionUrl, String username, String password)
      Creates a new MySqlStorageProvider.
      Parameters:
      valueType - The class of the value type
      tableName - The name of the table to store objects in
      connectionUrl - The JDBC connection URL
      username - The database username
      password - The database password
  • Method Details

    • 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,String>
      Overrides:
      initialize in class AbstractStorageProvider<T,String>
      Returns:
      A CompletableFuture that completes when initialization is done
    • save

      public CompletableFuture<Void> save(String key, T value)
      Saves an object to the storage.
      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(String key)
      Retrieves an object from the storage.
      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.
      Returns:
      A CompletableFuture that completes with a collection of all objects
    • delete

      public CompletableFuture<Void> delete(String key)
      Deletes an object from the storage.
      Parameters:
      key - The key of the object to delete
      Returns:
      A CompletableFuture that completes when the operation 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,String>
      Overrides:
      close in class AbstractStorageProvider<T,String>
      Returns:
      A CompletableFuture that completes when the provider is closed