summaryrefslogtreecommitdiff
path: root/system/libraries/Cache.php
blob: c7954d37c2bd2d641b0dc3c61306502ab538ae6a (plain)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
 * Provides a driver-based interface for finding, creating, and deleting cached
 * resources. Caches are identified by a unique string. Tagging of caches is
 * also supported, and caches can be found and deleted by id or tag.
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2009 Kohana Team
 * @license    http://kohanaphp.com/license
 */
class Cache_Core {

	protected static $instances = array();

	// Configuration
	protected $config;

	// Driver object
	protected $driver;

	/**
	 * Returns a singleton instance of Cache.
	 *
	 * @param   string  configuration
	 * @return  Cache_Core
	 */
	public static function & instance($config = FALSE)
	{
		if ( ! isset(Cache::$instances[$config]))
		{
			// Create a new instance
			Cache::$instances[$config] = new Cache($config);
		}

		return Cache::$instances[$config];
	}

	/**
	 * Loads the configured driver and validates it.
	 *
	 * @param   array|string  custom configuration or config group name
	 * @return  void
	 */
	public function __construct($config = FALSE)
	{
		if (is_string($config))
		{
			$name = $config;

			// Test the config group name
			if (($config = Kohana::config('cache.'.$config)) === NULL)
				throw new Cache_Exception('The :group: group is not defined in your configuration.', array(':group:' => $name));
		}

		if (is_array($config))
		{
			// Append the default configuration options
			$config += Kohana::config('cache.default');
		}
		else
		{
			// Load the default group
			$config = Kohana::config('cache.default');
		}

		// Cache the config in the object
		$this->config = $config;

		// Set driver name
		$driver = 'Cache_'.ucfirst($this->config['driver']).'_Driver';

		// Load the driver
		if ( ! Kohana::auto_load($driver))
			throw new Cache_Exception('The :driver: driver for the :class: library could not be found',
									   array(':driver:' => $this->config['driver'], ':class:' => get_class($this)));

		// Initialize the driver
		$this->driver = new $driver($this->config['params']);

		// Validate the driver
		if ( ! ($this->driver instanceof Cache_Driver))
			throw new Cache_Exception('The :driver: driver for the :library: library must implement the :interface: interface',
									   array(':driver:' => $this->config['driver'], ':library:' => get_class($this), ':interface:' => 'Cache_Driver'));

		Kohana_Log::add('debug', 'Cache Library initialized');
	}

	/**
	 * Set cache items
	 */
	public function set($key, $value = NULL, $tags = NULL, $lifetime = NULL)
	{
		if ($lifetime === NULL)
		{
			$lifetime = $this->config['lifetime'];
		}

		if ( ! is_array($key))
		{
			$key = array($key => $value);
		}

		if ($this->config['prefix'] !== NULL)
		{
			$key = $this->add_prefix($key);

			if ($tags !== NULL)
			{
				$tags = $this->add_prefix($tags, FALSE);
			}
		}

		return $this->driver->set($key, $tags, $lifetime);
	}

	/**
	 * Get a cache items by key
	 */
	public function get($keys)
	{
		$single = FALSE;

		if ( ! is_array($keys))
		{
			$keys = array($keys);
			$single = TRUE;
		}

		if ($this->config['prefix'] !== NULL)
		{
			$keys = $this->add_prefix($keys, FALSE);

			if ( ! $single)
			{
			    return $this->strip_prefix($this->driver->get($keys, $single));
			}

		}

		return $this->driver->get($keys, $single);
	}

	/**
	 * Get cache items by tags
	 */
	public function get_tag($tags)
	{
		if ( ! is_array($tags))
		{
			$tags = array($tags);
		}

		if ($this->config['prefix'] !== NULL)
		{
		    $tags = $this->add_prefix($tags, FALSE);
		    return $this->strip_prefix($this->driver->get_tag($tags));
		}
		else
		{
		    return $this->driver->get_tag($tags);
		}
	}

	/**
	 * Delete cache item by key
	 */
	public function delete($keys)
	{
		if ( ! is_array($keys))
		{
			$keys = array($keys);
		}

		if ($this->config['prefix'] !== NULL)
		{
			$keys = $this->add_prefix($keys, FALSE);
		}

		return $this->driver->delete($keys);
	}

	/**
	 * Delete cache items by tag
	 */
	public function delete_tag($tags)
	{
		if ( ! is_array($tags))
		{
			$tags = array($tags);
		}

		if ($this->config['prefix'] !== NULL)
		{
			$tags = $this->add_prefix($tags, FALSE);
		}

		return $this->driver->delete_tag($tags);
	}

	/**
	 * Empty the cache
	 */
	public function delete_all()
	{
		return $this->driver->delete_all();
	}

	/**
	 * Add a prefix to keys or tags
	 */
	protected function add_prefix($array, $to_key = TRUE)
	{
		$out = array();

		foreach($array as $key => $value)
		{
			if ($to_key)
			{
				$out[$this->config['prefix'].$key] = $value;
			}
			else
			{
				$out[$key] = $this->config['prefix'].$value;
			}
		}

		return $out;
	}

	/**
	 * Strip a prefix to keys or tags
	 */
	protected function strip_prefix($array)
	{
		$out = array();

		$start = strlen($this->config['prefix']);

		foreach($array as $key => $value)
		{
			$out[substr($key, $start)] = $value;
		}

		return $out;
	}

} // End Cache Library