This is the API documentation for concrete5 version 5.6 and earlier. View Current API Documentation
  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 
<?php

class Concrete5_Library_Cache {
    
    public static function key($type, $id) {
        return md5($type . $id);
    }
    
    public function getLibrary() {
        static $cache;
        if (!isset($cache) && defined('DIR_FILES_CACHE')) {
            if (is_dir(DIR_FILES_CACHE) && is_writable(DIR_FILES_CACHE)) {
                Loader::library('3rdparty/Zend/Cache');

                $frontendOptions = array(
                    'lifetime' => CACHE_LIFETIME,
                    'automatic_serialization' => true,
                    'cache_id_prefix' => CACHE_ID       
                );
                $backendOptions = array(
                    'read_control' => false,
                    'cache_dir' => DIR_FILES_CACHE,
                    'file_locking' => false
                );
                if (defined('CACHE_BACKEND_OPTIONS')) {
                    $opts = unserialize(CACHE_BACKEND_OPTIONS);
                    foreach($opts as $k => $v) {
                        $backendOptions[$k] = $v;
                    }
                }
                if (defined('CACHE_FRONTEND_OPTIONS')) {
                    $opts = unserialize(CACHE_FRONTEND_OPTIONS);
                    foreach($opts as $k => $v) {
                        $frontendOptions[$k] = $v;
                    }
                }
                if (!defined('CACHE_LIBRARY') || (defined("CACHE_LIBRARY") && CACHE_LIBRARY == "default")) {
                    define('CACHE_LIBRARY', 'File');
                }
                $customBackendNaming = false;
                if (CACHE_LIBRARY == 'Zend_Cache_Backend_ZendServer_Shmem') {
                    $customBackendNaming = true;
                }
                $cache = Zend_Cache::factory('Core', CACHE_LIBRARY, $frontendOptions, $backendOptions, false, $customBackendNaming);
            }
        }
        return $cache;
    }
    
    public function startup() {
        $cache = Cache::getLibrary();
    }
    
    public function disableCache() {
        $ca = Cache::getLibrary();
        if (is_object($ca)) {
            $ca->setOption('caching', false);
        }
    }
    
    public function enableCache() {
        $ca = Cache::getLibrary();
        if (is_object($ca)) {
            $ca->setOption('caching', true);
        }
    }
    
    public function disableLocalCache() {
        CacheLocal::get()->enabled = false;
    }
    public function enableLocalCache() {
        CacheLocal::get()->enabled = true;
    }
    
    /** 
     * Inserts or updates an item to the cache
     * the cache must always be enabled for (getting remote data, etc..)
     */ 
    public function set($type, $id, $obj, $expire = false) {
        $loc = CacheLocal::get();
        if ($loc->enabled) {
            if (is_object($obj)) {
                $r = clone $obj;
            } else {
                $r = $obj;
            }
            $loc->cache[Cache::key($type, $id)] = $r;
        }
        $cache = Cache::getLibrary();
        if (!$cache) {
            return false;
        }
        $cache->save($obj, Cache::key($type, $id), array($type), $expire);
    }
    
    /** 
     * Retrieves an item from the cache
     */ 
    public function get($type, $id, $mustBeNewerThan = false) {
        $loc = CacheLocal::get();
        $key = Cache::key($type, $id);
        if ($loc->enabled && array_key_exists($key, $loc->cache)) {
            return $loc->cache[$key];
        }
            
        $cache = Cache::getLibrary();
        if (!$cache) {
            if ($loc->enabled) {
                $loc->cache[$key] = false;
            }
            return false;
        }
        
        // if mustBeNewerThan is set, we check the cache mtime
        // if mustBeNewerThan is newer than that time, we relinquish
        if ($mustBeNewerThan != false) {
            $metadata = $cache->getMetadatas($key);
            if ($metadata['mtime'] < $mustBeNewerThan) {
                // clear cache record and return false
                Cache::getLibrary()->remove($key);
                if ($loc->enabled) {
                    $loc->cache[$key] = false;
                }
                return false;
            }
        }
        
        $loaded = $cache->load($key);
        if ($loc->enabled) {
            $loc->cache[$key] = $loaded;
        }
        return $loaded;
    }

    /** 
     * not used. Good idea but doesn't work with all cache layers and on large caches is VERY slow.

    public function deleteType($type) {
        Cache::getLibrary()->clean('matchingTag', array($type));
        $loc = CacheLocal::get();
        $loc->enabled = false;
    }
    
    */
    
    /** 
     * Removes an item from the cache
     */ 
    public function delete($type, $id){
        $cache = Cache::getLibrary();
        if ($cache) {
            $cache->remove(Cache::key($type, $id));
        }

        $loc = CacheLocal::get();
        if ($loc->enabled && isset($loc->cache[Cache::key($type, $id)])) {
            unset($loc->cache[Cache::key($type, $id)]);
        }
    }
    
    /** 
     * Completely flushes the cache
     */ 
    public function flush() {
        $db = Loader::db();
        $r = $db->MetaTables();

        // flush the CSS cache
        if (is_dir(DIR_FILES_CACHE . '/' . DIRNAME_CSS)) {
            $fh = Loader::helper("file");
            $fh->removeAll(DIR_FILES_CACHE . '/' . DIRNAME_CSS);
        }
        
        $pageCache = PageCache::getLibrary();
        if (is_object($pageCache)) {
            $pageCache->flush();
        }
        
        if (in_array('Config', $r)) {
            // clear the environment overrides cache
            $env = Environment::get();
            $env->clearOverrideCache();

            if(in_array('btCachedBlockRecord', $db->MetaColumnNames('Blocks'))) {
                $db->Execute('update Blocks set btCachedBlockRecord = null');
            }
            if (in_array('CollectionVersionBlocksOutputCache', $r)) {
                $db->Execute('truncate table CollectionVersionBlocksOutputCache');
            }
        }
        
        $loc = CacheLocal::get();
        $loc->cache = array();

        $cache = Cache::getLibrary();
        if ($cache) {
            $cache->setOption('caching', true);
            $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
        }

        if (function_exists('apc_clear_cache')) {
            apc_clear_cache();
        }        

        Events::fire('on_cache_flush', $cache);
        return true;
    }
        
}