yf_cache_driver_redis class

Fileplugins/cache/classes/cache/yf_cache_driver_redis.class.php
Use_class('cache_driver_redis', 'classes/cache/')
Extendsyf_cache_driver

Public properties

A site sets them in its config with $PROJECT_CONF['cache_driver_redis']['NAME'] = ...;

PropertyDefaultDescription
$DEFAULT_TTL
int
3600
$_conf
object
null internal @conf_skip
$_connection null

Inherited properties

from yf_cache_driver: $_parent, $implemented

Public methods 10

connect

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;
    }

del

public function del($name)
    public function del($name)
    {
        if ( ! $this->is_ready()) {
            return null;
        }
        return $this->_connection->del($name) > 0 ? true : null;
    }

flush

public function flush()
    public function flush()
    {
        if ( ! $this->is_ready()) {
            return null;
        }
        return $this->_connection->flushDB() ?: null;
    }

get

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;
    }

is_ready

public function is_ready()
    public function is_ready()
    {
        ! $this->_connection && $this->connect();
        return (bool) $this->_connection;
    }

reconnect

public function reconnect()
    public function reconnect()
    {
        $this->_connection && $this->_connection->reconnect();
    }

set

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;
    }

stats

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,
        ];
    }

Methods starting with _ hooks and helpers; not routable as page actions

__call

public 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);
    }

_get_conf

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;
    }