Classes

File io/data/cache/CacheManager.class.php

File io/data/cache/CacheManager.class.php

  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 
<?php
/**
 * This class manages cache. It makes a two-level lazy loading:
 * <ul>
 *  <li>A top-level cache which avoids loading a data if it has already been done since the
 * beginning of the current page generation. This cache has a short life span: it's flushed
 * as of the PHP interpreter reaches the end of the page generation.</li>
 *  <li>A filesystem or shared RAM (via APC) cache to avoid querying the database every time to obtain the same value.
 * This cache is less powerful than the previous but has an infinite life span. Indeed, it's
 * valid until the value changes and the manager is asked to store it</li>
 * </ul>
 * @package     IO
 * @subpackage  Data\cache
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Benoit SAUTEL <ben.popeye@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2014 12 22
 * @since       PHPBoost 3.0 - 2009 09 16
*/

class CacheManager
{
    /**
     * @var DataStore The RAM cache
     */
    private static $ram_cache = null;

    /**
     * @var DataStore
     */
    private static $fs_cache = null;

    /**
     * @return DataStore
     */
    private static function get_ram_cache()
    {
        if (self::$ram_cache == null)
        {
            self::$ram_cache = new RAMDataStore();
        }
        return self::$ram_cache;
    }

    /**
     * @return DataStore
     */
    private static function get_fs_cache()
    {
        if (self::$fs_cache === null)
        {
            self::$fs_cache = DataStoreFactory::get_filesystem_store(__CLASS__);
        }
        return self::$fs_cache;
    }

    /**
     * Loads the data which is identified by the parameters
     * @param string $classname Name of the class of which the result will be an instance
     * @param string $module_name Name of the module owning the entry to load
     * @param string $entry_name If the module wants to manage several entries,
     * it's the name of the entry you want to load
     * @return CacheData The loaded data
     */
    public static function load($classname, $module_name, $entry_name = '')
    {
        $name = self::compute_entry_name($module_name, $entry_name);
        try
        {
            return self::try_load($classname, $module_name, $entry_name);
        }
        catch(CacheDataNotFoundException $ex)
        {
            //Not cached anywhere, we create it
            $data = new $classname();
            $data->synchronize();
            self::file_cache_data($name, $data);
            self::memory_cache_data($name, $data);
            return $data;
        }
    }

    /**
     * Tries to load the data which is identified by the parameters
     * @param string $classname Name of the class of which the result will be an instance
     * @param string $module_name Name of the module
     * @param string $entry_name Name of the entry of the module
     * @return CacheData The loaded data
     * @throws CacheDataNotFoundException if the cache doesn't exist
     */
    public static function try_load($classname, $module_name, $entry_name)
    {
        $name = self::compute_entry_name($module_name, $entry_name);

        if (self::is_memory_cached($name))
        {
            return self::get_memory_cached_data($name);
        }
        else if (self::is_file_cached($name))
        {
            $data = self::get_file_cached_data($name);
            if ($data instanceof $classname)
            {
                self::memory_cache_data($name, $data);
                return $data;
            }
        }
        throw new CacheDataNotFoundException($name);
    }

    /**
     * Invalidates an entry which is cached. If the corresponding data are loaded agin,
     * they will be regenerated.
     * @param string $module_name Name of the module owning the entry to invalidate
     * @param string $entry_name If the module wants to manage several entries,
     * it's the name of the entry you want to invalidate
     */
    public static function invalidate($module_name, $entry_name = '')
    {
        $name = self::compute_entry_name($module_name, $entry_name);
        self::invalidate_file_cache($name);
        self::invalidate_memory_cache($name);
    }

    /**
     * Invalidates all the cached data
     */
    public static function clear()
    {
        self::get_ram_cache()->clear();
        self::get_fs_cache()->clear();
    }

    /**
     * Caches the data corresponding to the given identifier
     * @param mixed $data The data to cache
     * @param string $module_name Name of the module owning the entry to save
     * @param string $entry_name Name of the entry to save
     */
    public static function save($data, $module_name, $entry_name = '')
    {
        $name = self::compute_entry_name($module_name, $entry_name);
        self::file_cache_data($name, $data);
        self::memory_cache_data($name, $data);
    }

    /**
     * @return string
     */
    private static function compute_entry_name($module_name, $entry_name)
    {
        if (!empty($entry_name))
        {
            return Url::encode_rewrite($module_name . '-' . $entry_name);
        }
        else
        {
            return Url::encode_rewrite($module_name);
        }
    }

    //RAM cache management
    /**
     * @return bool
     */
    private static function is_memory_cached($name)
    {
        return self::get_ram_cache()->contains($name);
    }

    /**
     * @return CacheData
     */
    private static function get_memory_cached_data($name)
    {
        return self::get_ram_cache()->get($name);
    }

    private static function memory_cache_data($name, CacheData  $value)
    {
        self::get_ram_cache()->store($name, $value);
    }

    private static function invalidate_memory_cache($name)
    {
        self::get_ram_cache()->delete($name);
    }

    private static function get_file_name($name)
    {
        return $name . '.data';
    }

    /**
     * @return bool
     */
    private static function is_file_cached($name)
    {
        return self::get_fs_cache()->contains(self::get_file_name($name));
    }

    /**
     * @return CacheData
     */
    private static function get_file_cached_data($name)
    {
        return self::get_fs_cache()->get(self::get_file_name($name));
    }

    private static function file_cache_data($name, CacheData $value)
    {
        self::get_fs_cache()->store(self::get_file_name($name), $value);
    }

    private static function invalidate_file_cache($name)
    {
        self::get_fs_cache()->delete(self::get_file_name($name));
    }
}
?>