CacheManager class

Cache manager that implements a clean facade pattern. Orchestrates all cache operations through modular managers following SOLID principles.

The CacheManager provides a unified interface for caching operations across different storage backends including memory, file system, and Redis. It supports multiple cache drivers, automatic TTL (time-to-live) management, and cache tagging for efficient cache invalidation.

Features

  • Modular Architecture: Uses dependency injection with separate managers for each responsibility
  • Multiple Drivers: Support for memory, file, hybrid, and Redis cache drivers
  • TTL Management: Automatic expiration of cached items
  • Cache Tags: Group related cache items for bulk invalidation
  • Configuration: Load cache settings from configuration files
  • Statistics: Track cache hits, misses, and performance metrics
  • Error Handling: Robust error handling with custom exceptions

Basic Usage

// Create managers
final registry = CacheDriverRegistry();
final statsManager = CacheStatisticsManager();
final tagManager = CacheTagManager();
final configLoader = CacheConfigLoader();
final validator = CacheValidator();

// Create cache manager
final cache = CacheManager(
  driverRegistry: registry,
  statisticsManager: statsManager,
  tagManager: tagManager,
  configLoader: configLoader,
  validator: validator,
);

// Register a memory cache driver
cache.registerDriver('memory', MemoryCacheDriver());
cache.setDefaultDriver('memory');

// Store and retrieve data
await cache.put('user:123', {'name': 'John', 'age': 30}, Duration(hours: 1));
final user = await cache.get('user:123');

// Use remember pattern
final data = await cache.remember('expensive_data', Duration(minutes: 30), () async {
  return await fetchExpensiveData();
});

Configuration

Cache drivers can be configured through the application's configuration:

cache:
  default: memory
  drivers:
    memory:
      driver: memory
    file:
      driver: file
      path: ./storage/cache
    redis:
      driver: redis
      host: localhost
      port: 6379
Implemented types

Constructors

CacheManager({required ICacheDriverRegistry driverRegistry, required ICacheStatisticsManager statisticsManager, required ICacheTagManager tagManager, required ICacheConfigLoader configLoader, required ICacheValidator validator})
Creates a new CacheManager with the required managers.

Properties

allStats Map<String, CacheStats>
Gets cache statistics for all drivers.
no setteroverride
defaultDriverName String
Gets the name of the current default driver.
no setteroverride
driverNames List<String>
Gets all registered driver names.
no setteroverride
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
stats CacheStats
Gets cache statistics for the default driver.
no setteroverride

Methods

add(String key, dynamic value, Duration ttl) Future<bool>
Stores a value in the cache if the key does not exist.
override
clear() Future<void>
Clears all items from the cache using the default driver.
override
decrement(String key, [int amount = 1]) Future<int>
Decrement the value of an item in the cache.
override
driver([String? name]) CacheDriver
Gets a specific cache driver instance.
override
forever(String key, dynamic value) Future<void>
Stores a value in the cache forever using the default driver.
override
forget(String key) Future<void>
Removes a value from the cache using the default driver.
override
forgetByTag(String tag) Future<void>
Removes all cache items associated with the given tag.
override
get(String key) Future
Retrieves a value from the cache using the default driver.
override
has(String key) Future<bool>
Checks if a key exists in the cache using the default driver.
override
increment(String key, [int amount = 1]) Future<int>
Increment the value of an item in the cache.
override
loadFromConfig(ConfigInterface config) → void
Loads cache configuration from the application's config.
many(List<String> keys) Future<Map<String, dynamic>>
Retrieves multiple values from the cache by their keys.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
pull(String key) Future
Retrieve an item from the cache and delete it.
override
put(String key, dynamic value, Duration ttl) Future<void>
Stores a value in the cache using the default driver.
override
putMany(Map<String, dynamic> values, Duration ttl) Future<void>
Stores multiple values in the cache.
override
registerDriver(String name, CacheDriver driver) → void
Registers a cache driver with the given name.
remember(String key, Duration ttl, Future callback()) Future
Retrieves a value from the cache or stores the default value if it doesn't exist.
override
setDefaultDriver(String name) → void
Sets the default cache driver.
store(String name) CacheDriver
Get a cache store instance by name.
override
tag(String key, List<String> tags) Future<void>
Tags a cache item with one or more tags for group invalidation.
override
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited