1: <?php
2: /**
3: * Memcache storage engine for cache
4: *
5: *
6: * PHP versions 4 and 5
7: *
8: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
9: * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
10: *
11: * Licensed under The MIT License
12: * Redistributions of files must retain the above copyright notice.
13: *
14: * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
15: * @link http://cakephp.org CakePHP(tm) Project
16: * @package cake
17: * @subpackage cake.cake.libs.cache
18: * @since CakePHP(tm) v 1.2.0.4933
19: * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
20: */
21:
22: /**
23: * Memcache storage engine for cache. Memcache has some limitations in the amount of
24: * control you have over expire times far in the future. See MemcacheEngine::write() for
25: * more information.
26: *
27: * @package cake
28: * @subpackage cake.cake.libs.cache
29: */
30: class MemcacheEngine extends CacheEngine {
31:
32: /**
33: * Memcache wrapper.
34: *
35: * @var Memcache
36: * @access private
37: */
38: var $__Memcache = null;
39:
40: /**
41: * Settings
42: *
43: * - servers = string or array of memcache servers, default => 127.0.0.1. If an
44: * array MemcacheEngine will use them as a pool.
45: * - compress = boolean, default => false
46: *
47: * @var array
48: * @access public
49: */
50: var $settings = array();
51:
52: /**
53: * Initialize the Cache Engine
54: *
55: * Called automatically by the cache frontend
56: * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
57: *
58: * @param array $setting array of setting for the engine
59: * @return boolean True if the engine has been successfully initialized, false if not
60: * @access public
61: */
62: function init($settings = array()) {
63: if (!class_exists('Memcache')) {
64: return false;
65: }
66: parent::init(array_merge(array(
67: 'engine'=> 'Memcache',
68: 'prefix' => Inflector::slug(APP_DIR) . '_',
69: 'servers' => array('127.0.0.1'),
70: 'compress'=> false,
71: 'persistent' => true
72: ), $settings)
73: );
74:
75: if ($this->settings['compress']) {
76: $this->settings['compress'] = MEMCACHE_COMPRESSED;
77: }
78: if (!is_array($this->settings['servers'])) {
79: $this->settings['servers'] = array($this->settings['servers']);
80: }
81: if (!isset($this->__Memcache)) {
82: $return = false;
83: $this->__Memcache =& new Memcache();
84: foreach ($this->settings['servers'] as $server) {
85: list($host, $port) = $this->_parseServerString($server);
86: if ($this->__Memcache->addServer($host, $port, $this->settings['persistent'])) {
87: $return = true;
88: }
89: }
90: return $return;
91: }
92: return true;
93: }
94:
95: /**
96: * Parses the server address into the host/port. Handles both IPv6 and IPv4
97: * addresses and Unix sockets
98: *
99: * @param string $server The server address string.
100: * @return array Array containing host, port
101: */
102: function _parseServerString($server) {
103: if ($server[0] == 'u') {
104: return array($server, 0);
105: }
106: if (substr($server, 0, 1) == '[') {
107: $position = strpos($server, ']:');
108: if ($position !== false) {
109: $position++;
110: }
111: } else {
112: $position = strpos($server, ':');
113: }
114: $port = 11211;
115: $host = $server;
116: if ($position !== false) {
117: $host = substr($server, 0, $position);
118: $port = substr($server, $position + 1);
119: }
120: return array($host, $port);
121: }
122:
123: /**
124: * Write data for key into cache. When using memcache as your cache engine
125: * remember that the Memcache pecl extension does not support cache expiry times greater
126: * than 30 days in the future. Any duration greater than 30 days will be treated as never expiring.
127: *
128: * @param string $key Identifier for the data
129: * @param mixed $value Data to be cached
130: * @param integer $duration How long to cache the data, in seconds
131: * @return boolean True if the data was succesfully cached, false on failure
132: * @see http://php.net/manual/en/memcache.set.php
133: * @access public
134: */
135: function write($key, &$value, $duration) {
136: if ($duration > 30 * DAY) {
137: $duration = 0;
138: }
139: return $this->__Memcache->set($key, $value, $this->settings['compress'], $duration);
140: }
141:
142: /**
143: * Read a key from the cache
144: *
145: * @param string $key Identifier for the data
146: * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
147: * @access public
148: */
149: function read($key) {
150: return $this->__Memcache->get($key);
151: }
152:
153: /**
154: * Increments the value of an integer cached key
155: *
156: * @param string $key Identifier for the data
157: * @param integer $offset How much to increment
158: * @param integer $duration How long to cache the data, in seconds
159: * @return New incremented value, false otherwise
160: * @access public
161: */
162: function increment($key, $offset = 1) {
163: if ($this->settings['compress']) {
164: trigger_error(sprintf(__('Method increment() not implemented for compressed cache in %s', true), get_class($this)), E_USER_ERROR);
165: }
166: return $this->__Memcache->increment($key, $offset);
167: }
168:
169: /**
170: * Decrements the value of an integer cached key
171: *
172: * @param string $key Identifier for the data
173: * @param integer $offset How much to substract
174: * @param integer $duration How long to cache the data, in seconds
175: * @return New decremented value, false otherwise
176: * @access public
177: */
178: function decrement($key, $offset = 1) {
179: if ($this->settings['compress']) {
180: trigger_error(sprintf(__('Method decrement() not implemented for compressed cache in %s', true), get_class($this)), E_USER_ERROR);
181: }
182: return $this->__Memcache->decrement($key, $offset);
183: }
184:
185: /**
186: * Delete a key from the cache
187: *
188: * @param string $key Identifier for the data
189: * @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed
190: * @access public
191: */
192: function delete($key) {
193: return $this->__Memcache->delete($key);
194: }
195:
196: /**
197: * Delete all keys from the cache
198: *
199: * @return boolean True if the cache was succesfully cleared, false otherwise
200: * @access public
201: */
202: function clear() {
203: return $this->__Memcache->flush();
204: }
205:
206: /**
207: * Connects to a server in connection pool
208: *
209: * @param string $host host ip address or name
210: * @param integer $port Server port
211: * @return boolean True if memcache server was connected
212: * @access public
213: */
214: function connect($host, $port = 11211) {
215: if ($this->__Memcache->getServerStatus($host, $port) === 0) {
216: if ($this->__Memcache->connect($host, $port)) {
217: return true;
218: }
219: return false;
220: }
221: return true;
222: }
223: }
224: