VOOZH about

URL: https://deepwiki.com/hypervel/support/6.2-cache-management

⇱ Cache Management | hypervel/support | DeepWiki


Loading...
Menu

Cache Management

Purpose and Scope

This document explains caching operations via the Cache facade, including basic get/put/forget patterns, advanced remember operations, cache stores and drivers, tagging, locking mechanisms, and multi-key operations. Cache Management provides a unified interface for multiple cache backends (Redis, Memcached, file-based, etc.) through the Manager pattern.

For database operations, see Database Operations. For Redis-specific operations beyond caching, see Redis Operations. For general Manager pattern architecture, see Manager Pattern.


Cache System Architecture

The cache system follows a layered architecture where the Cache facade provides a static interface to the CacheManager, which manages multiple cache stores/drivers. Each store wraps an underlying cache backend implementation.


Sources: src/Facades/Cache.php1-68


Cache Facade Methods

The Cache facade exposes a comprehensive set of methods organized by functionality:

Method Categories

CategoryMethodsDescription
Driver Managementstore(), driver(), getDefaultDriver(), setDefaultDriver(), forgetDriver()Manage cache stores and default driver
Basic Operationsget(), set(), put(), forget(), delete(), clear(), flush(), has(), missing()Fundamental cache read/write/delete operations
Remember Patternsremember(), rememberForever(), sear(), pull()Retrieve or compute and cache values
Modificationadd(), increment(), decrement(), forever()Conditional writes and atomic operations
Multiple Keysmany(), putMany(), getMultiple(), setMultiple(), deleteMultiple()Batch operations for efficiency
Taggingtags(), supportsTags()Group cache entries for bulk invalidation
Lockinglock(), restoreLock()Distributed locking mechanisms
ConfigurationgetPrefix(), getName(), getDefaultCacheTime(), setDefaultCacheTime()Runtime configuration access

Sources: src/Facades/Cache.php9-57


Store and Driver Management

The cache system uses the Manager pattern to support multiple cache stores. Each store represents a connection to a cache backend.


Driver Resolution Methods

MethodSignaturePurpose
store()store(?string $name = null): RepositoryResolve a specific named store or default
driver()driver(?string $driver = null): RepositoryAlias for store()
repository()repository(Store $store, array $config = []): RepositoryCreate repository from store instance
extend()extend(string $driver, Closure $callback): CacheManagerRegister custom driver implementation
forgetDriver()forgetDriver(array|string|null $name = null): CacheManagerRemove cached driver instance

Sources: src/Facades/Cache.php10-19


Basic Cache Operations

Read Operations

The cache provides multiple methods for retrieving values:





































MethodReturn TypeBehavior
get(string $key, mixed $default = null)mixedRetrieve value or return default (evaluates closures)
has(string $key)boolCheck if key exists
missing(string $key)boolInverse of has()
many(array $keys)arrayRetrieve multiple keys at once
getMultiple(iterable $keys, mixed $default = null)iterablePSR-16 compatible batch retrieval

Sources: src/Facades/Cache.php31-46

Write Operations

Write operations support various TTL (time-to-live) formats:

MethodParametersTTL SupportBehavior
put()(string|array $key, mixed $value, int|DateInterval|DateTimeInterface|null $ttl)YesStore value with optional TTL
set()(string $key, mixed $value, int|DateInterval|null $ttl)YesPSR-16 alias for put()
add()(string $key, mixed $value, int|DateInterval|DateTimeInterface|null $ttl)YesStore only if key doesn't exist
forever()(string $key, mixed $value)No (infinite)Store value indefinitely
putMany()(array $values, int $seconds)Yes (seconds)Store multiple key-value pairs
setMultiple()(iterable $values, int|DateInterval|null $ttl)YesPSR-16 batch write

Sources: src/Facades/Cache.php21-43

Delete Operations

MethodParametersReturnDescription
forget()(string $key)boolRemove single key
delete()(string $key)boolPSR-16 alias for forget()
deleteMultiple()(iterable $keys)boolRemove multiple keys
clear()noneboolPSR-16 remove all entries
flush()noneboolRemove all entries from store
purge()(?string $name)voidPurge specific store or all stores

Sources: src/Facades/Cache.php17-44


Remember Patterns

Remember patterns combine retrieval and computation, caching the result if it doesn't exist:


Remember Method Comparison

MethodTTLUse Case
remember(string $key, int|DateInterval|DateTimeInterface|null $ttl, Closure $callback)ConfigurableStandard caching with expiration
rememberForever(string $key, Closure $callback)InfiniteLong-lived computed values
sear(string $key, Closure $callback)InfiniteAlias for rememberForever()
pull(string $key, mixed $default = null)N/A (deletes)Retrieve once then remove

Sources: src/Facades/Cache.php20-28


Atomic Operations

