VOOZH about

URL: https://deepwiki.com/hypervel/cache/3.4-redis-store

⇱ Redis Store | hypervel/cache | DeepWiki


Loading...
Menu

Redis Store

The RedisStore class provides a Redis-backed cache store implementation with support for distributed caching, cache tagging, and distributed locking. This store is designed for production environments where high performance, persistence, and multi-server cache coordination are required.

For information about other cache stores, see page 3. For cache tagging implementation details, see page 4.1. For distributed locking features, see page 4.2.

Class Structure

The RedisStore class extends TaggableStore and implements the LockProvider interface, providing comprehensive Redis-backed caching with tagging and locking capabilities.

Class Hierarchy and Dependencies Diagram


Sources: src/RedisStore.php1-265

Store Interface Implementation

The RedisStore implements all methods from the Store contract. The following table lists all public methods and their Redis operations:

MethodParametersReturnRedis CommandsPurpose
get()string $keymixedGETRetrieve single item
many()array $keysarrayMGETRetrieve multiple items
put()string $key, mixed $value, int $secondsboolSETEXStore item with TTL
putMany()array $values, int $secondsboolMULTI/EXECStore multiple items atomically
add()string $key, mixed $value, int $secondsboolLua scriptStore if not exists
increment()string $key, int $value = 1intINCRBYIncrement numeric value
decrement()string $key, int $value = 1intDECRBYDecrement numeric value
forever()string $key, mixed $valueboolSETStore without expiration
forget()string $keyboolDELDelete item
flush()-boolFLUSHDBClear all items

Sources: src/RedisStore.php42-175

RedisFactory Integration

The RedisStore uses Hyperf's RedisFactory for connection management and pooling. The factory provides RedisProxy instances that handle connection lifecycle and pooling automatically.

RedisFactory Connection Flow


Sources: src/RedisStore.php33-38 src/RedisStore.php191-202

Connection Configuration

The store supports separate connections for data and lock operations to isolate workloads:

PropertyTypePurposeDefault
$factoryRedisFactoryConnection factory instanceInjected via constructor
$connectionstringData operation connection name'default'
$lockConnectionstringLock operation connection nameFalls back to $connection
$prefixstringKey prefix for namespace isolation''

Connection Methods


Sources: src/RedisStore.php191-220

Cache Operation Details

Read Operations

Single Item Retrieval


Sources: src/RedisStore.php43-48

Bulk Retrieval

The many() method uses Redis MGET for efficient multi-key retrieval:


Sources: src/RedisStore.php54-67

Write Operations

Standard Write

The put() method uses SETEX for atomic write-with-TTL:


Sources: src/RedisStore.php72-79

Bulk Write with Transaction

The putMany() method wraps multiple put() calls in a Redis transaction:


Sources: src/RedisStore.php84-99

Permanent Storage

The forever() method stores without expiration:


Sources: src/RedisStore.php138-141

Atomic Operations and Lua Scripts

Numeric Operations

The store provides atomic increment/decrement using Redis native commands:


These operations are atomic at the Redis server level, ensuring thread-safe counter updates across multiple processes.

Sources: src/RedisStore.php122-133

Conditional Write with Lua

The add() method uses a Lua script for atomic "set-if-not-exists" with TTL:

Lua Script Execution Flow


Implementation


Sources: src/RedisStore.php104-117

Serialization Strategy

The RedisStore implements intelligent serialization to optimize storage for different data types:

Serialization Decision Tree


Implementation



























Data TypeStorage MethodReason
Integer, Float (normal)Raw numeric stringRedis native support, no overhead
INF, -INF, NaNPHP serialize()Special float values need preservation
String, Array, ObjectPHP serialize()Complex types require serialization

Sources: src/RedisStore.php249-264

Cache Tagging Support

The RedisStore extends TaggableStore to provide cache tagging capabilities through the RedisTaggedCache and RedisTagSet classes.

Tagged Cache Creation Flow


Implementation


The method accepts either an array of tag names or variadic arguments, creating a RedisTaggedCache instance that wraps the store and manages tag associations through RedisTagSet.

For detailed information about the tagging system implementation, see page 4.1.

Sources: src/RedisStore.php178-186

Distributed Locking

The RedisStore implements the LockProvider interface to create distributed locks via RedisLock instances. These locks coordinate access across multiple processes and servers.

Lock Creation and Connection Routing


Lock Methods

MethodParametersReturnPurpose
lock()string $name, int $seconds, ?string $ownerRedisLockCreate new lock instance
restoreLock()string $name, string $ownerRedisLockRestore lock with specific owner

Implementation


Note that locks use the dedicated lockConnection() (or fall back to the main connection) and automatically apply the key prefix. For detailed lock implementation and usage patterns, see page 4.2.

Sources: src/RedisStore.php146-157

Rate Limiting Backend

While RedisStore does not directly implement rate limiting logic, it serves as the storage backend for rate limiters that use atomic increment operations and TTL-based key expiration.

Rate limiting systems built on RedisStore typically use:

OperationStore MethodPurpose
Counter initializationadd()Create counter key if not exists (atomic)
Attempt incrementincrement()Atomic counter increment
Attempt checkget()Retrieve current attempt count
Counter expirationTTL in put()/add()Automatic cleanup after decay period

Typical Rate Limit Flow


The Limit class defines rate limit configurations (maxAttempts, decay time) but does not directly interact with the store. For rate limiting implementation details, see page 4.3.

Sources: src/RedisStore.php104-133 src/RateLimiting/Limit.php1-92

Key Management and Prefixing

The Redis store implements a flexible key prefixing system to avoid collisions and organize cache data:


Prefix management methods:

MethodPurposeBehavior
setPrefix(string $prefix)Set key prefixAdds ':' if non-empty
getPrefix()Get current prefixReturns prefix with ':'

All Redis operations automatically apply the prefix through the $this->prefix . $key pattern used throughout the class.

Sources: src/RedisStore.php222-244