joomla-2.5 /administrator/components/com_zoo/helpers/cache.php

Language PHP Lines 213
MD5 Hash 31d60dc6a62d37c1f6f847649c09db65 Estimated Cost $1,805 (why?)
Repository https://bitbucket.org/organicdevelopment/joomla-2.5.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
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
211
212
213
<?php
/**
* @package   com_zoo
* @author    YOOtheme http://www.yootheme.com
* @copyright Copyright (C) YOOtheme GmbH
* @license   http://www.gnu.org/licenses/gpl.html GNU/GPL
*/

/**
 * The cache helper class.
 *
 * @package Component.Helpers
 * @since 2.0
 */
class CacheHelper extends AppHelper {

	/**
	 * Creates an AppCache instance
	 *
	 * @param string $file Path to cache file
	 * @param boolean $hash Wether the key should be hashed
	 * @param int $lifetime The values lifetime
	 *
	 * @return AppCache
	 *
	 * @since 2.0
	 */
	public function create($file, $hash = true, $lifetime = null) {
		$cache = $this->app->object->create('AppCache', array($file, $hash, $lifetime));
		$this->app->zoo->putIndexFile(dirname($file));
		return $cache;
	}

}

/**
 * The cache class.
 *
 * @package Component.Helpers
 * @since 2.0
 */
class AppCache {

	/**
	 * Path to cache file
	 *
	 * @var string
	 * @since 2.0
	 */
	protected $_file = 'config.txt';

	/**
	 * Path to cache file
	 *
	 * @var array
	 * @since 2.0
	 */
	protected $_items = array();

	/**
	 * marks cache dirty
	 *
	 * @var boolean
	 * @since 2.0
	 */
	protected $_dirty = false;

	/**
	 * The cached items
	 *
	 * @var boolean
	 * @since 2.0
	 */
	protected $_hash = true;

	/**
	 * Class constructor
	 *
	 * @param string $file Path to cache file
	 * @param boolean $hash Wether the key should be hashed
	 * @param int $lifetime The values lifetime
	 * @since 2.0
	 */
	public function __construct($file, $hash = true, $lifetime = null) {

		// if cache file doesn't exist, create it
		if (!JFile::exists($file)) {
			JFolder::create(dirname($file));
			$buffer = '';
			JFile::write($file, $buffer);
		}

		// set file and parse it
		$this->_file = $file;
		$this->_hash = $hash;
		$this->_parse();

		// clear out of date values
		if ($lifetime) {
			$lifetime = (int) $lifetime;
			$remove = array();
			foreach ($this->_items as $key => $value) {
				if ((time() - $value['timestamp']) > $lifetime) {
					$remove[] = $key;
				}
			}
			foreach ($remove as $key) {
				unset($this->_items[$key]);
			}
		}
	}

	/**
	 * Check if the cache file is writable and readable
	 *
	 * @return boolean If the cache can be used
	 *
	 * @since 2.0
	 */
	public function check() {
		return is_readable($this->_file) && is_writable($this->_file);
	}

	/**
	 * Get a cache content
	 *
	 * @param  string $key The key
	 *
	 * @return mixed      The cache content
	 *
	 * @since 2.0
	 */
	public function get($key) {
		if ($this->_hash)
			$key = md5($key);
		if (!array_key_exists($key, $this->_items))
			return null;

		return $this->_items[$key]['value'];
	}

	/**
	 * Set a cache content
	 *
	 * @param string $key   The key
	 * @param mixed $value The value
	 *
	 * @return AppCache $this for chaining support
	 *
	 * @since 2.0
	 */
	public function set($key, $value) {
		if ($this->_hash)
			$key = md5($key);
		if (array_key_exists($key, $this->_items) && @$this->_items[$key]['value'] == $value)
			return $this;

		$this->_items[$key]['value'] = $value;
		$this->_items[$key]['timestamp'] = time();
		$this->_dirty = true;
		return $this;
	}

	/**
	 * Parse the cache file
	 *
	 * @return AppCache $this for chaining support
	 *
	 * @since 2.0
	 */
	protected function _parse() {
		$content = JFile::read($this->_file);
		if (!empty($content)) {
			$items = json_decode($content, true);
			if (is_array($items)) {
				$this->_items = $items;
			}
		}
		return $this;
	}

	/**
	 * Save the cache file if it was changed
	 *
	 * @return AppCache $this for chaining support
	 *
	 * @since 2.0
	 */
	public function save() {
		if ($this->_dirty) {
			JFile::write($this->_file, json_encode($this->_items));
		}
		return $this;
	}

	/**
	 * Clear the cache file
	 *
	 * @return AppCache $this for chaining support
	 */
	public function clear() {
		$this->_items = array();
		$this->_dirty = true;
		return $this;
	}

}

/**
 * AppCacheException identifies an Exception in the AppCache class
 * @see AppCache
 */
class AppCacheException extends AppException {}
Back to Top