yf_cache_driver_files class

Fileplugins/cache/classes/cache/yf_cache_driver_files.class.php
Use_class('cache_driver_files', 'classes/cache/')
Extendsyf_cache_driver

Public properties

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

PropertyDefaultDescription
$CACHE_DIR null
$CREATE_DIRS
bool
true Auto-create cache folder
$DIR_CHMOD
int
511 Octal value of cache dir and subdirs
$DIR_LEVELS
int
2 Number of levels of subdirs, set to 0 to store everything in plain dir
$DIR_STEP
int
testme, subdir == te/st/ */ Property [ public $DIR_STEP = 2 Number of symbols from name to use in subdirs, example: name = testme, subdir == te/st/
$FILE_EXT
@conf_skip
'.php'
$FILE_PREFIX
@conf_skip
'cache_'

Inherited properties

from yf_cache_driver: $_parent, $implemented

Public methods 12

del

public function del($name)
    public function del($name)
    {
        if ( ! $this->is_ready()) {
            return null;
        }
        $path = $this->_dir_by_name($name) . $this->FILE_PREFIX . $name . $this->FILE_EXT;
        if (file_exists($path)) {
            unlink($path);
        }
        return ! file_exists($path) ? true : null;
    }

flush

public function flush()
    public function flush()
    {
        if ( ! $this->is_ready()) {
            return null;
        }
        foreach ((array) $this->_get_all_files() as $path) {
            unlink($path);
        }
        return true;
    }

get

public function get($name, $ttl = 0, $params = [])
    public function get($name, $ttl = 0, $params = [])
    {
        if ( ! $this->is_ready()) {
            return null;
        }
        $path = $this->_dir_by_name($name) . $this->FILE_PREFIX . $name . $this->FILE_EXT;
        return $this->_get_cache_file($path, $ttl);
    }

is_ready

public function is_ready()
    public function is_ready()
    {
        return file_exists($this->CACHE_DIR) && is_writable($this->CACHE_DIR);
    }

list_keys

public function list_keys()
    public function list_keys()
    {
        if ( ! $this->is_ready()) {
            return null;
        }
        $keys = [];
        foreach ((array) $this->_get_all_files() as $path) {
            $name = substr(trim(basename($path)), strlen($this->FILE_PREFIX), -strlen($this->FILE_EXT));
            if ($name) {
                $keys[$name] = $name;
            }
        }
        return array_keys($keys);
    }

set

public function set($name, $data, $ttl = 0)
    public function set($name, $data, $ttl = 0)
    {
        if ( ! $this->is_ready()) {
            return null;
        }
        $path = $this->_dir_by_name($name) . $this->FILE_PREFIX . $name . $this->FILE_EXT;
        return $this->_put_cache_file($data, $path) ?: null;
    }

stats

public function stats()
    public function stats()
    {
        $usage = 0;
        foreach ($this->_get_all_files() as $file) {
            $usage += filesize($file);
        }
        return [
            'hits' => null,
            'misses' => null,
            'uptime' => null,
            'mem_usage' => $usage,
            'mem_avail' => disk_free_space($this->CACHE_DIR),
        ];
    }

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

_dir_by_name

public function _dir_by_name($name)
    public function _dir_by_name($name)
    {
        $dir = $this->CACHE_DIR;
        if ( ! $this->DIR_LEVELS) {
            return $dir;
        }
        $step = $this->DIR_STEP;
        for ($i = 0; $i < $this->DIR_LEVELS; $i++) {
            $dir .= substr($name, $i * $step, $step) . '/';
        }
        if ($this->CREATE_DIRS && ! file_exists($dir)) {
            mkdir($dir, $this->DIR_CHMOD, true);
        }
        return $dir;
    }
public function _get_all_files()
    public function _get_all_files()
    {
        if ( ! $this->is_ready()) {
            return null;
        }
        return _class('dir')->rglob($this->CACHE_DIR, '*' . $this->FILE_PREFIX . '*' . $this->FILE_EXT);
    }
public function _get_cache_file($path = '', $ttl = 0)

Do get cache file contents.

    public function _get_cache_file($path = '', $ttl = 0)
    {
        if (empty($path)) {
            return null;
        }
        if ( ! file_exists($path)) {
            return null;
        }
        $last_modified = filemtime($path);
        $ttl = (int) ($ttl ?: $this->_parent->TTL);
        if ($last_modified < (time() - $ttl)) {
            return null;
        }
        $data = [];
        $_time_start = null;
        if (DEBUG_MODE) {
            $_time_start = microtime(true);
        }

        $data = include $path;

        if (DEBUG_MODE) {
            $_cf = strtolower(str_replace(DIRECTORY_SEPARATOR, '/', $path));
            debug('include_files_exec_time::' . $_cf, microtime(true) - $_time_start);
        }
        return $data;
    }

_init

public function _init()
    public function _init()
    {
        $this->CACHE_DIR = STORAGE_PATH . 'core_cache/';
        if ($this->CREATE_DIRS && ! file_exists($this->CACHE_DIR)) {
            mkdir($this->CACHE_DIR, $this->DIR_CHMOD, true);
        }
    }
public function _put_cache_file($data = [], $path = '')

Do put cache file contents.

    public function _put_cache_file($data = [], $path = '')
    {
        if (empty($path)) {
            return null;
        }
        $str = str_replace(' => ' . PHP_EOL . 'array (', '=>array(', preg_replace('/^\s+/m', '', var_export($data, 1)));
        $str = '<?' . 'php' . PHP_EOL . 'return ' . $str . ';' . PHP_EOL;

        // http://php.net/manual/en/function.file-put-contents.php
        // This function returns the number of bytes that were written to the file, or FALSE on failure.
        return (bool) file_put_contents($path, $str);
    }