mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-09-02 22:17:03 +03:00
By using a connection URL, it is possible to support all Predis features, like passwords, unix sockets, database selection, etc. Tested with TCP and unix socket connections. Closes #113
106 lines
1.9 KiB
PHP
106 lines
1.9 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of MailSo.
|
|
*
|
|
* (c) 2014 Usenko Timur
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace MailSo\Cache\Drivers;
|
|
|
|
/**
|
|
* @category MailSo
|
|
* @package Cache
|
|
* @subpackage Drivers
|
|
*/
|
|
class Redis implements \MailSo\Cache\DriverInterface
|
|
{
|
|
/**
|
|
* @var string
|
|
*/
|
|
private $sUrl;
|
|
|
|
/**
|
|
* @var int
|
|
*/
|
|
private $iExpire;
|
|
|
|
/**
|
|
* @var \Memcache|null
|
|
*/
|
|
private $oRedis;
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
private $sKeyPrefix;
|
|
|
|
function __construct(string $sUrl = 'redis://127.0.0.1:6379', int $iExpire = 43200, string $sKeyPrefix = '')
|
|
{
|
|
$this->sUrl = $sUrl;
|
|
$this->iExpire = 0 < $iExpire ? $iExpire : 43200;
|
|
|
|
$this->oRedis = null;
|
|
|
|
try
|
|
{
|
|
$this->oRedis = new \Predis\Client($sUrl);
|
|
|
|
$this->oRedis->connect();
|
|
|
|
if (!$this->oRedis->isConnected())
|
|
{
|
|
$this->oRedis = null;
|
|
}
|
|
}
|
|
catch (\Throwable $oExc)
|
|
{
|
|
$this->oRedis = null;
|
|
unset($oExc);
|
|
}
|
|
|
|
$this->sKeyPrefix = $sKeyPrefix;
|
|
if (!empty($this->sKeyPrefix))
|
|
{
|
|
$this->sKeyPrefix =
|
|
\preg_replace('/[^a-zA-Z0-9_]/', '_', rtrim(trim($this->sKeyPrefix), '\\/')).'/';
|
|
}
|
|
}
|
|
|
|
public function Set(string $sKey, string $sValue) : bool
|
|
{
|
|
return $this->oRedis ? $this->oRedis->setex($this->generateCachedKey($sKey), $this->iExpire, $sValue) : false;
|
|
}
|
|
|
|
public function Get(string $sKey) : string
|
|
{
|
|
$sValue = $this->oRedis ? $this->oRedis->get($this->generateCachedKey($sKey)) : '';
|
|
return \is_string($sValue) ? $sValue : '';
|
|
}
|
|
|
|
public function Delete(string $sKey) : void
|
|
{
|
|
if ($this->oRedis)
|
|
{
|
|
$this->oRedis->del($this->generateCachedKey($sKey));
|
|
}
|
|
}
|
|
|
|
public function GC(int $iTimeToClearInHours = 24) : bool
|
|
{
|
|
if (0 === $iTimeToClearInHours && $this->oRedis)
|
|
{
|
|
return $this->oRedis->flushdb();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private function generateCachedKey(string $sKey) : string
|
|
{
|
|
return $this->sKeyPrefix.\sha1($sKey);
|
|
}
|
|
}
|