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: * Write value for a key into cache if it doesn't already exist
88: *
89: * @param string $key Identifier for the data
90: * @param mixed $value Data to be cached
91: * @param int $duration How long to cache for.
92: * @return bool True if the data was successfully cached, false on failure
93: */
94: public function add($key, $value, $duration) {
95: }
96:
97: /**
98: * Read a key from the cache
99: *
100: * @param string $key Identifier for the data
101: * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
102: */
103: abstract public function read($key);
104:
105: /**
106: * Increment a number under the key and return incremented value
107: *
108: * @param string $key Identifier for the data
109: * @param int $offset How much to add
110: * @return New incremented value, false otherwise
111: */
112: abstract public function increment($key, $offset = 1);
113:
114: /**
115: * Decrement a number under the key and return decremented value
116: *
117: * @param string $key Identifier for the data
118: * @param int $offset How much to subtract
119: * @return New incremented value, false otherwise
120: */
121: abstract public function decrement($key, $offset = 1);
122:
123: /**
124: * Delete a key from the cache
125: *
126: * @param string $key Identifier for the data
127: * @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
128: */
129: abstract public function delete($key);
130:
131: /**
132: * Delete all keys from the cache
133: *
134: * @param bool $check if true will check expiration, otherwise delete all
135: * @return bool True if the cache was successfully cleared, false otherwise
136: */
137: abstract public function clear($check);
138:
139: /**
140: * Clears all values belonging to a group. Is up to the implementing engine
141: * to decide whether actually delete the keys or just simulate it to achieve
142: * the same result.
143: *
144: * @param string $group name of the group to be cleared
145: * @return bool
146: */
147: public function clearGroup($group) {
148: return false;
149: }
150:
151: /**
152: * Does whatever initialization for each group is required
153: * and returns the `group value` for each of them, this is
154: * the token representing each group in the cache key
155: *
156: * @return array
157: */
158: public function groups() {
159: return $this->settings['groups'];
160: }
161:
162: /**
163: * Cache Engine settings
164: *
165: * @return array settings
166: */
167: public function settings() {
168: return $this->settings;
169: }
170:
171: /**
172: * Generates a safe key for use with cache engine storage engines.
173: *
174: * @param string $key the key passed over
175: * @return mixed string $key or false
176: */
177: public function key($key) {
178: if (empty($key)) {
179: return false;
180: }
181:
182: $prefix = '';
183: if (!empty($this->_groupPrefix)) {
184: $prefix = vsprintf($this->_groupPrefix, $this->groups());
185: }
186:
187: $key = preg_replace('/[\s]+/', '_', strtolower(trim(str_replace(array(DS, '/', '.'), '_', strval($key)))));
188: return $prefix . $key;
189: }
190: }
191: