comparison cake/libs/cache/memcache.php @ 0:261e66bd5a0c

hg init
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Sun, 24 Jul 2011 21:08:31 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:261e66bd5a0c
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-2010, 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-2010, 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
98 *
99 * @param string $server The server address string.
100 * @return array Array containing host, port
101 */
102 function _parseServerString($server) {
103 if (substr($server, 0, 1) == '[') {
104 $position = strpos($server, ']:');
105 if ($position !== false) {
106 $position++;
107 }
108 } else {
109 $position = strpos($server, ':');
110 }
111 $port = 11211;
112 $host = $server;
113 if ($position !== false) {
114 $host = substr($server, 0, $position);
115 $port = substr($server, $position + 1);
116 }
117 return array($host, $port);
118 }
119
120 /**
121 * Write data for key into cache. When using memcache as your cache engine
122 * remember that the Memcache pecl extension does not support cache expiry times greater
123 * than 30 days in the future. Any duration greater than 30 days will be treated as never expiring.
124 *
125 * @param string $key Identifier for the data
126 * @param mixed $value Data to be cached
127 * @param integer $duration How long to cache the data, in seconds
128 * @return boolean True if the data was succesfully cached, false on failure
129 * @see http://php.net/manual/en/memcache.set.php
130 * @access public
131 */
132 function write($key, &$value, $duration) {
133 if ($duration > 30 * DAY) {
134 $duration = 0;
135 }
136 return $this->__Memcache->set($key, $value, $this->settings['compress'], $duration);
137 }
138
139 /**
140 * Read a key from the cache
141 *
142 * @param string $key Identifier for the data
143 * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
144 * @access public
145 */
146 function read($key) {
147 return $this->__Memcache->get($key);
148 }
149
150 /**
151 * Increments the value of an integer cached key
152 *
153 * @param string $key Identifier for the data
154 * @param integer $offset How much to increment
155 * @param integer $duration How long to cache the data, in seconds
156 * @return New incremented value, false otherwise
157 * @access public
158 */
159 function increment($key, $offset = 1) {
160 if ($this->settings['compress']) {
161 trigger_error(sprintf(__('Method increment() not implemented for compressed cache in %s', true), get_class($this)), E_USER_ERROR);
162 }
163 return $this->__Memcache->increment($key, $offset);
164 }
165
166 /**
167 * Decrements the value of an integer cached key
168 *
169 * @param string $key Identifier for the data
170 * @param integer $offset How much to substract
171 * @param integer $duration How long to cache the data, in seconds
172 * @return New decremented value, false otherwise
173 * @access public
174 */
175 function decrement($key, $offset = 1) {
176 if ($this->settings['compress']) {
177 trigger_error(sprintf(__('Method decrement() not implemented for compressed cache in %s', true), get_class($this)), E_USER_ERROR);
178 }
179 return $this->__Memcache->decrement($key, $offset);
180 }
181
182 /**
183 * Delete a key from the cache
184 *
185 * @param string $key Identifier for the data
186 * @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed
187 * @access public
188 */
189 function delete($key) {
190 return $this->__Memcache->delete($key);
191 }
192
193 /**
194 * Delete all keys from the cache
195 *
196 * @return boolean True if the cache was succesfully cleared, false otherwise
197 * @access public
198 */
199 function clear() {
200 return $this->__Memcache->flush();
201 }
202
203 /**
204 * Connects to a server in connection pool
205 *
206 * @param string $host host ip address or name
207 * @param integer $port Server port
208 * @return boolean True if memcache server was connected
209 * @access public
210 */
211 function connect($host, $port = 11211) {
212 if ($this->__Memcache->getServerStatus($host, $port) === 0) {
213 if ($this->__Memcache->connect($host, $port)) {
214 return true;
215 }
216 return false;
217 }
218 return true;
219 }
220 }