yf_wrapper_memcached class

Memcached API wrapper.

Fileplugins/memcached/classes/yf_wrapper_memcached.class.php
Use_class('wrapper_memcached')

Public properties

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

PropertyDefaultDescription
$DEFAULT
array
['port' => 11211, 'host' => '127.0.0.1', 'persistent' => false] internal @conf_skip
$FORCE_EXT
mixed
'' Kept for BC: only ext/memcached is supported (ext/memcache has no PHP 8 build), any value is ignored
$_connected_ok
boo;
false internal @conf_skip
$_memcache_new_extension
mixed
null @conf_skip

Public methods 16

connect

public function connect($params = [])
    public function connect($params = [])
    {
        $this->_connected_ok = false;

        if (class_exists('Memcached')) {
            $this->_connection = new Memcached();
        }
        if (is_object($this->_connection)) {
            $mc_params = [$this->DEFAULT];
            if (isset($params['memcache']) && ! empty($params['memcache'])) {
                $mc_params = is_array($params['memcache']) ? $params['memcache'] : [$params['memcache']];
            }
            $failed = true;
            foreach ((array) $mc_params as $server) {
                if ( ! is_array($server) || ! isset($server['host'])) {
                    continue;
                }
                $server['port'] = isset($server['port']) ? (int) $server['port'] : 11211;
                $server['persistent'] = isset($server['persistent']) ? (bool) $server['persistent'] : true;
                if ($this->_connection->addServer($server['host'], $server['port'], $server['persistent'])) {
                    $failed = false;
                    break;
                }
            }
            if ( ! $failed) {
                $this->_connected_ok = true;
            }
        }
        if (is_object($this->_connection)) {
            $this->_memcache_new_extension = method_exists($this->_connection, 'getMulti');
        }
        return $this->_connection;
    }

del

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

flush

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

get

public function get($name, $ttl = 0, $params = [])
    public function get($name, $ttl = 0, $params = [])
    {
        if ( ! $this->is_ready()) {
            return null;
        }
        if ($name === false || $name === null) {
            return null;
        }
        $result = $this->_connection->get($name);
        if ($result === false) {
            return null;
        }
        if (is_string($result)) {
            $try_unpack = unserialize($result);
            if ($try_unpack || substr($result, 0, 2) == 'a:') {
                $result = $try_unpack;
            }
            if ($result === 'false') {
                $result = false;
            }
        }
        return $result;
    }

is_ready

public function is_ready()
    public function is_ready()
    {
        return isset($this->_connection) && $this->_connected_ok;
    }

list_keys

public function list_keys()
    public function list_keys()
    {
        if ( ! $this->is_ready()) {
            return null;
        }
        if ( ! method_exists($this->_connection, 'getAllKeys')) {
            return null;
        }
        return $this->_connection->getAllKeys() ?: null;
    }

multi_del

public function multi_del(array $names)
  • $names array
    public function multi_del(array $names)
    {
        if ( ! $this->is_ready()) {
            return null;
        }
        // PHPWTF!! deleteMulti in Memcached extension exists only starting from version 2.0 in PECL
        if ( ! method_exists($this->_connection, 'deleteMulti')) {
            $failed = false;
            foreach ((array) $names as $name) {
                if ( ! $this->del($name)) {
                    $failed = true;
                }
            }
            return $failed ? null : true;
        }
        return $this->_connection->deleteMulti($names) ?: null;
    }

multi_get

public function multi_get(array $names, $ttl = 0, $params = [])
  • $names array
    public function multi_get(array $names, $ttl = 0, $params = [])
    {
        if ( ! $this->is_ready()) {
            return null;
        }
        $result = $this->_connection->getMulti($names);
        if ($result === false) {
            return null;
        }
        foreach ((array) $result as $k => $v) {
            if ($v === 'false') {
                $result[$k] = false;
            }
        }
        return $result;
    }

multi_set

public function multi_set(array $data, $ttl = 0)
  • $data array
    public function multi_set(array $data, $ttl = 0)
    {
        if ( ! $this->is_ready()) {
            return null;
        }
        foreach ((array) $data as $k => $v) {
            if ($v === false) {
                $data[$k] = 'false';
            }
        }
        return $this->_connection->setMulti($data, $ttl) ?: null;
    }

set

public function set($name, $data, $ttl = 0)
    public function set($name, $data, $ttl = 0)
    {
        if ( ! $this->is_ready()) {
            return null;
        }
        if ($name === false || $name === null) {
            return null;
        }
        $result = null;
        if ($data === false) {
            $data = 'false';
        }
        // Solved set() trouble with many servers. http://www.php.net/manual/en/function.memcache-set.php#84032
        // TODO: test if really solved (not really checked before)
        if ( ! $this->_connection->replace($name, $data, $ttl)) {
            $result = $this->_connection->set($name, $data, $ttl);
        }
        return $result ?: null;
    }

stats

public function stats()
    public function stats()
    {
        if ( ! $this->is_ready()) {
            return null;
        }
        $stats = $this->_connection->getStats();
        return [
            'hits' => $stats['get_hits'],
            'misses' => $stats['get_misses'],
            'uptime' => $stats['uptime'],
            'mem_usage' => $stats['bytes'],
            'mem_avail' => $stats['limit_maxbytes'],
        ];
    }

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)
    {
        // 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);
    }

__clone

public function __clone()
    public function __clone()
    {
        foreach ((array) get_object_vars($this) as $k => $v) {
            if ($k[0] == '_') {
                unset($this->$k);
            }
        }
    }

__serialize

public function __serialize(): array
    public function __serialize(): array
    {
        $vars = get_object_vars($this);
        unset($vars['_connection'], $vars['_connected_ok']);
        return $vars;
    }

__unserialize

public function __unserialize(array $data): void
  • $data array
    public function __unserialize(array $data): void
    {
        foreach ($data as $k => $v) {
            $this->$k = $v;
        }
    }

_init

public function _init()
    public function _init()
    {
        ($host = getenv('MEMCACHED_HOST') ?: conf('MEMCACHED_HOST') ?: (defined('MEMCACHED_HOST') ? constant('MEMCACHED_HOST') : null)) && $this->DEFAULT['host'] = $host;
        ($port = getenv('MEMCACHED_PORT') ?: conf('MEMCACHED_PORT') ?: (defined('MEMCACHED_PORT') ? constant('MEMCACHED_PORT') : null)) && $this->DEFAULT['port'] = $port;
    }