Skip to content

Latest commit

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Redis CLI

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.

packagistversionlicense
readmereadme

Three Core Features

Automatic Connection Management

Intelligent connection pool management, automatically establishes and maintains Redis connections, supports persistent connections for improved performance

Multi-Database Support

Flexible database selection mechanism, supports Redis multi-database operations to meet different business scenario requirements

Stable Connection

Built-in retry mechanism and error handling, ensuring reliability in unstable network environments

Features

  • 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

Usage

Installation

composer require pardnchiu/redis-cli

Environment Configuration

REDIS_HOST=localhost# Redis host addressREDIS_PORT=6379# Redis portREDIS_PASSWORD=your_pass# Redis password (optional)

Basic Usage

<?phpusepardnchiu\RDB;
// Initialize client$redis = newRDB();
// 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 statusif ($redis->isConnected()) {
echo"Redis connection is normal";
}
// Counter operations$redis->incr(0, "page_views");
$redis->decr(0, "stock_count");

API Reference

Basic Operations

  • 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 exists

    if ($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:*");

Hash Operations

// 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");

List Operations

// 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");

Set Operations

// 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 existenceif ($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

Management Operations

// 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();

Error Handling

try {
$redis = newRDB();
// 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 handlingerror_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";
}
}

Connection Status Check

$redis = newRDB();
// Check connection statusif (!$redis->isConnected()) {
// Handle connection failureerror_log("Redis connection failed, using fallback solution");
// Can use other cache solutions or directly query databasereturn$this->fallbackCache($key);
}
// Normal Redis usage$data = $redis->get(0, $key);

Performance Monitoring

// 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}");
}

License

This project is licensed under the MIT License.

Author

邱敬幃 Pardn Chiu


©️ 2024 邱敬幃 Pardn Chiu

About

(PHP Libaray) Redis client wrapper

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages