PageRenderTime 103ms CodeModel.GetById 40ms app.highlight 5ms RepoModel.GetById 55ms app.codeStats 1ms

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