BoxCache Provider

The BoxCache Provider is BoxLang's default, high-performance cache implementation.

The BoxCache Provider is BoxLang's default, high-performance cache implementation. It provides a comprehensive caching solution with pluggable object stores, configurable eviction policies, automatic maintenance, and extensive monitoring capabilities. The BoxCache Provider serves as the foundation for most caching needs in BoxLang applications.

Overview

The BoxCache Provider (BoxCacheProvider) is designed to be:

  • High Performance: Uses efficient concurrent data structures and async operations

  • Flexible Storage: Supports multiple object store backends (memory, disk, custom)

  • Intelligent Eviction: Configurable eviction policies (LRU, MRU, LFU, etc.)

  • Self-Maintaining: Automatic expiration, reaping, and memory management

  • Event-Driven: Comprehensive event broadcasting for cache operations

  • Thread-Safe: Fully concurrent and thread-safe for multi-threaded applications

  • Observable: Rich statistics and monitoring capabilities

Accessing the BoxCache Provider

The primary way to access BoxCache instances in BoxLang is through the cache() BIF:

// Get the default cache (usually BoxCache provider)
defaultCache = cache();

// Get a named BoxCache instance
userCache = cache("userSessions");
productCache = cache("productCatalog");

// Check cache type
println( "Cache type: " & cache().getType() ); // Outputs: BoxLang

Core Storage Operations

Setting Data

The BoxCache Provider offers multiple ways to store data with flexible timeout options:

Getting Data

Retrieve data with built-in null safety using the Attempt pattern. Please see the Attempt construct section.

Basic Retrieval Patterns

Functional Transformations

Validation and Type Safety

Side Effects and Actions

Error Handling and Recovery

Stream Operations

Advanced Functional Patterns

Bulk Retrieval with Functional Processing

Silent Retrieval and Internal Operations

Filtered Retrieval with Complex Processing

Get-or-Set Pattern

The BoxCache Provider includes the powerful getOrSet() method to eliminate double-lookup patterns:

Clearing Data

Remove data from the cache with various options:

Key Management and Filtering

Getting Cache Keys

Using Cache Filters

Cache filters provide powerful pattern matching for bulk operations:

Lookup Operations

Check for key existence without retrieving data:

Statistics and Monitoring

The BoxCache Provider includes comprehensive statistics tracking:

Configuration and Properties

Access and understand cache configuration:

Async Operations

BoxCache supports asynchronous operations for high-performance scenarios:

Cache Maintenance

Manual Maintenance Operations

Automatic Maintenance

BoxCache automatically handles:

  • Expiration Checking: Removes expired entries based on timeouts

  • Idle Timeout Management: Removes entries that haven't been accessed

  • Memory Threshold Eviction: Triggers eviction when memory usage is high

  • Scheduled Reaping: Periodic cleanup based on reapFrequency setting

Event Integration

BoxCache broadcasts events for all major operations:

Practical Usage Patterns

User Session Management

API Response Caching

Database Query Caching

Multi-Level Caching

Performance Optimization

Efficient Key Design

Batch Operations

Memory Management

Error Handling and Resilience

Graceful Degradation

Health Checks

Best Practices

1. Key Naming Conventions

2. Appropriate Timeouts

3. Memory Efficiency

4. Strategic Cache Warming

Conclusion

The BoxCache Provider offers a comprehensive, high-performance caching solution for BoxLang applications. By leveraging its rich feature set including flexible storage options, intelligent eviction policies, comprehensive monitoring, and powerful filtering capabilities, developers can build scalable, efficient applications.

Key advantages of BoxCache Provider:

  • Developer Friendly: Simple BIF-based access with powerful functionality

  • High Performance: Concurrent operations, async support, and efficient data structures

  • Flexible Configuration: Pluggable object stores and eviction policies

  • Production Ready: Comprehensive monitoring, event integration, and automatic maintenance

  • Scalable: Supports everything from simple key-value caching to complex multi-level architectures

Whether you're caching user sessions, API responses, database queries, or computed data, the BoxCache Provider provides the tools and flexibility needed to optimize your application's performance and scalability.

Last updated

Was this helpful?