https-bitbucket.org-bityvip /tmp/busines13_bundle_installer/lib_gantry/core/utilities/gantrycache.class.php

Language PHP Lines 171
MD5 Hash 87c402bcbed0cb3e3a986be1024a233b Estimated Cost $2,408 (why?)
Repository https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip.git View Raw File View Project SPDX
  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
<?php
/**
 * @version   3.2.22 August 3, 2012
 * @author    RocketTheme http://www.rockettheme.com
 * @copyright Copyright (C) 2007 - 2012 RocketTheme, LLC
 * @license   http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
 */

require_once(dirname(__FILE__) . '/cache/cache.class.php');
require_once(dirname(__FILE__) . '/cache/joomlaCacheDriver.class.php');
require_once(dirname(__FILE__) . '/cache/fileCacheDriver.class.php');

gantry_import('core.gantrysingleton');
/**
 *
 */
class GantryCache
{
    static $instance;

    const GROUP_NAME = 'Gantry';

    /**
     * Files to watch for changes.  Invalidate cache
     * @var array
     */
    protected $watch_files = array();

    /**
     * The cache object.
     *
     * @var Cache
     */
    protected $cache = null;

    /**
     * Lifetime of the cache
     * @access private
     * @var int
     */
    protected $lifetime = 900;

    /**
     * @var string
     */
    protected $base_cache_dir;

    /**
     * @static
     * @param bool $admin
     * @return GantryCache
     */
    public static function getInstance($admin = false)
    {
        if (!self::$instance) {
            self::$instance = new GantryCache($admin);
        }
        return self::$instance;
    }

    public function __construct($admin = false)
    {
        $this->base_cache_dir = JPATH_BASE . '/cache/_gantry-3.2.22/';
        $this->cache = new GantryCacheLib();
        $this->init($admin);
    }

    public function init($admin)
    {
        $conf = & JFactory::getConfig();

        if (!$admin && $conf->getValue('config.caching')) {
            // Use Joomla Cache for the frontend
            $this->cache->addDriver('frontend', new JoomlaCacheDriver(self::GROUP_NAME, $this->lifetime));
        }
        elseif ($admin) {
            // TODO get lifetime for backend cache
            $cache_dir = JPATH_BASE . '/cache/_gantry-3.2.22/';
            $this->cache->addDriver('admin', new FileCacheDriver($this->lifetime, $cache_dir));                 
        }
    }

    protected function checkForClear(){
        if ($this->checkWatchedFiles()){
            $this->clearGroupCache();
        }
    }

    public function call($identifier, $function = null, $arguments = array())
    {
        $this->checkForClear();
        $ret = $this->cache->get(self::GROUP_NAME, $identifier);
        if ($ret == false && $function != null) {
            $ret = call_user_func_array($function, $arguments);
            $this->cache->set(self::GROUP_NAME, $identifier, $ret);
        }
        return $ret;
    }

    public function get($identifier){
        $this->checkForClear();
        return $this->cache->get(self::GROUP_NAME, $identifier);
    }

    public function set($identifier, $data){
        return $this->cache->set(self::GROUP_NAME, $identifier, $data);
    }

    public function clearAllCache()
    {
        return $this->cache->clearAllCache();
    }

    public function clearGroupCache()
    {
        return $this->cache->clearGroupCache(self::GROUP_NAME);
    }

    public function clear($identifier)
    {
        return $this->cache->clearCache(self::GROUP_NAME, $identifier);
    }

    /**
     * Gets the lifetime for gantry
     * @access public
     * @return int
     */
    public function getLifetime()
    {
        return $this->lifetime;
    }

    /**
     * Sets the lifetime for gantry
     * @access public
     * @param int $lifetime
     */
    public function setLifetime($lifetime)
    {
        $this->lifetime = $lifetime;
        $this->cache->setLifeTime($lifetime);
    }

    public function addWatchFile($filepath){
        if (file_exists($filepath) && !in_array($filepath, $this->watch_files)){
            $key = md5($filepath);
            $this->watch_files[$key] = $filepath;
            if ($this->cache->get(self::GROUP_NAME,$key) === false){
                $this->cache->set(self::GROUP_NAME,$key, filemtime($filepath));
            }
        }
    }

    /**
     * @return bool true if the cache needs to be clean
     */
    protected function checkWatchedFiles(){
        // check the watch files
        foreach($this->watch_files as $key => $watchfile){
            if (file_exists($watchfile)){
                $file_mtime = filemtime($watchfile);
                $cached_filemtime = $this->cache->get(self::GROUP_NAME,$key);
                if ($cached_filemtime != false && $file_mtime != $cached_filemtime){
                    return true;
                }
            }
        }
        return false;
    }
}
Back to Top