PHP code example of mfonte / redisw

1. Go to this page and download the library: Download mfonte/redisw 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/ */

    

mfonte / redisw example snippets




use Mfonte\Redisw\Exception\RedisException;
use Mfonte\Redisw\Exception\RedisKeyNotFoundException;
use Mfonte\Redisw\RedisClient;

$client = RedisClient::instance([
    'host' => '127.0.0.1',
    'port' => 6379,
    'serializer' => 'igbinary',
    'cache_ttl' => 300,
]);

// no connection is opened until the first command
$client->set('user:42', ['name' => 'Ada', 'admin' => true]);

try {
    $user = $client->get('user:42');
} catch (RedisKeyNotFoundException) {
    $user = null; // the key does not exist (or has expired)
} catch (RedisException $e) {
    // connection or server error
}

// don't want exceptions? wrap() returns null instead
$user = $client->wrap('get', 'user:42');

$client->set('session:abc', $data, 3600);   // expires in exactly 1 hour (no jitter)
$client->set('config', $config, 0);         // never expires
$client->mset(['a' => 1, 'b' => 2], 120);   // same TTL for every key

use Mfonte\Redisw\Cache\SimpleCache;

$cache = new SimpleCache($client); // a Psr\SimpleCache\CacheInterface

$cache->set('user.42', $user, new DateInterval('PT10M'));
$user = $cache->get('user.42', default: null);

$client = RedisClient::instance([
    'host' => 'redis.example.com',
    'port' => 6380,
    'ssl' => true,
    'ssl_context' => [
        'cafile' => '/etc/ssl/certs/redis-ca.pem',
        'verify_peer_name' => true,
    ],
    'username' => 'app',      // Redis >= 6 ACL user
    'auth' => getenv('REDIS_PASSWORD'),
]);

if ($client->has('report')) {
    return $client->get('report');
}

$report = buildExpensiveReport();
$client->set('report', $report);

return $report;