PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/gantry/core/utilities/gantrycache.class.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 171 lines | 102 code | 22 blank | 47 comment | 16 complexity | 87c402bcbed0cb3e3a986be1024a233b MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT
  1. <?php
  2. /**
  3. * @version 3.2.22 August 3, 2012
  4. * @author RocketTheme http://www.rockettheme.com
  5. * @copyright Copyright (C) 2007 - 2012 RocketTheme, LLC
  6. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  7. */
  8. require_once(dirname(__FILE__) . '/cache/cache.class.php');
  9. require_once(dirname(__FILE__) . '/cache/joomlaCacheDriver.class.php');
  10. require_once(dirname(__FILE__) . '/cache/fileCacheDriver.class.php');
  11. gantry_import('core.gantrysingleton');
  12. /**
  13. *
  14. */
  15. class GantryCache
  16. {
  17. static $instance;
  18. const GROUP_NAME = 'Gantry';
  19. /**
  20. * Files to watch for changes. Invalidate cache
  21. * @var array
  22. */
  23. protected $watch_files = array();
  24. /**
  25. * The cache object.
  26. *
  27. * @var Cache
  28. */
  29. protected $cache = null;
  30. /**
  31. * Lifetime of the cache
  32. * @access private
  33. * @var int
  34. */
  35. protected $lifetime = 900;
  36. /**
  37. * @var string
  38. */
  39. protected $base_cache_dir;
  40. /**
  41. * @static
  42. * @param bool $admin
  43. * @return GantryCache
  44. */
  45. public static function getInstance($admin = false)
  46. {
  47. if (!self::$instance) {
  48. self::$instance = new GantryCache($admin);
  49. }
  50. return self::$instance;
  51. }
  52. public function __construct($admin = false)
  53. {
  54. $this->base_cache_dir = JPATH_BASE . '/cache/_gantry-3.2.22/';
  55. $this->cache = new GantryCacheLib();
  56. $this->init($admin);
  57. }
  58. public function init($admin)
  59. {
  60. $conf = & JFactory::getConfig();
  61. if (!$admin && $conf->getValue('config.caching')) {
  62. // Use Joomla Cache for the frontend
  63. $this->cache->addDriver('frontend', new JoomlaCacheDriver(self::GROUP_NAME, $this->lifetime));
  64. }
  65. elseif ($admin) {
  66. // TODO get lifetime for backend cache
  67. $cache_dir = JPATH_BASE . '/cache/_gantry-3.2.22/';
  68. $this->cache->addDriver('admin', new FileCacheDriver($this->lifetime, $cache_dir));
  69. }
  70. }
  71. protected function checkForClear(){
  72. if ($this->checkWatchedFiles()){
  73. $this->clearGroupCache();
  74. }
  75. }
  76. public function call($identifier, $function = null, $arguments = array())
  77. {
  78. $this->checkForClear();
  79. $ret = $this->cache->get(self::GROUP_NAME, $identifier);
  80. if ($ret == false && $function != null) {
  81. $ret = call_user_func_array($function, $arguments);
  82. $this->cache->set(self::GROUP_NAME, $identifier, $ret);
  83. }
  84. return $ret;
  85. }
  86. public function get($identifier){
  87. $this->checkForClear();
  88. return $this->cache->get(self::GROUP_NAME, $identifier);
  89. }
  90. public function set($identifier, $data){
  91. return $this->cache->set(self::GROUP_NAME, $identifier, $data);
  92. }
  93. public function clearAllCache()
  94. {
  95. return $this->cache->clearAllCache();
  96. }
  97. public function clearGroupCache()
  98. {
  99. return $this->cache->clearGroupCache(self::GROUP_NAME);
  100. }
  101. public function clear($identifier)
  102. {
  103. return $this->cache->clearCache(self::GROUP_NAME, $identifier);
  104. }
  105. /**
  106. * Gets the lifetime for gantry
  107. * @access public
  108. * @return int
  109. */
  110. public function getLifetime()
  111. {
  112. return $this->lifetime;
  113. }
  114. /**
  115. * Sets the lifetime for gantry
  116. * @access public
  117. * @param int $lifetime
  118. */
  119. public function setLifetime($lifetime)
  120. {
  121. $this->lifetime = $lifetime;
  122. $this->cache->setLifeTime($lifetime);
  123. }
  124. public function addWatchFile($filepath){
  125. if (file_exists($filepath) && !in_array($filepath, $this->watch_files)){
  126. $key = md5($filepath);
  127. $this->watch_files[$key] = $filepath;
  128. if ($this->cache->get(self::GROUP_NAME,$key) === false){
  129. $this->cache->set(self::GROUP_NAME,$key, filemtime($filepath));
  130. }
  131. }
  132. }
  133. /**
  134. * @return bool true if the cache needs to be clean
  135. */
  136. protected function checkWatchedFiles(){
  137. // check the watch files
  138. foreach($this->watch_files as $key => $watchfile){
  139. if (file_exists($watchfile)){
  140. $file_mtime = filemtime($watchfile);
  141. $cached_filemtime = $this->cache->get(self::GROUP_NAME,$key);
  142. if ($cached_filemtime != false && $file_mtime != $cached_filemtime){
  143. return true;
  144. }
  145. }
  146. }
  147. return false;
  148. }
  149. }