Lightweight PHP Redis client supporting cache operations, automatic connection management, and complete Redis functionality.
Built on native Redis extension, providing stable and reliable cache operation experience.
Intelligent connection pool management, automatically establishes and maintains Redis connections, supports persistent connections for improved performance
Flexible database selection mechanism, supports Redis multi-database operations to meet different business scenario requirements
Built-in retry mechanism and error handling, ensuring reliability in unstable network environments
- Environment Variable Configuration: Flexible environment variable settings, supports multi-environment deployment
- Persistent Connections: Uses persistent connections to improve performance and reduce connection overhead
- Automatic Retry: Built-in retry mechanism handles network fluctuations and temporary connection failures
- Complete Redis Operations: Supports Redis data types including strings, hashes, lists, sets, etc.
- Multi-Database Support: Supports Redis multi-database operations, flexible management of different business data
- Security Authentication: Supports password authentication to ensure connection security
- Stateless Design: Automatic connection management and cleanup
composer require pardnchiu/redis-cli
REDIS_HOST=localhost # Redis host address
REDIS_PORT=6379 # Redis port
REDIS_PASSWORD=your_pass # Redis password (optional)
<?php
use pardnchiu\RDB;
// Initialize client
$redis = new RDB();
// Basic string operations
$redis->set(0, "user:123", "John Doe", 3600); // Set value with expiration
$user = $redis->get(0, "user:123"); // Get value
// Check connection status
if ($redis->isConnected()) {
echo "Redis connection is normal";
}
// Counter operations
$redis->incr(0, "page_views");
$redis->decr(0, "stock_count");
-
get($db, $key)
- Get string value$value = $redis->get(0, "user:123");
-
set($db, $key, $content, $expire = null)
- Set string value$redis->set(0, "session:abc", $data, 1800); // 30 minutes expiration $redis->set(1, "config:app", $config); // Never expires
-
exists($db, $key)
- Check if key existsif ($redis->exists(0, "user:123")) { echo "User exists"; }
-
delete($db, $key)
- Delete key$redis->delete(0, "temp:data");
-
ttl($db, $key)
- Get expiration time$seconds = $redis->ttl(0, "session:abc");
-
keys($db, $pattern)
- Search key names$userKeys = $redis->keys(0, "user:*");
// Set hash field
$redis->hset(0, "user:123", "name", "John Doe", 3600);
$redis->hset(0, "user:123", "email", "john@example.com");
// Get hash field
$name = $redis->hget(0, "user:123", "name");
// Get all hash data
$userData = $redis->hgetall(0, "user:123");
// Push to list (left/right side)
$redis->lpush(0, "tasks", "New Task", 3600);
$redis->rpush(0, "logs", "Log Message");
// Pop list elements
$task = $redis->lpop(0, "tasks");
$log = $redis->rpop(0, "logs");
// Get list length
$length = $redis->llen(0, "tasks");
// Add set members
$redis->sadd(0, "tags", "php", 3600);
$redis->sadd(0, "tags", "redis");
// Remove set member
$redis->srem(0, "tags", "old_tag");
// Get all members
$tags = $redis->smembers(0, "tags");
// Check member existence
if ($redis->sismember(0, "tags", "php")) {
echo "Contains PHP tag";
}
// Set operations
$common = $redis->sinter(0, ["tags:user1", "tags:user2"]); // Intersection
$all = $redis->sunion(0, ["tags:user1", "tags:user2"]); // Union
$diff = $redis->sdiff(0, ["tags:user1", "tags:user2"]); // Difference
// Batch operations
$values = $redis->mget(0, ["key1", "key2", "key3"]);
$redis->mset(0, ["key1" => "value1", "key2" => "value2"]);
// Numeric operations
$redis->incr(0, "counter"); // Increment by 1
$redis->decr(0, "stock"); // Decrement by 1
$redis->append(0, "log", "New content"); // Append string
// Clear database
$redis->flushdb(0);
// Get server info
$info = $redis->info();
try {
$redis = new RDB();
// Redis operations
$result = $redis->set(0, "user:123", $userData, 3600);
if ($result) {
echo "Data saved successfully";
} else {
echo "Data save failed";
}
} catch (\Exception $e) {
// Connection error handling
error_log("Redis error: " . $e->getMessage());
if (strpos($e->getMessage(), "Connection refused") !== false) {
echo "Redis server is not running";
} elseif (strpos($e->getMessage(), "Authentication") !== false) {
echo "Redis authentication failed, please check password";
} else {
echo "Redis operation exception, please try again later";
}
}
$redis = new RDB();
// Check connection status
if (!$redis->isConnected()) {
// Handle connection failure
error_log("Redis connection failed, using fallback solution");
// Can use other cache solutions or directly query database
return $this->fallbackCache($key);
}
// Normal Redis usage
$data = $redis->get(0, $key);
// Monitor connection status
$info = $redis->info();
if ($info) {
$connectedClients = $info['connected_clients'] ?? 0;
$usedMemory = $info['used_memory_human'] ?? '0B';
error_log("Redis status - Connections: {$connectedClients}, Memory usage: {$usedMemory}");
}
This project is licensed under the MIT License.
©️ 2024 邱敬幃 Pardn Chiu