/administrator/components/com_zoo/helpers/cache.php

https://bitbucket.org/organicdevelopment/joomla-2.5 · PHP · 213 lines · 78 code · 22 blank · 113 comment · 13 complexity · 31d60dc6a62d37c1f6f847649c09db65 MD5 · raw file

  1. <?php
  2. /**
  3. * @package com_zoo
  4. * @author YOOtheme http://www.yootheme.com
  5. * @copyright Copyright (C) YOOtheme GmbH
  6. * @license http://www.gnu.org/licenses/gpl.html GNU/GPL
  7. */
  8. /**
  9. * The cache helper class.
  10. *
  11. * @package Component.Helpers
  12. * @since 2.0
  13. */
  14. class CacheHelper extends AppHelper {
  15. /**
  16. * Creates an AppCache instance
  17. *
  18. * @param string $file Path to cache file
  19. * @param boolean $hash Wether the key should be hashed
  20. * @param int $lifetime The values lifetime
  21. *
  22. * @return AppCache
  23. *
  24. * @since 2.0
  25. */
  26. public function create($file, $hash = true, $lifetime = null) {
  27. $cache = $this->app->object->create('AppCache', array($file, $hash, $lifetime));
  28. $this->app->zoo->putIndexFile(dirname($file));
  29. return $cache;
  30. }
  31. }
  32. /**
  33. * The cache class.
  34. *
  35. * @package Component.Helpers
  36. * @since 2.0
  37. */
  38. class AppCache {
  39. /**
  40. * Path to cache file
  41. *
  42. * @var string
  43. * @since 2.0
  44. */
  45. protected $_file = 'config.txt';
  46. /**
  47. * Path to cache file
  48. *
  49. * @var array
  50. * @since 2.0
  51. */
  52. protected $_items = array();
  53. /**
  54. * marks cache dirty
  55. *
  56. * @var boolean
  57. * @since 2.0
  58. */
  59. protected $_dirty = false;
  60. /**
  61. * The cached items
  62. *
  63. * @var boolean
  64. * @since 2.0
  65. */
  66. protected $_hash = true;
  67. /**
  68. * Class constructor
  69. *
  70. * @param string $file Path to cache file
  71. * @param boolean $hash Wether the key should be hashed
  72. * @param int $lifetime The values lifetime
  73. * @since 2.0
  74. */
  75. public function __construct($file, $hash = true, $lifetime = null) {
  76. // if cache file doesn't exist, create it
  77. if (!JFile::exists($file)) {
  78. JFolder::create(dirname($file));
  79. $buffer = '';
  80. JFile::write($file, $buffer);
  81. }
  82. // set file and parse it
  83. $this->_file = $file;
  84. $this->_hash = $hash;
  85. $this->_parse();
  86. // clear out of date values
  87. if ($lifetime) {
  88. $lifetime = (int) $lifetime;
  89. $remove = array();
  90. foreach ($this->_items as $key => $value) {
  91. if ((time() - $value['timestamp']) > $lifetime) {
  92. $remove[] = $key;
  93. }
  94. }
  95. foreach ($remove as $key) {
  96. unset($this->_items[$key]);
  97. }
  98. }
  99. }
  100. /**
  101. * Check if the cache file is writable and readable
  102. *
  103. * @return boolean If the cache can be used
  104. *
  105. * @since 2.0
  106. */
  107. public function check() {
  108. return is_readable($this->_file) && is_writable($this->_file);
  109. }
  110. /**
  111. * Get a cache content
  112. *
  113. * @param string $key The key
  114. *
  115. * @return mixed The cache content
  116. *
  117. * @since 2.0
  118. */
  119. public function get($key) {
  120. if ($this->_hash)
  121. $key = md5($key);
  122. if (!array_key_exists($key, $this->_items))
  123. return null;
  124. return $this->_items[$key]['value'];
  125. }
  126. /**
  127. * Set a cache content
  128. *
  129. * @param string $key The key
  130. * @param mixed $value The value
  131. *
  132. * @return AppCache $this for chaining support
  133. *
  134. * @since 2.0
  135. */
  136. public function set($key, $value) {
  137. if ($this->_hash)
  138. $key = md5($key);
  139. if (array_key_exists($key, $this->_items) && @$this->_items[$key]['value'] == $value)
  140. return $this;
  141. $this->_items[$key]['value'] = $value;
  142. $this->_items[$key]['timestamp'] = time();
  143. $this->_dirty = true;
  144. return $this;
  145. }
  146. /**
  147. * Parse the cache file
  148. *
  149. * @return AppCache $this for chaining support
  150. *
  151. * @since 2.0
  152. */
  153. protected function _parse() {
  154. $content = JFile::read($this->_file);
  155. if (!empty($content)) {
  156. $items = json_decode($content, true);
  157. if (is_array($items)) {
  158. $this->_items = $items;
  159. }
  160. }
  161. return $this;
  162. }
  163. /**
  164. * Save the cache file if it was changed
  165. *
  166. * @return AppCache $this for chaining support
  167. *
  168. * @since 2.0
  169. */
  170. public function save() {
  171. if ($this->_dirty) {
  172. JFile::write($this->_file, json_encode($this->_items));
  173. }
  174. return $this;
  175. }
  176. /**
  177. * Clear the cache file
  178. *
  179. * @return AppCache $this for chaining support
  180. */
  181. public function clear() {
  182. $this->_items = array();
  183. $this->_dirty = true;
  184. return $this;
  185. }
  186. }
  187. /**
  188. * AppCacheException identifies an Exception in the AppCache class
  189. * @see AppCache
  190. */
  191. class AppCacheException extends AppException {}