Neuron-PHP

Redis
in package
implements IRepository

Redis-backed repository implementation for persistent data storage.

The Redis repository provides persistent, distributed storage with automatic serialization and TTL support.

Requires the Redis PHP extension to be installed and a Redis server running.

Tags
example
// Connect using URL (recommended)
$repo = new Redis([
    'url' => 'redis://username:password@localhost:6379/0',
    'prefix' => 'myapp:'
]);

// Connect using traditional config
$repo = new Redis([
    'host' => '127.0.0.1',
    'port' => 6379,
    'auth' => ['username', 'password'], // ACL auth (Redis 6.0+)
    'database' => 0,
    'prefix' => 'myapp:'
]);

// Connect with password-only auth (Redis < 6.0)
$repo = new Redis([
    'host' => '127.0.0.1',
    'auth' => 'mypassword',
    'prefix' => 'myapp:'
]);

// Store a DTO
$repo->save('user:123', $userDto, 3600);

// Retrieve it
$dto = $repo->find('user:123');

Table of Contents

Interfaces

IRepository
Repository interface for storing and retrieving serializable data.

Properties

$config  : array<string|int, mixed>
$prefix  : string
$redis  : Redis|null

Methods

__construct()  : mixed
Redis repository constructor.
__destruct()  : mixed
Destructor - ensure connection is closed.
delete()  : bool
Delete a value by key.
disconnect()  : void
Disconnect from Redis.
exists()  : bool
Check if a key exists in Redis.
find()  : mixed
Find a value by key.
getRedis()  : Redis|null
Get the underlying Redis instance.
isConnected()  : bool
Check if Redis connection is active.
save()  : bool
Save a value to Redis.
connect()  : void
Connect to Redis server.
parseRedisUrl()  : array<string|int, mixed>
Parse a Redis URL into connection configuration.

Properties

$config

private array<string|int, mixed> $config

$prefix

private string $prefix

$redis

private Redis|null $redis = null

Methods

__construct()

Redis repository constructor.

public __construct([array<string|int, mixed> $config = [] ]) : mixed
Parameters
$config : array<string|int, mixed> = []

Redis configuration with keys:

  • url: Redis URL (e.g., redis://user:pass@host:port/db) (optional)
  • host: Redis server hostname (default: 127.0.0.1)
  • port: Redis server port (default: 6379)
  • database: Redis database index (default: 0)
  • prefix: Key prefix for all entries (default: '')
  • timeout: Connection timeout in seconds (default: 2.0)
  • auth: Authentication - string for password-only (Redis < 6.0) or array ['username', 'password'] for ACL (Redis 6.0+) (optional)
  • persistent: Use persistent connections (default: false)
  • ssl: Use SSL/TLS connection (default: false, auto-detected from rediss:// URL)
Tags
throws
RuntimeException

If Redis extension is not loaded

throws
RuntimeException

If connection fails

throws
InvalidArgumentException

If URL format is invalid

__destruct()

Destructor - ensure connection is closed.

public __destruct() : mixed

delete()

Delete a value by key.

public delete(string $key) : bool
Parameters
$key : string

The key to delete

Return values
bool

True if deleted, false if key didn't exist

disconnect()

Disconnect from Redis.

public disconnect() : void

exists()

Check if a key exists in Redis.

public exists(string $key) : bool
Parameters
$key : string

The key to check

Return values
bool

True if exists and not expired, false otherwise

find()

Find a value by key.

public find(string $key) : mixed
Parameters
$key : string

The key to look up

Return values
mixed

The stored value, or null if not found or expired

getRedis()

Get the underlying Redis instance.

public getRedis() : Redis|null
Return values
Redis|null

isConnected()

Check if Redis connection is active.

public isConnected() : bool
Return values
bool

save()

Save a value to Redis.

public save(string $key, mixed $value[, int $ttl = 0 ]) : bool
Parameters
$key : string

The unique key to store the value under

$value : mixed

The value to store (will be serialized)

$ttl : int = 0

Time-to-live in seconds (0 = no expiration)

Return values
bool

True on success, false on failure

connect()

Connect to Redis server.

private connect() : void
Tags
throws
RuntimeException

If connection fails

parseRedisUrl()

Parse a Redis URL into connection configuration.

private parseRedisUrl(string $url) : array<string|int, mixed>

Supports both redis:// and rediss:// (SSL/TLS) schemes.

Parameters
$url : string

Redis URL in format: redis://[username:password@]host[:port][/database]

Tags
throws
InvalidArgumentException

If URL format is invalid

Return values
array<string|int, mixed>

Parsed configuration array


        
On this page

Search results