Package dev.crafty.core.config
Class CachedConfigObject<K,V>
java.lang.Object
dev.crafty.core.config.CachedConfigObject<K,V>
- Type Parameters:
K
- the type of the key used to identify objectsV
- the type of the value to be cached and serialized
An abstract class that provides a cached, file-backed configuration object.
This class uses a Caffeine cache to store objects in memory and synchronizes them with a YAML configuration file on disk. Subclasses or instances (via the Builder) must provide the config file, serializer, and config section.
- Since:
- 1.0.5
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Builder for creating instances ofCachedConfigObject
with custom configuration. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionGets a value from the cache, loading from config if not present.getAll()
Gets all values currently stored in the cache.protected abstract File
Returns the configuration file backing this cache.protected abstract String
Returns the section name in the config file where objects are stored.protected abstract Optional
<ConfigSerializer<V>> Returns the serializer used to (de)serialize objects to/from the config.protected abstract K
keyFromString
(String key) Converts a String key from the config to the correct key type.protected abstract String
keyToString
(K key) Converts a key of type K to a String for config storage.void
loadAll()
Loads all values from the config section into the cache, replacing any existing cache entries.void
Removes a value from the cache and the config file.void
Sets a value in the cache and saves it to the config file.
-
Constructor Details
-
CachedConfigObject
public CachedConfigObject()
-
-
Method Details
-
getConfigFile
Returns the configuration file backing this cache.- Returns:
- the config file
-
getSerializer
Returns the serializer used to (de)serialize objects to/from the config.- Returns:
- an optional serializer
-
getConfigSection
Returns the section name in the config file where objects are stored.- Returns:
- the config section name
-
keyFromString
Converts a String key from the config to the correct key type.- Parameters:
key
- the string key from config- Returns:
- the key of type K
-
keyToString
Converts a key of type K to a String for config storage.- Parameters:
key
- the key of type K
-
get
Gets a value from the cache, loading from config if not present.- Parameters:
key
- the key to look up- Returns:
- an optional containing the value if present
-
loadAll
public void loadAll()Loads all values from the config section into the cache, replacing any existing cache entries. -
getAll
Gets all values currently stored in the cache.- Returns:
- a list of all cached values
-
remove
Removes a value from the cache and the config file.- Parameters:
key
- the key to remove
-
set
Sets a value in the cache and saves it to the config file.- Parameters:
key
- the key to setvalue
- the value to associate with the key
-