242 lines
5.8 KiB
PHP
242 lines
5.8 KiB
PHP
<?php
|
|
/**
|
|
* The cache library of zentaopms.
|
|
*
|
|
* @copyright Copyright 2009-2024 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
|
|
* @license ZPL(https://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
|
|
* @author dingguodong <dingguodong@easycorp.ltd>
|
|
* @package cache
|
|
* @link https://www.zentao.net
|
|
*/
|
|
|
|
namespace ZenTao\Cache\Driver;
|
|
|
|
use ZenTao\Cache\SimpleCache\CacheInterface;
|
|
use ZenTao\Cache\SimpleCache\InvalidArgumentException;
|
|
|
|
class RedisDriver implements CacheInterface
|
|
{
|
|
/**
|
|
* 缓存命名空间,用来区分不同的缓存。
|
|
* The cache namespace, used to distinguish different caches.
|
|
*
|
|
* @var string
|
|
*/
|
|
private $namespace;
|
|
|
|
/**
|
|
* 缓存过期时间,单位为秒。
|
|
* The cache expiration time, in seconds.
|
|
*
|
|
* @var int
|
|
*/
|
|
private $defaultLifetime;
|
|
|
|
/**
|
|
* 缓存序列化器,默认使用 PHP 内置的序列化器。
|
|
* The cache serializer, the default is the PHP built-in serializer.
|
|
*
|
|
* @var int
|
|
*/
|
|
private $serializer = \Redis::SERIALIZER_PHP;
|
|
|
|
public function __construct($namespace = '', $defaultLifetime = 0)
|
|
{
|
|
$this->namespace = $namespace;
|
|
$this->defaultLifetime = $defaultLifetime;
|
|
|
|
$this->setSerializer();
|
|
|
|
$this->connectRedis();
|
|
}
|
|
|
|
/**
|
|
* 设置序列化器。
|
|
* Set the serializer.
|
|
*
|
|
* @access private
|
|
* @return void
|
|
*/
|
|
private function setSerializer()
|
|
{
|
|
if(function_exists('igbinary_serialize'))
|
|
{
|
|
$this->serializer = \Redis::SERIALIZER_IGBINARY;
|
|
}
|
|
elseif(function_exists('msgpack_pack'))
|
|
{
|
|
$this->serializer = \Redis::SERIALIZER_MSGPACK;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 连接 Redis 服务器。
|
|
* Connect to the Redis server.
|
|
*
|
|
* @access private
|
|
* @return object
|
|
*/
|
|
private function connectRedis()
|
|
{
|
|
global $config;
|
|
|
|
try
|
|
{
|
|
$this->redis = \helper::connectRedis($config->redis);
|
|
$this->redis->setOption(\Redis::OPT_SERIALIZER, $this->serializer);
|
|
}
|
|
catch(Exception $e)
|
|
{
|
|
\helper::end($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the value related to the specified key.
|
|
*
|
|
* @link https://github.com/phpredis/phpredis?tab=readme-ov-file#get
|
|
* @param mixed $key
|
|
* @param mixed $default
|
|
* @return mixed
|
|
*/
|
|
public function get($key, $default = null)
|
|
{
|
|
$value = $this->redis->get($key);
|
|
|
|
return $value ? $value : $default;
|
|
}
|
|
|
|
/**
|
|
* Set the string value in argument as value of the key.
|
|
*
|
|
* @param mixed $key
|
|
* @param mixed $value
|
|
* @param mixed $ttl
|
|
* @return void
|
|
*/
|
|
public function set($key, $value, $ttl = null)
|
|
{
|
|
$ttl = (int)($ttl ?: $this->defaultLifetime);
|
|
|
|
return $this->redis->set($key, $value, $ttl ?: null);
|
|
}
|
|
|
|
/**
|
|
* Remove specified keys.
|
|
*
|
|
* @link https://github.com/phpredis/phpredis?tab=readme-ov-file#del-delete-unlink
|
|
* @param mixed $key
|
|
* @return int
|
|
*/
|
|
public function delete($key)
|
|
{
|
|
return $this->redis->del($key);
|
|
}
|
|
|
|
/**
|
|
* Remove all keys from the current database.
|
|
*
|
|
* @link https://github.com/phpredis/phpredis?tab=readme-ov-file#flushdb
|
|
* @return bool
|
|
*/
|
|
public function clear()
|
|
{
|
|
/* With Redis::SCAN_RETRY enabled */
|
|
$this->redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_RETRY);
|
|
|
|
$it = null;
|
|
$keys = [];
|
|
|
|
while($cachedKeys = $this->redis->scan($it, $this->namespace . ':*'))
|
|
{
|
|
$keys = array_merge($keys, $cachedKeys);
|
|
}
|
|
|
|
return $this->deleteMultiple($keys);
|
|
}
|
|
|
|
/**
|
|
* Get the values of all the specified keys.
|
|
*
|
|
* @param array $keys
|
|
* @param mixed $default
|
|
* @return array
|
|
*/
|
|
public function getMultiple($keys, $default = null)
|
|
{
|
|
return $this->redis->mget($keys);
|
|
}
|
|
|
|
/**
|
|
* Sets multiple key-value pairs in one atomic command.
|
|
*
|
|
* @link https://github.com/phpredis/phpredis?tab=readme-ov-file#mset-msetnx
|
|
* @param mixed $values
|
|
* @param mixed $ttl
|
|
* @return bool
|
|
*/
|
|
public function setMultiple($values, $ttl = null)
|
|
{
|
|
$ttl = (int)($ttl ?: $this->defaultLifetime);
|
|
|
|
if(!$ttl) return $this->redis->mset($values);
|
|
|
|
foreach($values as $key => $value) $this->redis->set($key, $value, $ttl);
|
|
}
|
|
|
|
/**
|
|
* Delete the multiple keys.
|
|
*
|
|
* @param array $keys
|
|
* @return bool
|
|
*/
|
|
public function deleteMultiple($keys)
|
|
{
|
|
$result = $this->redis->del($keys);
|
|
|
|
return $result === count($keys);
|
|
}
|
|
|
|
/**
|
|
* Verify if the specified key exists.
|
|
*
|
|
* @link https://github.com/phpredis/phpredis?tab=readme-ov-file#exists
|
|
* @param mixed $key
|
|
* @return bool
|
|
*/
|
|
public function has($key)
|
|
{
|
|
return (bool) $this->redis->exists($key);
|
|
}
|
|
|
|
/**
|
|
* 关闭 Redis 连接。
|
|
* Close the Redis connection.
|
|
*
|
|
* @access public
|
|
* @return bool
|
|
*/
|
|
public function close()
|
|
{
|
|
return $this->redis->close();
|
|
}
|
|
|
|
/**
|
|
* 获取内存使用情况。
|
|
* Get memory usage.
|
|
*
|
|
* @param string $type
|
|
* @return string
|
|
*/
|
|
public function memory($type)
|
|
{
|
|
$info = $this->redis->info();
|
|
|
|
if($type == 'total') return $info['total_system_memory_human'];
|
|
if($type == 'free') return \helper::formatKB($info['total_system_memory'] - $info['used_memory']);
|
|
if($type == 'used') return $info['used_memory_human'];
|
|
if($type == 'rate') return round(($info['used_memory'] / $info['total_system_memory']) * 100, 2);
|
|
return '';
|
|
}
|
|
}
|