59 lines
1.2 KiB
PHP
59 lines
1.2 KiB
PHP
<?
|
|
class CacheHandler
|
|
{
|
|
protected $redis;
|
|
|
|
public function __construct()
|
|
{
|
|
if (class_exists('Redis')) {
|
|
try {
|
|
$this->redis = \RedisConnection::getInstance()->getConnection();
|
|
} catch (\Exception $e) {
|
|
$this->redis = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function get($key)
|
|
{
|
|
if ($this->redis) {
|
|
return $this->redis->get($key);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public function set($key, $value, $ttl = 86400) // 86400 = 60 * 60 * 24 (1 dzień)
|
|
{
|
|
if ($this->redis) {
|
|
$this->redis->setex($key, $ttl, serialize($value));
|
|
}
|
|
}
|
|
|
|
public function exists($key)
|
|
{
|
|
if ($this->redis) {
|
|
return $this->redis->exists($key);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function delete($key)
|
|
{
|
|
if ($this->redis) {
|
|
return $this->redis->del($key);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function deletePattern($pattern)
|
|
{
|
|
if ($this->redis) {
|
|
$keys = $this->redis->keys($pattern);
|
|
if (!empty($keys)) {
|
|
return $this->redis->del($keys);
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|