plugins/cache/classes/cache/yf_cache_driver_xcache.class.php

Summary

Maintainability
A
3 hrs
Test Coverage
<?php

load('cache_driver', '', 'classes/cache/');
class yf_cache_driver_xcache extends yf_cache_driver
{
    /**
     * Catch missing method call.
     * @param mixed $name
     * @param mixed $args
     */
    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);
    }


    public function _init()
    {
        //        ini_set('xcache.admin.user', 'yf_xcache_admin');
//        ini_set('xcache.admin.pass', md5('yf_xcache_pass'));
    }


    public function is_ready()
    {
        return function_exists('xcache_get') && ini_get('xcache.cacher');
    }

    /**
     * @param mixed $name
     * @param mixed $ttl
     * @param mixed $params
     */
    public function get($name, $ttl = 0, $params = [])
    {
        if ( ! $this->is_ready()) {
            return null;
        }
        $result = xcache_get($name);
        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;
    }

    /**
     * @param mixed $name
     * @param mixed $data
     * @param mixed $ttl
     */
    public function set($name, $data, $ttl = 0)
    {
        if ( ! $this->is_ready()) {
            return null;
        }
        if ($data === false) {
            $data = 'false';
        }
        return xcache_set($name, $data, $ttl);
    }

    /**
     * @param mixed $name
     */
    public function del($name)
    {
        if ( ! $this->is_ready()) {
            return null;
        }
        return xcache_unset($name);
    }


    public function flush()
    {
        if ( ! $this->is_ready()) {
            return null;
        }
        if ($this->_check_xcache_auth()) {
            xcache_clear_cache(XC_TYPE_VAR, 0);
            return true;
        }
        return null;
    }


    public function stats()
    {
        if ( ! $this->is_ready()) {
            return null;
        }
        if ( ! $this->_check_xcache_auth()) {
            return null;
        }
        $info = xcache_info(XC_TYPE_VAR, 0);
        return [
            'hits' => $info['hits'],
            'misses' => $info['misses'],
            'uptime' => null,
            'mem_usage' => $info['size'],
            'mem_avail' => $info['avail'],
        ];
    }


    protected function _check_xcache_auth()
    {
        if (ini_get('xcache.admin.enable_auth')) {
            throw new Exception('To use all features of Xcache cache, you must set "xcache.admin.enable_auth" to "Off" in your php.ini.');
            return null;
        }
        return true;
    }
}