yf_cache_driver_redis class| File | plugins/cache/classes/cache/yf_cache_driver_redis.class.php |
|---|---|
| Use | _class('cache_driver_redis', 'classes/cache/') |
| Extends | yf_cache_driver |
A site sets them in its config with $PROJECT_CONF['cache_driver_redis']['NAME'] = ...;
| Property | Default | Description |
|---|---|---|
$DEFAULT_TTLint |
3600 |
|
$_confobject |
null |
internal @conf_skip |
$_connection |
null |
from yf_cache_driver: $_parent, $implemented
public function connect($options = [])
public function connect($options = [])
{
if ( ! $this->_connection) {
if ( ! $options) {
$options = [
'REDIS_HOST' => $this->_get_conf('REDIS_CACHE_HOST'),
'REDIS_PORT' => $this->_get_conf('REDIS_CACHE_PORT'),
'REDIS_PREFIX' => $this->_get_conf('REDIS_CACHE_PREFIX'),
];
}
$this->_connection = redis()->factory($options);
$this->_connection->connect();
}
if ( ! $this->_connection->is_connection()) {
$this->reconnect();
}
return $this->_connection;
}
public function del($name)
public function del($name)
{
if ( ! $this->is_ready()) {
return null;
}
return $this->_connection->del($name) > 0 ? true : null;
}
public function flush()
public function flush()
{
if ( ! $this->is_ready()) {
return null;
}
return $this->_connection->flushDB() ?: null;
}
public function get($name, $ttl = 0, $params = [])
public function get($name, $ttl = 0, $params = [])
{
if ( ! $this->is_ready()) {
return null;
}
$res = $this->_connection->get($name);
return $res ? json_decode($res, true) : null;
}
public function is_ready()
public function is_ready()
{
! $this->_connection && $this->connect();
return (bool) $this->_connection;
}
public function reconnect()
public function reconnect()
{
$this->_connection && $this->_connection->reconnect();
}
public function set($name, $data, $ttl = 0)
public function set($name, $data, $ttl = 0)
{
if ( ! $this->is_ready()) {
return null;
}
$data = json_encode($data, JSON_PRETTY_PRINT);
return $this->_connection->setex($name, $ttl ?: $this->DEFAULT_TTL, $data) ?: null;
}
public function stats()
public function stats()
{
if ( ! $this->is_ready()) {
return null;
}
$info = $this->_connection->info();
return [
'hits' => false,
'misses' => false,
'uptime' => $info['uptime_in_seconds'],
'mem_usage' => $info['used_memory'],
'mem_avail' => false,
];
}
_ hooks and helpers; not routable as page actionspublic function __call($name, $args)
Catch missing method call.
public function __call($name, $args)
{
if ( ! $this->is_ready()) {
return null;
}
// Support for driver-specific methods
if (is_object($this->_connection) && method_exists($this->_connection, $name)) {
return call_user_func_array([$this->_connection, $name], $args);
}
return main()->extend_call($this, $name, $args);
}
public function _get_conf($name, $default = null, array $params = [])
$params array public function _get_conf($name, $default = null, array $params = [])
{
if (isset($params[$name]) && $val = $params[$name]) {
return $val;
}
if ($val = getenv($name)) {
return $val;
}
if ($val = conf($name)) {
return $val;
}
if (defined($name) && ($val = constant($name)) != $name) {
return $val;
}
return $default;
}