20241230#1

This commit is contained in:
User
2024-12-30 02:37:57 +03:00
parent 8f96569b68
commit b15e126831
2 changed files with 72 additions and 0 deletions

56
src/RedisStorage.php Normal file
View File

@@ -0,0 +1,56 @@
<?php
namespace Rmphp\Redis;
use Redis;
use RedisException;
class RedisStorage implements RedisStorageInterface {
private Redis $redis;
private int $expire = 300;
/**
* @throws RedisException
*/
public function __construct(array $params) {
if(isset($params['database'])){
if(is_int($params['database'])) $database = $params['database'];
unset($params['database']);
if(is_int($params['defaultExpire'])) $this->expire = $params['defaultExpire'];
unset($params['defaultExpire']);
}
$this->redis = new Redis($params);
if(!empty($database)) $this->redis->select($database);
}
/**
* @throws RedisException
*/
public function set(string $key, mixed $value, mixed $option = null): void {
$this->redis->set($key, $value, $option);
}
/**
* @throws RedisException
*/
public function setEx(string $key, mixed $value): void {
$this->redis->set($key, $value, $this->expire);
}
/**
* @throws RedisException
*/
public function get(string $key) : mixed {
return $this->redis->get($key);
}
/**
* @throws RedisException
*/
public function exists(mixed $key) : bool|int|Redis {
return $this->redis->exists($key);
}
}

View File

@@ -0,0 +1,16 @@
<?php
namespace Rmphp\Redis;
use Redis;
interface RedisStorageInterface {
public function set(string $key, mixed $value, mixed $option = null): void;
public function setEx(string $key, mixed $value): void;
public function get(string $key): mixed;
public function exists(mixed $key): bool | int | Redis;
}