Package valorless.rarespawns.databases
Class Cache
java.lang.Object
valorless.rarespawns.databases.Cache
- All Implemented Interfaces:
org.bukkit.event.Listener
Simple key-value cache system using a YAML file for storage.
This class provides static methods to initialize the cache, check for the existence of keys, retrieve values, and set values. The cache is backed by a YAML file located in the plugin's data folder. Values are stored as strings and should be serialized/deserialized to/from JSON format using JsonUtils when storing complex objects.
Usage
- Call
init()once during plugin startup to load or create the cache file. - Use
has(String)to check if a key exists in the cache. - Use
get(String)to retrieve a value by key (returns null if not found). - Use
set(String, String)to store a value by key (value must be JSON string).
Thread Safety
This class is not thread-safe. All interactions with the cache should occur on the main server thread.Failure / Edge Cases
- If the cache file cannot be created or read, an error will be logged but no exception is thrown.
- If a requested key does not exist,
get(String)returns null and logs an error.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static valorless.valorlessutils.config.ConfigThe configuration object for the cache. -
Constructor Summary
Constructors -
Method Summary
-
Field Details
-
cache
protected static valorless.valorlessutils.config.Config cacheThe configuration object for the cache. This is a static Config object that is used to read and write the cache.yml file.
-
-
Constructor Details
-
Cache
public Cache()
-
-
Method Details
-
init
public static void init()Initializes the cache by loading the cache.yml file. If the file does not exist, it will be created. This method should be called once during plugin startup. -
has
Checks if a cache entry exists for the given ID.- Parameters:
cacheId- The ID of the cache entry.- Returns:
- True if the cache contains an entry for the ID, false otherwise.
-
get
Retrieves a cache entry by ID.- Parameters:
cacheId- The ID of the cache entry.- Returns:
- The cached value as a string, or null if not found.
-
set
Sets a cache entry by ID.- Parameters:
cacheId- The ID of the cache entry.value- The value to store.
-