Atomic operations provide thread-safe increment and decrement functionality:






















MethodReturn TypeBehavior
increment(string $key, int $value = 1)int|boolAtomically increase value by amount
decrement(string $key, int $value = 1)int|boolAtomically decrease value by amount

If the key doesn't exist, it is typically initialized to 0 before the operation. Backend support varies:

  • Redis/Memcached: Native atomic operations
  • File/Database stores: Simulated via locking

Sources: src/Facades/Cache.php23-24


Cache Tagging

Cache tagging allows grouping related cache entries for efficient bulk invalidation:


Tagging Support by Store

Store TypeTags SupportedImplementation
Redis✓ YesTag namespace keys
Memcached✓ YesTag revision tracking
File✗ NoN/A
Database✗ NoN/A
Array✓ YesIn-memory tracking

Tagged Cache Methods

MethodPurpose
tags(mixed $names): TaggedCacheCreate tagged cache instance with one or more tags
supportsTags(): boolCheck if current store supports tagging
flush()When called on tagged cache, invalidates all entries with those tags

Sources: src/Facades/Cache.php41-47


Cache Locking

Cache locks provide distributed locking for coordinating work across multiple processes or servers:


Lock Interface Methods

MethodReturn TypeDescription
lock(string $name, int $seconds = 0, ?string $owner = null)LockCreate lock instance with optional TTL and owner ID
restoreLock(string $name, string $owner)LockRestore existing lock using owner token
get(?callable $callback = null)bool|mixedAttempt to acquire lock; execute callback if successful
block(int $seconds, ?callable $callback = null)bool|mixedWait up to N seconds to acquire lock
release()boolRelease owned lock
forceRelease()voidForce release lock regardless of owner
owner()stringGet lock owner identifier

Sources: src/Facades/Cache.php39-40


Cache Configuration Integration

The cache system integrates with repository-level configuration:
































MethodReturn TypePurpose
getPrefix()stringRetrieve key prefix for namespace isolation
getName()?stringGet store name identifier
getDefaultCacheTime()?intGet default TTL in seconds
setDefaultCacheTime(?int $seconds)RepositorySet default TTL for operations without explicit TTL

Sources: src/Facades/Cache.php45-52


Event Dispatcher Integration

Cache operations can trigger events for monitoring and logging:



























MethodParametersPurpose
getEventDispatcher()NoneRetrieve current event dispatcher instance
setEventDispatcher(EventDispatcherInterface $events)DispatcherSet event dispatcher for cache events
refreshEventDispatcher()NoneRe-resolve event dispatcher from container

Sources: src/Facades/Cache.php13-51


Macro Support

The cache system supports runtime extension via macros:

MethodPurpose
macro(string $name, callable|object $macro)Register custom method
mixin(object $mixin, bool $replace = true)Register multiple methods from mixin class
hasMacro(string $name)Check if macro exists
macroCall(string $method, array $parameters)Invoke registered macro

This allows adding custom cache patterns without modifying core classes.

Sources: src/Facades/Cache.php53-56


PSR-16 SimpleCache Compliance

The cache repository implements the PSR-16 SimpleCache interface:

PSR-16 MethodCache MethodNotes
get()get()Direct mapping
set()set() / put()set() is PSR-16, put() is Laravel-style
delete()delete() / forget()delete() is PSR-16, forget() is Laravel-style
clear()clear() / flush()Both clear all cache entries
getMultiple()getMultiple()Batch retrieval
setMultiple()setMultiple()Batch storage
deleteMultiple()deleteMultiple()Batch deletion
has()has()Existence check

This enables interoperability with PSR-16 compliant libraries.

Sources: src/Facades/Cache.php31-38


Usage Patterns

Basic Caching Pattern


Remember Pattern


Tagged Cache Pattern


Lock Pattern


Atomic Counter Pattern



Store Management Patterns


Multi-Store Operations

Different cache stores can be used within the same application:


Sources: src/Facades/Cache.php10-18


Integration with Facade System

The Cache facade follows the standard facade pattern:


The facade resolves to \Hypervel\Cache\Contracts\Factory, which is bound to the CacheManager instance in the service container. This manager pattern enables runtime driver switching and custom driver registration.

Sources: src/Facades/Cache.php63-66


Summary

The Cache Management system provides:

  • Unified Interface: Single facade for multiple cache backends
  • Multiple Stores: Redis, Memcached, File, Database, Array support
  • Rich Operations: Get/put/forget, remember patterns, atomic operations
  • Advanced Features: Tagging, distributed locking, batch operations
  • Standard Compliance: PSR-16 SimpleCache interface
  • Extensibility: Manager pattern for custom drivers
  • Event Integration: Cache operation events for monitoring
  • Testing Support: Swappable and mockable for tests

For testing cache operations, see Testing Overview. For the underlying manager architecture, see Manager Pattern.