Skip to main content

Cache Management

Overview

Cache management in the DomainFlow Container provides a mechanism to store and retrieve resolved service instances, reducing redundant instantiation and improving performance. The system integrates both an internal, in-memory cache and external cache systems (via the ContainerCacheInterface), ensuring that once a service is resolved, it can be reused efficiently.


Key Concepts

Internal Caching:

The container maintains an internal cache of resolved services using an associative array (e.g., $resolvedServicesCache). Once a service is resolved, it is stored for faster future access.

External Caching:

An external cache store can be integrated by implementing the ContainerCacheInterface. This allows caching service instances beyond a single request or process, providing persistence or distributed caching as needed.

Cache Manager Trait:

The caching functionality is managed through a dedicated trait. This trait provides methods to cache, retrieve, and clear resolved services, seamlessly bridging internal caching with any external cache system you set up.


Implementation Details

ContainerCacheInterface

This interface defines the contract for external caching. The key methods include:

MethodSignatureDescription
getget(string $key): mixedRetrieves a value from the cache.
setset(string $key, mixed $value, int $ttl = 3600): boolStores a value with a specified time-to-live.
hashas(string $key): boolChecks if a cached value exists for the key.
deletedelete(string $key): boolRemoves a value from the cache.

InMemoryContainerCache

This is a simple in-memory implementation of the ContainerCacheInterface that uses a PHP array to support basic cache operations. It is useful when persistent caching is not required.

CacheManager Trait

The cache management trait in DomainFlow provides key functionalities such as:

Caching Resolved Services: Method like cacheResolvedService(string $abstract, mixed $instance) caches a resolved service for quicker future access.

Retrieving Cached Services: Method cacheResolvedServices() returns all cached service instances for inspection or further processing.

Clearing the Cache: The clearResolvedServicesCache(Closure $cacheKey) method clears the internal cache and, if an external cache is configured, persists the cache using a unique key generated by the provided closure.


Example Usage

Below is an example demonstrating how to use cache management within your container:

$container = new Container();

// Optionally set an external cache store (e.g., an in-memory cache)
$externalCache = new InMemoryContainerCache();
$container->setExternalCache($externalCache);

// Resolve a service, which is then cached internally
$service = $container->get(SomeService::class);
$container->cacheResolvedService(SomeService::class, $service);

// Retrieve and inspect all cached services
$cachedServices = $container->cacheResolvedServices();
print_r($cachedServices);

// Clear the internal cache and persist it externally using a unique key
$container->clearResolvedServicesCache(function() {
return 'unique_cache_key';
});

In this example:

  • An external cache store is assigned to the container.
  • When a service (e.g., SomeService) is resolved, it is cached internally using cacheResolvedService().
  • The internal cache can be retrieved for inspection with cacheResolvedServices().
  • The cache is cleared using clearResolvedServicesCache(), which also saves the cache externally with a unique key.

Benefits of Cache Management

  • Performance Optimization:
    Reduces the overhead of repeatedly constructing the same service by reusing resolved instances.

  • Resource Efficiency:
    Minimizes redundant object creation, leading to more efficient memory usage.

  • Flexibility:
    Supports both internal and external caching strategies, allowing you to tailor the caching mechanism to your application’s requirements.


Summary

Cache management is a vital feature in the DomainFlow Container that ensures resolved services are efficiently reused. By leveraging both internal caching and optional external caching through a unified trait, the container optimizes performance and resource utilization while offering flexible caching strategies to suit your application's needs.