1. Go to this page and download the library: Download zenstruck/redis 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/ */
use Zenstruck\Redis;
$proxy = Redis::create('redis://localhost?prefix=app:');
$proxy = Redis::create('redis://localhost', ['prefix' => 'app:']); // equivalent to above
use Zenstruck\Redis;
// PHP: serialize/unserialize values
$proxy = Redis::create('redis://localhost?serializer=php');
$proxy = Redis::create('redis://localhost', ['serializer' => \Redis::SERIALIZER_PHP]); // equivalent to above
// JSON: json_encode/json_decode values (doesn't work for objects)
$proxy = Redis::create('redis://localhost?serializer=json');
$proxy = Redis::create('redis://localhost', ['serializer' => \Redis::SERIALIZER_JSON]); // equivalent to above
/** @var Zenstruck\Redis $proxy */
// call any \Redis|\RedisArray|\RedisCluster method
$proxy->set('mykey', 'value');
$proxy->get('mykey'); // "value"
// get the "real" client
$proxy->realClient(); // \Redis|\RedisArray|\RedisCluster
/** @var Zenstruck\Redis $proxy */
// use \Redis::multi()
$results = $proxy->transaction()
->set('x', '42')
->incr('x')
->get('x')->as('value') // alias the result of this command
->del('x')
->execute() // the results of the above transaction as an array (keyed by index of command or alias if set)
;
$results['value']; // "43" (result of ->get())
$results[3]; // true (result of ->del())
// use \Redis::pipeline() - see note below about \RedisCluster
$proxy->sequence()
->set('x', '42')
->incr('x')
->get('x')->as('value') // alias the result of this command
->del('x')
->execute() // the results of the above sequence as an array (keyed by index of command of alias if set)
;
$results['value']; // "43" (result of ->get())
$results[3]; // true (result of ->del())
/** @var Zenstruck\Redis $proxy */
$proxy->count(); // 1 if \Redis, # hosts if \RedisArray, # "masters" if \RedisCluster
foreach ($proxy as $node) {
$proxy->flushAll(); // this is permitted even for \RedisCluster (which typically
/** @var Zenstruck\Redis $client */
$set = $client->expiringSet('my-set'); // redis key to store the set
$set->add('member1', 600); // set add "member1" that expires in 10 minutes
$set->add('member1', new \DateInterval::createFromDateString('5 minutes')); // can use \DateInterval for the TTL
$set->add('member1', new \DateTime('+5 minutes')); // use \DateTimeInterface to set specific expiry timestamp
$set->remove('member1'); // explicitly remove a member
$set->all(); // array - all unexpired members
$set->contains('member'); // true/false
$set->clear(); // clear all items
$set->prune(); // explicitly "prune" the set (remove expired members)
count($set); // int - number of unexpired members
foreach ($set as $member) {
// iterate over unexpired members
}
// fluent
$set
->add('member1', 600)
->add('member2', 600)
->remove('member1')
->remove('member2')
->prune()
->clear()
;