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
|
<?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.
*
* $Id$
*
* @package Cache
* @author Kohana Team
* @copyright (c) 2007-2008 Kohana Team
* @license http://kohanaphp.com/license.html
*/
class Cache_Core {
// For garbage collection
protected static $loaded;
// Configuration
protected $config;
// Driver object
protected $driver;
/**
* Returns a singleton instance of Cache.
*
* @param array configuration
* @return Cache_Core
*/
public static function instance($config = array())
{
static $obj;
// Create the Cache instance
($obj === NULL) and $obj = new Cache($config);
return $obj;
}
/**
* 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 Kohana_Exception('cache.undefined_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 Kohana_Exception('core.driver_not_found', $this->config['driver'], get_class($this));
// Initialize the driver
$this->driver = new $driver($this->config['params']);
// Validate the driver
if ( ! ($this->driver instanceof Cache_Driver))
throw new Kohana_Exception('core.driver_implements', $this->config['driver'], get_class($this), 'Cache_Driver');
Kohana::log('debug', 'Cache Library initialized');
if (self::$loaded !== TRUE)
{
$this->config['requests'] = (int) $this->config['requests'];
if ($this->config['requests'] > 0 AND mt_rand(1, $this->config['requests']) === 1)
{
// Do garbage collection
$this->driver->delete_expired();
Kohana::log('debug', 'Cache: Expired caches deleted.');
}
// Cache has been loaded once
self::$loaded = TRUE;
}
}
/**
* Fetches a cache by id. Non-string cache items are automatically
* unserialized before the cache is returned. NULL is returned when
* a cache item is not found.
*
* @param string cache id
* @return mixed cached data or NULL
*/
public function get($id)
{
// Change slashes to colons
$id = str_replace(array('/', '\\'), '=', $id);
if ($data = $this->driver->get($id))
{
if (substr($data, 0, 14) === '<{serialized}>')
{
// Data has been serialized, unserialize now
$data = unserialize(substr($data, 14));
}
}
return $data;
}
/**
* Fetches all of the caches for a given tag. An empty array will be
* returned when no matching caches are found.
*
* @param string cache tag
* @return array all cache items matching the tag
*/
public function find($tag)
{
if ($ids = $this->driver->find($tag))
{
$data = array();
foreach ($ids as $id)
{
// Load each cache item and add it to the array
if (($cache = $this->get($id)) !== NULL)
{
$data[$id] = $cache;
}
}
return $data;
}
return array();
}
/**
* Set a cache item by id. Tags may also be added and a custom lifetime
* can be set. Non-string data is automatically serialized.
*
* @param string unique cache id
* @param mixed data to cache
* @param array tags for this item
* @param integer number of seconds until the cache expires
* @return boolean
*/
function set($id, $data, $tags = NULL, $lifetime = NULL)
{
if (is_resource($data))
throw new Kohana_Exception('cache.resources');
// Change slashes to colons
$id = str_replace(array('/', '\\'), '=', $id);
if ( ! is_string($data))
{
// Serialize all non-string data, so that types can be preserved
$data = '<{serialized}>'.serialize($data);
}
// Make sure that tags is an array
$tags = empty($tags) ? array() : (array) $tags;
if ($lifetime === NULL)
{
// Get the default lifetime
$lifetime = $this->config['lifetime'];
}
return $this->driver->set($id, $data, $tags, $lifetime);
}
/**
* Delete a cache item by id.
*
* @param string cache id
* @return boolean
*/
public function delete($id)
{
// Change slashes to colons
$id = str_replace(array('/', '\\'), '=', $id);
return $this->driver->delete($id);
}
/**
* Delete all cache items with a given tag.
*
* @param string cache tag name
* @return boolean
*/
public function delete_tag($tag)
{
return $this->driver->delete(FALSE, $tag);
}
/**
* Delete ALL cache items items.
*
* @return boolean
*/
public function delete_all()
{
return $this->driver->delete(TRUE);
}
} // End Cache
|