Location: PHPKode > projects > ContentCMS > core/content_cache.class.php
<?php
/* 
 * @package     ContentCMS
 * @author      Dan Goldsmith
 * @copyright   Dan Goldsmith 2012
 * @link        http://contentcms.d2g.org.uk/
 * @version     {SUBVERSION_BUILD_NUMBER}
 * 
 * @licence     MPL 2.0
 * 
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 
 *
 */
class content_cache
{
    private $content_id = null;
    private $parameters = null;
    
    public function __construct($content_id,$parameters)
    {
        if(is_array($parameters))
        {
            array_multisort($parameters);
        }
        
        $this->setParameters($parameters);
        $this->setContentID($content_id);
    }
    
    private function setContentID($value)
    {
        $this->content_id = $value;
    }
    
    public function getContentID()
    {
        return $this->content_id;
    }
    
    private function setParameters($value)
    {
        $this->parameters = $value;
    }
    
    public function getParameters()
    {
        return $this->parameters;
    }
    
    public function getKey()
    {
        return $this->getContentID() . "_" . md5(json_encode($this->getParameters()));
    }

    public function setCache($value)
    {
        $cache = new simple_cache($this->getKey());
        $cache->setCache($value);
    }
    
    public function getCache()
    {
        $cache = new simple_cache($this->getKey());
        return $cache->getCache();
    }
    
    public function addLink($content_id,$parameters)
    {
        if(is_array($parameters))
        {
            array_multisort($parameters);
        }
        
        $parent_key = $content_id . "_" . md5(json_encode($parameters));
        
        $links = $this->getLinks();
        if(!in_array($parent_key, $links))
        {
            $links[] = $parent_key;
            $this->setLinks($links);
        }
    }
    
    public function setLinks($array_links)
    {
        $links_cache = new simple_cache($this->getKey() . '.links');
        $links_cache->setCache(serialize($array_links));        
    }
    
    public function getLinks()
    {
        $links_cache = new simple_cache($this->getKey() . '.links');
        $cache = $links_cache->getCache();
        if($cache === null)
        {
            return array();
        }
        else
        {
            return unserialize($links_cache->getCache());
        }
    }
    
    public function deleteCache()
    {
        $cache = new simple_cache($this->getKey());
        $cache->delete();
    }
    
    public static function clearCacheForContentID($content_id)
    {
        $content_id_cached_items = new RegexIterator(new ArrayIterator(simple_cache::getKeys()), '/^' . $content_id . '_[^\.]*$/');
        foreach ($content_id_cached_items as $cache_key) 
        {
            self::recursiveDeleteKey($cache_key);
        }
    }
    
    private static function recursiveDeleteKey($key)
    {
        $cache = new simple_cache($key);
        $cache->delete();
        
        $links_cache = new simple_cache($key . ".links");
        $cache_data = $links_cache->getCache();
        
        $links = array();
        
        if($cache_data !== null)
        {
            $links = unserialize($cache_data);
        }
        
        foreach($links as $link)
        {
            self::recursiveDeleteKey($link);
        }        
        $links_cache->delete();
    }
    
}

?>
Return current item: ContentCMS