PHP code example of switon / redis
1. Go to this page and download the library: Download switon/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/ */
switon / redis example snippets
use Switon\Core\Attribute\Autowired;
use Switon\Redis\ClientInterface;
class UserCache
{
#[Autowired] protected ClientInterface $redis;
public function save(int $userId, array $data): void
{
$this->redis->set("user:{$userId}", json_encode($data), 3600);
}
public function replaceSession(string $sessionId, array $data): void
{
$redis = $this->redis->getTransient();
$redis->watch("session:{$sessionId}");
$redis->multi();
$redis->set("session:{$sessionId}", json_encode($data));
$redis->set("session:{$sessionId}:updated_at", (string)time());
$redis->exec();
}
}