pictcode / lib / Cake / Cache / CacheEngine.php @ master
履歴 | 表示 | アノテート | ダウンロード (4.678 KB)
1 |
<?php
|
---|---|
2 |
/**
|
3 |
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
4 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
5 |
*
|
6 |
* Licensed under The MIT License
|
7 |
* For full copyright and license information, please see the LICENSE.txt
|
8 |
* Redistributions of files must retain the above copyright notice.
|
9 |
*
|
10 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
11 |
* @link http://cakephp.org CakePHP(tm) Project
|
12 |
* @package Cake.Cache
|
13 |
* @since CakePHP(tm) v 1.2.0.4933
|
14 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
15 |
*/
|
16 |
|
17 |
/**
|
18 |
* Storage engine for CakePHP caching
|
19 |
*
|
20 |
* @package Cake.Cache
|
21 |
*/
|
22 |
abstract class CacheEngine { |
23 |
|
24 |
/**
|
25 |
* Settings of current engine instance
|
26 |
*
|
27 |
* @var array
|
28 |
*/
|
29 |
public $settings = array(); |
30 |
|
31 |
/**
|
32 |
* Contains the compiled string with all groups
|
33 |
* prefixes to be prepended to every key in this cache engine
|
34 |
*
|
35 |
* @var string
|
36 |
*/
|
37 |
protected $_groupPrefix = null; |
38 |
|
39 |
/**
|
40 |
* Initialize the cache engine
|
41 |
*
|
42 |
* Called automatically by the cache frontend
|
43 |
*
|
44 |
* @param array $settings Associative array of parameters for the engine
|
45 |
* @return bool True if the engine has been successfully initialized, false if not
|
46 |
*/
|
47 |
public function init($settings = array()) { |
48 |
$settings += $this->settings + array( |
49 |
'prefix' => 'cake_', |
50 |
'duration' => 3600, |
51 |
'probability' => 100, |
52 |
'groups' => array() |
53 |
); |
54 |
$this->settings = $settings; |
55 |
if (!empty($this->settings['groups'])) { |
56 |
sort($this->settings['groups']); |
57 |
$this->_groupPrefix = str_repeat('%s_', count($this->settings['groups'])); |
58 |
} |
59 |
if (!is_numeric($this->settings['duration'])) { |
60 |
$this->settings['duration'] = strtotime($this->settings['duration']) - time(); |
61 |
} |
62 |
return true; |
63 |
} |
64 |
|
65 |
/**
|
66 |
* Garbage collection
|
67 |
*
|
68 |
* Permanently remove all expired and deleted data
|
69 |
*
|
70 |
* @param int $expires [optional] An expires timestamp, invalidating all data before.
|
71 |
* @return void
|
72 |
*/
|
73 |
public function gc($expires = null) { |
74 |
} |
75 |
|
76 |
/**
|
77 |
* Write value for a key into cache
|
78 |
*
|
79 |
* @param string $key Identifier for the data
|
80 |
* @param mixed $value Data to be cached
|
81 |
* @param int $duration How long to cache for.
|
82 |
* @return bool True if the data was successfully cached, false on failure
|
83 |
*/
|
84 |
abstract public function write($key, $value, $duration); |
85 |
|
86 |
/**
|
87 |
* Read a key from the cache
|
88 |
*
|
89 |
* @param string $key Identifier for the data
|
90 |
* @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
|
91 |
*/
|
92 |
abstract public function read($key); |
93 |
|
94 |
/**
|
95 |
* Increment a number under the key and return incremented value
|
96 |
*
|
97 |
* @param string $key Identifier for the data
|
98 |
* @param int $offset How much to add
|
99 |
* @return New incremented value, false otherwise
|
100 |
*/
|
101 |
abstract public function increment($key, $offset = 1); |
102 |
|
103 |
/**
|
104 |
* Decrement a number under the key and return decremented value
|
105 |
*
|
106 |
* @param string $key Identifier for the data
|
107 |
* @param int $offset How much to subtract
|
108 |
* @return New incremented value, false otherwise
|
109 |
*/
|
110 |
abstract public function decrement($key, $offset = 1); |
111 |
|
112 |
/**
|
113 |
* Delete a key from the cache
|
114 |
*
|
115 |
* @param string $key Identifier for the data
|
116 |
* @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
|
117 |
*/
|
118 |
abstract public function delete($key); |
119 |
|
120 |
/**
|
121 |
* Delete all keys from the cache
|
122 |
*
|
123 |
* @param bool $check if true will check expiration, otherwise delete all
|
124 |
* @return bool True if the cache was successfully cleared, false otherwise
|
125 |
*/
|
126 |
abstract public function clear($check); |
127 |
|
128 |
/**
|
129 |
* Clears all values belonging to a group. Is up to the implementing engine
|
130 |
* to decide whether actually delete the keys or just simulate it to achieve
|
131 |
* the same result.
|
132 |
*
|
133 |
* @param string $group name of the group to be cleared
|
134 |
* @return bool
|
135 |
*/
|
136 |
public function clearGroup($group) { |
137 |
return false; |
138 |
} |
139 |
|
140 |
/**
|
141 |
* Does whatever initialization for each group is required
|
142 |
* and returns the `group value` for each of them, this is
|
143 |
* the token representing each group in the cache key
|
144 |
*
|
145 |
* @return array
|
146 |
*/
|
147 |
public function groups() { |
148 |
return $this->settings['groups']; |
149 |
} |
150 |
|
151 |
/**
|
152 |
* Cache Engine settings
|
153 |
*
|
154 |
* @return array settings
|
155 |
*/
|
156 |
public function settings() { |
157 |
return $this->settings; |
158 |
} |
159 |
|
160 |
/**
|
161 |
* Generates a safe key for use with cache engine storage engines.
|
162 |
*
|
163 |
* @param string $key the key passed over
|
164 |
* @return mixed string $key or false
|
165 |
*/
|
166 |
public function key($key) { |
167 |
if (empty($key)) { |
168 |
return false; |
169 |
} |
170 |
|
171 |
$prefix = ''; |
172 |
if (!empty($this->_groupPrefix)) { |
173 |
$prefix = vsprintf($this->_groupPrefix, $this->groups()); |
174 |
} |
175 |
|
176 |
$key = preg_replace('/[\s]+/', '_', strtolower(trim(str_replace(array(DS, '/', '.'), '_', strval($key))))); |
177 |
return $prefix . $key; |
178 |
} |
179 |
|
180 |
} |