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