PHP code example of flytachi / winter-redis

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

    

flytachi / winter-redis example snippets


use Flytachi\Winter\Redis\Config\RedisConfig;

class MainRedisConfig extends RedisConfig
{
    public function setUp(): void
    {
        $this->host     = getenv('REDIS_HOST') ?: 'localhost';
        $this->password = getenv('REDIS_PASS') ?: '';
    }
}

use Flytachi\Winter\Redis\Store\RedisStore;

class SessionStore extends RedisStore
{
    protected string $redisConfigClassName = MainRedisConfig::class;
    protected string $prefix               = 'session:';
}

$sessions = new SessionStore();

$sessions->set('42', $token, ttl: 3600);
$sessions->get('42');                       // $token | null
$sessions->increment('logins');             // atomic
$sessions->keys();                          // ['logins', '42'] — SCAN order, not insertion
$sessions->flush();                         // this store only, never the database

// strings, counters, keys
$store->set('key', $value, ttl: 60);   $store->ttl('key');
$store->increment('hits', 5);          $store->keys('user:*');

// hashes — fields, with their own lifetimes on Redis 8.0+
$cart = $store->hash('cart:42');
$cart->setAll(['qty' => '2', 'sku' => 'A-1']);
$cart->set('lock', '1', ttl: 30);      // HSETEX: the field expires, not the hash
$cart->increment('qty');               $cart->all();

// lists — queues, capped logs, blocking reads on their own connection
$jobs = $store->list('jobs');
$jobs->push($payload, cap: 10_000);    $jobs->consume(timeout: 5);
$jobs->moveTo($processing);            // LMOVE: never in neither, never in both

// streams — history, and delivery tracking the server keeps
$events = $store->stream('events');
$events->add(['type' => 'signup'], cap: 100_000);
$events->ensureGroup('mailers');

$group = $events->group('mailers', consumer: 'worker-1');
foreach ($group->consume(count: 10, timeout: 5) as $entry) {
    $this->handle($entry);
    $group->ack($entry);               // acknowledging is never automatic
}
foreach ($group->claimStale(idle: 60_000) as $entry) { /* a worker died holding these */ }

// anything not wrapped
$store->raw()->zAdd($store->key('leaders'), 100, 'user:1');

use Flytachi\Winter\Kernel\Process\ForkReset;
use Flytachi\Winter\Redis\RedisPool;

RedisPool::setLogger(LoggerFactory::getLogger(RedisPool::class));
ForkReset::register(static fn() => RedisPool::reset());

RedisPool::stats();
// ['Main\Configurations\MainRedisConfig' => ['total' => 4, 'idle' => 3, 'active' => 1, 'maximum' => 10]]

$config->pingDetail();   // ['status' => true, 'latency' => 0.32, 'error' => null]