Custom Cache Providers

Extend the BoxCache your way!

Overview

BoxLang's caching system provides a flexible architecture that allows you to create custom cache providers to integrate with any caching backend. Whether you want to integrate with Caffeine, Hazelcast, database storage, or any other caching solution, BoxLang's provider interface makes it straightforward.

Architecture Overview

The BoxLang cache system consists of several key components:

  • ICacheProvider - The main interface that all cache providers must implement

  • AbstractCacheProvider - An optional base class that provides common functionality

  • ICacheEntry - Interface representing individual cache entries

  • ICacheStats - Interface for tracking cache performance statistics

  • @BoxCache - Annotation for registering cache provider metadata

Required Interfaces

ICacheProvider

All cache providers must implement the ICacheProvider interface, which defines the contract for cache operations:

public interface ICacheProvider {
    // Configuration and lifecycle
    ICacheProvider configure( CacheService cacheService, CacheConfig config );
    void shutdown();
    
    // Basic operations
    Attempt<Object> get( String key );
    void set( String key, Object value );
    boolean clear( String key );
    void clearAll();
    
    // Bulk operations
    IStruct get( String... keys );
    void set( IStruct entries );
    
    // Cache management
    Array getKeys();
    boolean lookup( String key );
    int getSize();
    void reap();
    
    // Advanced operations
    Object getOrSet( String key, Supplier<Object> provider );
    CompletableFuture<Attempt<Object>> getAsync( String key );
    
    // Statistics and reporting
    ICacheStats getStats();
    IStruct getStoreMetadataReport();
}

ICacheStats

Cache providers must track performance statistics through the ICacheStats interface:

ICacheEntry

Cache entries encapsulate the stored data along with metadata:

Step-by-Step Implementation Guide

Step 1: Create the Provider Class

Create a new class that extends AbstractCacheProvider (recommended) or implements ICacheProvider directly:

Step 2: Implement Core Cache Operations

Step 3: Implement Bulk Operations

Step 4: Implement Advanced Features

Step 5: Implement Statistics and Reporting

Configuration

Provider Configuration

Define your cache provider configuration in the BoxLang configuration:

Configuration Properties

The Caffeine cache provider supports the following configuration properties:

  • maxObjects - Maximum number of entries in the cache (default: 10000)

  • defaultTimeout - Default expiration time in seconds (default: 3600)

  • defaultLastAccessTimeout - Default idle time before expiration in seconds (default: 1800)

  • reapFrequency - How often to run cleanup tasks in seconds (default: 300)

  • useLastAccessTimeouts - Whether to enforce idle timeouts (default: true)

  • recordStats - Enable Caffeine's built-in statistics recording (default: true)

  • weakKeys - Use weak references for keys (default: false)

  • weakValues - Use weak references for values (default: false)

  • softValues - Use soft references for values (default: false)

Registration

Register your custom provider with BoxLang:

Best Practices

1. Error Handling

Always wrap cache operations in try-catch blocks and provide fallback behavior:

2. Serialization

Implement robust serialization for complex objects when needed:

3. Resource Management

Implement proper resource cleanup and lifecycle management:

4. Thread Safety

Ensure your provider is thread-safe:

5. Performance Optimization

  • Use bulk operations when possible to reduce overhead

  • Implement efficient key filtering and streaming

  • Consider memory usage and implement appropriate eviction policies

  • Use async operations for better throughput when appropriate

6. Statistics and Monitoring

Implement comprehensive statistics tracking:

7. Configuration Validation

Validate configuration parameters during setup:

Last updated

Was this helpful?