Class BoxCacheProvider
java.lang.Object
ortus.boxlang.runtime.cache.providers.AbstractCacheProvider
ortus.boxlang.runtime.cache.providers.BoxCacheProvider
- All Implemented Interfaces:
ICacheProvider
@BoxCache(alias="BoxLang",
distributed=false,
description="BoxLang\'s native cache provider using an object store.")
public class BoxCacheProvider
extends AbstractCacheProvider
BoxLang's native cache provider implementation that uses an object store for caching operations.
This cache provider supports automatic expiration, reaping, eviction policies, and provides
comprehensive caching functionality with performance monitoring and event announcements.
Key Features:
- Object store-based caching with configurable timeout and last access timeout
- Automatic reaping of expired entries via scheduled tasks
- Memory threshold and max objects eviction policies
- Performance statistics tracking (hits, misses, etc.)
- Event-driven architecture with cache lifecycle announcements
- Thread-safe operations with proper synchronization
- Flexible key filtering and batch operations
- Metadata reporting for cache monitoring and debugging
Configuration Properties:
maxObjects- Maximum number of objects allowed in cachedefaultTimeout- Default expiration timeout in secondsdefaultLastAccessTimeout- Default last access timeout in secondsreapFrequency- Frequency of reaping operations in secondsuseLastAccessTimeouts- Whether to use last access timeouts for eviction
Thread Safety:
This implementation is thread-safe and uses synchronization where necessary, particularly in the configure(), reap(), and getOrSet() methods to ensure data consistency.Usage Example:
// Basic operations cache.set( "key", "value", 3600 ); // Store with 1 hour timeout Optional<Object> value = cache.get( "key" ); boolean exists = cache.lookup( "key" ); cache.clear( "key" ); // Batch operations cache.set( Struct.of( "key1", "value1", "key2", "value2" ) ); IStruct results = cache.get( "key1", "key2", "key3" ); // Get-or-set pattern (thread-safe) Object value = cache.getOrSet( "expensiveKey", () -> computeExpensiveValue() );
- Since:
- 1.0.0
-
Field Summary
Fields inherited from class ortus.boxlang.runtime.cache.providers.AbstractCacheProvider
cacheService, config, enabled, interceptorPool, name, reportingEnabled, stats, TYPES -
Constructor Summary
ConstructorsConstructorDescription-------------------------------------------------------------------------- Constructor -------------------------------------------------------------------------- -
Method Summary
Modifier and TypeMethodDescriptionbooleanClears an object from the cache providerClears multiple objects from the cache providervoidclearAll()Clear all the elements in the cache providerbooleanclearAll(ICacheKeyFilter filter) Clear all the elements in the cache provider with a $ICacheKeyFilterpredicate.booleanclearQuiet(String key) Clears an object from the cache providerconfigure(CacheService cacheService, CacheConfig config) Configure the cache provider for operationGet an object from the store with metadata trackingGet multiple objects from the store with metadata trackingget(ICacheKeyFilter filter) Get multiple objects from the store with metadata tracking using a filterGet a cache objects metadata about its performance.getCacheEntry(String key) Get a raw ICacheEntry from the cache's object storegetCacheEntry(Key key) Get a raw ICacheEntry from the cache's object storegetKeys()Get all the keys in the cache providergetKeys(ICacheKeyFilter filter) Get all the keys in the cache provider using a filterGet all the keys in the cache provider as a streamgetKeysStream(ICacheKeyFilter filter) Get all the keys in the cache provider as a streamReturn the configured object store for this cache providerTries to get an object from the cache, if not found, it will call the lambda to get the value and store it in the cache with the default timeout and last access timeoutTries to get an object from the cache, if not found, it will call the lambda to get the value and store it in the cache with the default timeout and last access timeoutTries to get an object from the cache, if not found, it will call the lambda to get the value and store it in the cache with the default timeout and last access timeoutgetOrSet(String key, Supplier<Object> provider, Object timeout, Object lastAccessTimeout, IStruct metadata) Tries to get an object from the cache, if not found, it will call the lambda to get the value and store it in the cache with the default timeout and last access timeoutGet an object from cache with no metadata trackingGet the reaping futureintgetSize()Get the size of the cacheintgetSize(ICacheKeyFilter filter) Get the size of the cacheGet a key lookup structure where the BoxCache can build the report on.Get the store metadata report with no limitgetStoreMetadataReport(int limit) Get a structure of all the keys in the cache with their appropriate metadata structures.booleanCheck if an object is in the store or record a hit or miss in the statsCheck if multiple objects are in the storelookup(ICacheKeyFilter filter) Check if multiple objects are in the store using a filterbooleanlookupQuiet(String key) Check if an object is in the store with no stats updated or listenersvoidreap()Reap the cachevoidSets an object in the storage using the default timeout and last access timeoutvoidSets an object in the storage with a default last access timeoutvoidSets an object in the storagevoidSets an object in the storagevoidSet's multiple objects in the storage using all the same default timeout and last access timeoutsvoidSet's multiple objects in the storage using all the same default timeout and last access timeoutsvoidsetQuiet(Key key, ICacheEntry value) Sets an object in the storage with no announcements or eviction checksvoidshutdown()Shutdown the cache providerMethods inherited from class ortus.boxlang.runtime.cache.providers.AbstractCacheProvider
announce, announce, buildObjectStore, buildObjectStoreByClass, clearStats, getAsync, getCachedObjectMetadata, getCacheService, getConfig, getInterceptorPool, getName, getStats, getTaskScheduler, getType, isEnabled, isReportingEnabled, memoryThresholdCheck, setName, setName, toDuration, toDuration
-
Constructor Details
-
BoxCacheProvider
public BoxCacheProvider()-------------------------------------------------------------------------- Constructor --------------------------------------------------------------------------
-
-
Method Details
-
configure
Configure the cache provider for operation- Specified by:
configurein interfaceICacheProvider- Overrides:
configurein classAbstractCacheProvider- Parameters:
cacheService- The cache service that is configuring the cache providerconfig- The configuration object- Returns:
- The cache provider
-
getObjectStore
Return the configured object store for this cache provider -
shutdown
public void shutdown()Shutdown the cache provider -
getStoreMetadataReport
Get a structure of all the keys in the cache with their appropriate metadata structures. This is used to build the reporting for the cache provider Example:{ "key1": { "hits": 0, "lastAccessed": 0, "lastUpdated": 0, ... }, "key2": { "hits": 0, "lastAccessed": 0, "lastUpdated": 0, ... } }ThegetStoreMetadataKeyMapmethod is used to get the keys that this method returns as metadata in order to build the reports. Careful, this will be a large structure if the cache is large.- Parameters:
limit- The limit of keys to return, default is all keys or 0
-
getStoreMetadataReport
Get the store metadata report with no limit -
getStoreMetadataKeyMap
Get a key lookup structure where the BoxCache can build the report on. Ex:{ timeout=timeout, lastAccessTimeout=idleTimeout }It is a way for the visualizer to construct the columns correctly on the reports -
getCachedObjectMetadata
Get a cache objects metadata about its performance. This value is a structure of name-value pairs of metadata.- Parameters:
key- The key of the object- Returns:
- The metadata structure or an empty struct if the object is not found
-
reap
public void reap()Reap the cache -
getSize
public int getSize()Get the size of the cache -
getSize
Get the size of the cache -
clearAll
public void clearAll()Clear all the elements in the cache provider -
clearAll
Clear all the elements in the cache provider with a $ICacheKeyFilterpredicate. This can be a lambda or method reference since it's a functional interface.- Parameters:
filter- The filter that determines which keys to clear
-
clearQuiet
Clears an object from the cache provider- Parameters:
key- The object key to clear- Returns:
- True if the object was cleared, false otherwise (if the object was not found in the store)
-
clear
Clears an object from the cache provider- Parameters:
key- The object key to clear- Returns:
- True if the object was cleared, false otherwise (if the object was not found in the store)
-
clear
Clears multiple objects from the cache provider- Parameters:
keys- The keys to clear- Returns:
- A struct of keys and their clear status
-
getKeys
Get all the keys in the cache provider- Returns:
- An array of keys in the cache
-
getKeys
Get all the keys in the cache provider using a filter- Parameters:
filter- The filter that determines which keys to return- Returns:
- An array of keys in the cache
-
getKeysStream
Get all the keys in the cache provider as a stream- Returns:
- A stream of keys in the cache
-
getKeysStream
Get all the keys in the cache provider as a stream- Parameters:
filter- The filter that determines which keys to return- Returns:
- A stream of keys in the cache
-
lookupQuiet
Check if an object is in the store with no stats updated or listeners- Parameters:
key- The key to lookup in the store- Returns:
- True if the object is in the store, false otherwise
-
lookup
Check if an object is in the store or record a hit or miss in the stats- Parameters:
key- The key to lookup in the store- Returns:
- True if the object is in the store, false otherwise
-
lookup
Check if multiple objects are in the store- Parameters:
keys- A varargs of keys to lookup in the store- Returns:
- A struct of keys and their lookup status
-
lookup
Check if multiple objects are in the store using a filter- Parameters:
filter- The filter that determines which keys to return- Returns:
- A struct of keys and their lookup status
-
getQuiet
Get an object from cache with no metadata tracking- Parameters:
key- The key to retrieve- Returns:
- The cache entry retrieved or null
-
get
Get an object from the store with metadata tracking- Parameters:
key- The key to retrieve- Returns:
- The value retrieved or null
-
get
Get multiple objects from the store with metadata tracking- Parameters:
keys- The keys to retrieve- Returns:
- A struct of keys and their cache entries
-
get
Get multiple objects from the store with metadata tracking using a filter- Parameters:
filter- The filter that determines which keys to return- Returns:
- A struct of keys and their cache entries
-
setQuiet
Sets an object in the storage with no announcements or eviction checks- Parameters:
key- The key to storevalue- The value to store
-
set
public void set(String key, Object value, Object timeout, Object lastAccessTimeout, IStruct metadata) Sets an object in the storage -
set
Sets an object in the storage- Parameters:
key- The key to storevalue- The value to storetimeout- The timeout in secondslastAccessTimeout- The last access timeout in seconds
-
set
Sets an object in the storage with a default last access timeout- Parameters:
key- The key to storevalue- The value to storetimeout- The timeout in seconds
-
set
Sets an object in the storage using the default timeout and last access timeout- Parameters:
key- The key to storevalue- The value to store
-
set
Set's multiple objects in the storage using all the same default timeout and last access timeouts- Parameters:
entries- The keys and cache entries to store
-
set
Set's multiple objects in the storage using all the same default timeout and last access timeouts- Parameters:
entries- The keys and cache entries to store in the cachetimeout- The timeout in secondslastAccessTimeout- The last access timeout in seconds
-
getOrSet
public Object getOrSet(String key, Supplier<Object> provider, Object timeout, Object lastAccessTimeout, IStruct metadata) Tries to get an object from the cache, if not found, it will call the lambda to get the value and store it in the cache with the default timeout and last access timeoutThis is a convenience method to avoid the double lookup pattern
var value = cache.getOrSet( "myKey", () -> { return "myValue"; });This is the same as:
var value = cache.get( "myKey" ).orElseGet( () -> { var value = "myValue"; cache.set( "myKey", value ); return value; });This method is thread safe and will only call the lambda once if the key is not found in the cache
- Parameters:
key- The key to retrieveprovider- The lambda to call if the key is not foundtimeout- The timeout in secondslastAccessTimeout- The last access timeout in secondsmetadata- The metadata to store
-
getOrSet
public Object getOrSet(String key, Supplier<Object> provider, Object timeout, Object lastAccessTimeout) Tries to get an object from the cache, if not found, it will call the lambda to get the value and store it in the cache with the default timeout and last access timeout- Parameters:
key- The key to retrieveprovider- The lambda to call if the key is not foundtimeout- The timeout in secondslastAccessTimeout- The last access timeout in seconds- Returns:
- The object
-
getOrSet
Tries to get an object from the cache, if not found, it will call the lambda to get the value and store it in the cache with the default timeout and last access timeout- Parameters:
key- The key to retrieveprovider- The lambda to call if the key is not foundtimeout- The timeout in seconds- Returns:
- The object
-
getOrSet
Tries to get an object from the cache, if not found, it will call the lambda to get the value and store it in the cache with the default timeout and last access timeout- Parameters:
key- The key to retrieveprovider- The lambda to call if the key is not found- Returns:
- The object
-
getReapingFuture
Get the reaping future -
getCacheEntry
Get a raw ICacheEntry from the cache's object store- Parameters:
key- The key to retrieve- Returns:
- The ICacheEntry from the object store or null if not found
-
getCacheEntry
Get a raw ICacheEntry from the cache's object store- Parameters:
key- The key to retrieve- Returns:
- The ICacheEntry from the object store or null if not found
-