Class Cache

java.lang.Object
valorless.rarespawns.databases.Cache
All Implemented Interfaces:
org.bukkit.event.Listener

public class Cache extends Object implements 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

    Fields
    Modifier and Type
    Field
    Description
    protected static valorless.valorlessutils.config.Config
    The configuration object for the cache.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    get(String cacheId)
    Retrieves a cache entry by ID.
    static boolean
    has(String cacheId)
    Checks if a cache entry exists for the given ID.
    static void
    Initializes the cache by loading the cache.yml file.
    static void
    set(String cacheId, String value)
    Sets a cache entry by ID.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • cache

      protected static valorless.valorlessutils.config.Config cache
      The 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

      public static boolean has(String cacheId)
      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

      public static String get(String cacheId)
      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

      public static void set(String cacheId, String value)
      Sets a cache entry by ID.
      Parameters:
      cacheId - The ID of the cache entry.
      value - The value to store.