<?php /*

 Composr
 Copyright (c) ocProducts, 2004-2016

 See text/EN/licence.txt for full licencing information.


 NOTE TO PROGRAMMERS:
   Do not edit this file. If you need to make changes, save your changed file to the appropriate *_custom folder
   **** If you ignore this advice, then your website upgrades (e.g. for bug fixes) will likely kill your changes ****

*/

/**
 * @license    http://opensource.org/licenses/cpal_1.0 Common Public Attribution License
 * @copyright  ocProducts Ltd
 * @package    core
 */

/*EXTRA FUNCTIONS: eaccelerator_.**/

/**
 * Cache driver class.
 */
class Persistent_caching_eacceleratorcache
{
    public $objects_list = null;

    /**
     * Instruction to load up the objects list.
     *
     * @return array The list of objects
     */
    public function &load_objects_list()
    {
        if (is_null($this->objects_list)) {
            $this->objects_list = eaccelerator_get(get_file_base() . 'PERSISTENT_CACHE_OBJECTS');
            if ($this->objects_list === null) {
                $this->objects_list = array();
            }
        }
        return $this->objects_list;
    }

    /**
     * Get data from the persistent cache.
     *
     * @param  string $key Key
     * @param  ?TIME $min_cache_date Minimum timestamp that entries from the cache may hold (null: don't care)
     * @return ?mixed The data (null: not found / null entry)
     */
    public function get($key, $min_cache_date = null)
    {
        $data = eaccelerator_get($key);
        if (is_null($data)) {
            return null;
        }
        if ((!is_null($min_cache_date)) && ($data[0] < $min_cache_date)) {
            return null;
        }
        $ret = @unserialize($data[1]);
        if ($ret === false) {
            return null;
        }
        return $ret;
    }

    /**
     * Put data into the persistent cache.
     *
     * @param  string $key Key
     * @param  mixed $data The data
     * @param  integer $flags Various flags (parameter not used)
     * @param  ?integer $expire_secs The expiration time in seconds (null: no expiry)
     */
    public function set($key, $data, $flags = 0, $expire_secs = null)
    {
        // Update list of persistent-objects
        $this->load_objects_list();
        if (!array_key_exists($key, $this->objects_list)) {
            $this->objects_list[$key] = true;
            eaccelerator_put(get_file_base() . 'PERSISTENT_CACHE_OBJECTS', $this->objects_list, 0);
        }

        eaccelerator_put($key, array(time(), serialize($data)), $expire_secs);
    }

    /**
     * Delete data from the persistent cache.
     *
     * @param  string $key Key
     */
    public function delete($key)
    {
        // Update list of persistent-objects
        $this->load_objects_list();
        unset($this->objects_list[$key]);
        //Wasteful eaccelerator_put(get_file_base() . 'PERSISTENT_CACHE_OBJECTS', $this->objects_list, 0);

        eaccelerator_rm($key);
    }

    /**
     * Remove all data from the persistent cache.
     */
    public function flush()
    {
        $this->load_objects_list();
        foreach (array_keys($this->objects_list) as $obkey) {
            eaccelerator_rm($obkey);
        }

        $this->objects_list = array();
        eaccelerator_put(get_file_base() . 'PERSISTENT_CACHE_OBJECTS', $this->objects_list, 0);
    }
}
