PHP code example of pardnchiu / redis-cli

1. Go to this page and download the library: Download pardnchiu/redis-cli library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pardnchiu / redis-cli example snippets




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

  $value = $redis->get(0, "user:123");
  

  $redis->set(0, "session:abc", $data, 1800);  // 30 minutes expiration
  $redis->set(1, "config:app", $config);        // Never expires
  

  if ($redis->exists(0, "user:123")) {
      echo "User exists";
  }
  

  $redis->delete(0, "temp:data");
  

  $seconds = $redis->ttl(0, "session:abc");
  

  $userKeys = $redis->keys(0, "user:*");
  

// Set hash field
$redis->hset(0, "user:123", "name", "John Doe", 3600);
$redis->hset(0, "user:123", "email", "[email protected]");

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