Cache Service

The CacheService manages all the caches in a BoxLang application

The Cache Service is the central management layer for all caching functionality in BoxLang. It acts as a registry and orchestrator for cache providers, handling their lifecycle, configuration, and providing a unified interface for cache operations across your application.

Overview

The Cache Service serves as the foundation of BoxLang's caching infrastructure by:

  • Managing Cache Providers: Registering, creating, and maintaining multiple cache instances

  • Lifecycle Coordination: Handling startup, shutdown, and maintenance operations

  • Provider Registry: Managing both core and custom cache provider implementations

  • Event Broadcasting: Announcing cache-related events throughout the system

  • Global Operations: Providing system-wide cache management capabilities

Core Concepts

Cache Providers vs Cache Service

  • Cache Providers (ICacheProvider): Individual cache instances that handle specific caching needs

  • Cache Service (CacheService): The service layer that manages multiple cache providers

Think of the Cache Service as a "cache manager" that coordinates multiple specialized cache instances within your application.

Service Lifecycle

The Cache Service follows BoxLang's service lifecycle pattern:

  1. Configuration Load: Reads cache configurations from BoxLang settings

  2. Startup: Creates and initializes all configured caches

  3. Operation: Provides access to registered caches during runtime

  4. Shutdown: Gracefully shuts down all cache providers

Getting Started

Accessing the Cache Service

The Cache Service is available through the cacheService() BIF

Working with Caches

Basic Cache Operations

Cache Validation

Cache Management

Creating Default Caches

The simplest way to create a new cache:

Creating Custom Provider Caches

Conditional Cache Creation

Manual Registration

If you have a pre-configured cache provider:

Cache Replacement

Individual Cache Shutdown

Bulk Operations

BoxLang BIF Integration

BoxLang provides convenient Built-in Functions (BIFs) that offer a developer-friendly interface to the Cache Service functionality. These BIFs abstract the Java API complexity and provide native BoxLang syntax for common caching operations.

Available Cache BIFs

BIF
Cache Service Equivalent
Purpose

cache()

cacheService.getCache()

Get a cache instance by name

cacheFilter()

N/A (Utility function)

Create filters for cache key operations

cacheNames()

cacheService.getRegisteredCaches()

List all registered cache names

cacheProviders()

cacheService.getRegisteredProviders()

List all registered cache providers

cacheService()

Direct access

Access the cache service directly

Basic Cache Operations

Service Discovery

Advanced Service Operations

Filter Operations

The equivalent direct service operations would require implementing ICacheKeyFilter or using the built-in filter classes.

BoxLang Application Integration

Mixed Java/BoxLang Usage

Advanced Usage

Task Scheduling Integration

The Cache Service provides a shared task scheduler for cache operations:

Event Integration

The Cache Service broadcasts events throughout cache lifecycles:

Error Handling

Monitoring and Diagnostics

Cache Inspection

Performance Monitoring

Best Practices

Cache Naming Conventions

Resource Management

Provider Selection

Error Recovery

Integration Patterns

Application Startup

Module Integration

Application Usage Patterns

Service Layer Integration

Request-Scoped Caching

Conclusion

The Cache Service provides a robust foundation for all caching needs in BoxLang applications. By centralizing cache management, it enables:

  • Scalable Architecture: Easy addition of new cache providers and instances

  • Consistent Interface: Uniform access patterns across different cache implementations

  • Lifecycle Management: Proper resource handling and cleanup

  • Event Integration: Seamless integration with BoxLang's event system

  • Flexible Configuration: Runtime and configuration-based cache setup

Whether you're using built-in providers or creating custom implementations, the Cache Service ensures your caching infrastructure remains manageable, performant, and maintainable as your application grows.

Last updated

Was this helpful?