Custom Eviction Policies

Eviction policies determine which cached objects should be removed when the cache reaches its capacity limits or memory thresholds.

Eviction policies determine which cached objects should be removed when the cache reaches its capacity limits or memory thresholds. While BoxLang provides several built-in eviction policies, you can create custom policies to implement specialized eviction strategies tailored to your application's specific needs.

Understanding Eviction Policies

What are Eviction Policies?

Eviction policies are algorithms that decide which cached entries to remove when:

  • The cache reaches its maximum object count (maxObjects)

  • Memory usage exceeds the configured threshold (freeMemoryPercentageThreshold)

  • Manual eviction is triggered through cache maintenance operations

When Eviction Occurs

Eviction is triggered automatically by the cache system during:

  • Object Storage: When adding new items would exceed cache limits

  • Memory Pressure: When JVM memory usage crosses the configured threshold

  • Scheduled Maintenance: During periodic reaping operations

  • Manual Triggers: When explicitly calling eviction methods

Policy Selection Impact

The choice of eviction policy significantly affects:

  • Cache Hit Rates: How often requested data is found in the cache

  • Application Performance: Response times for cached vs. uncached operations

  • Memory Efficiency: How well the cache utilizes available memory

  • Data Consistency: Whether important data remains accessible

Built-in Eviction Policies

BoxLang includes seven core eviction policies:

Policy
Purpose

LRU (Least Recently Used)

Evicts the objects that haven't been accessed for the longest time. Best for applications with temporal locality where recently accessed items are more likely to be accessed again. Ideal for general-purpose caching scenarios.

MRU (Most Recently Used)

Evicts the most recently accessed objects first. Useful when you want to keep older, established data and remove newly added items. Good for scenarios where recent additions are less valuable than historical data.

LFU (Least Frequently Used)

Evicts objects with the lowest access frequency count. Maintains items that are accessed often, regardless of when they were last accessed. Perfect for caching frequently requested data like popular products or common API responses.

MFU (Most Frequently Used)

Evicts the most frequently accessed objects first. Counterintuitive but useful in scenarios where you want to cycle out "hot" data to make room for less popular items that might become important. Rare use case but valuable for specialized applications.

FIFO (First In, First Out)

Evicts objects in the order they were added to the cache. Simple queue-based eviction that doesn't consider access patterns. Good for time-sensitive data where older entries naturally become less relevant, like news feeds or log entries.

LIFO (Last In, First Out)

Evicts the most recently added objects first, like a stack. Keeps older established data while removing newer additions. Useful when you want to maintain a stable core dataset and only temporarily cache additional items.

Random

Evicts objects randomly without considering access patterns or insertion order. Provides consistent average performance without the overhead of tracking access metadata. Good for scenarios where no clear access pattern exists or when you want to avoid worst-case behaviors of other policies.

The ICachePolicy Interface

To create a custom eviction policy, you must implement the ICachePolicy interface:

Creating Custom Eviction Policies

Language Support

Custom eviction policies can be implemented in both Java and BoxLang:

  • Java: Standard Java class implementation using the ICachePolicy interface

  • BoxLang: BoxLang component using the implements="java:classpath" approach to implement the Java interface

BoxLang Implementation Example

Step 1: Implement the Interface (Java)

Step 2: Implement Selection Logic

Here are several examples of custom eviction policies:

Size-Based Eviction Policy

Evicts the largest objects first to free up the most memory:

Priority-Based Eviction Policy

Evicts objects based on custom priority metadata:

Time-Window Eviction Policy

Evicts objects based on time windows and access patterns:

Step 3: Advanced Pattern - Adaptive Policy

An adaptive policy that changes behavior based on cache performance:

Configuration

You can register and use your custom eviction policies via the caches configuration section by putting the full Java class path of the object store as the evictionPolicy key

Or you can use the createCache( name, provider, propertes ) method on the CacheService and pass in a class or object instance for the evictionPolicy

Testing Custom Policies

Unit Testing Framework

Performance Testing

Integration Testing

Best Practices

Performance Considerations

  1. Efficient Sorting: Use efficient sorting algorithms for large key sets

  2. Minimize Object Creation: Reuse objects and avoid unnecessary allocations

  3. Lazy Evaluation: Only calculate expensive metrics when needed

  4. Batch Operations: Process multiple eviction candidates efficiently

Memory Management

Configuration Validation

Error Handling

Conclusion

Custom eviction policies provide powerful control over cache behavior, allowing you to optimize for your specific application patterns and requirements. By implementing the ICachePolicy interface, you can create sophisticated eviction strategies that consider multiple factors such as object size, access patterns, business priority, and temporal characteristics.

Key benefits of custom eviction policies include:

  • Tailored Performance: Optimize cache hit rates for your specific data access patterns

  • Business Logic Integration: Incorporate application-specific priorities and rules

  • Advanced Algorithms: Implement cutting-edge eviction algorithms from research

  • Adaptive Behavior: Create policies that adapt to changing conditions

  • Multi-Criteria Optimization: Balance multiple factors in eviction decisions

Whether you need simple priority-based eviction or complex adaptive algorithms, BoxLang's eviction policy framework provides the flexibility to implement precisely the caching behavior your application requires.

Last updated

Was this helpful?