Linux multi-monitor fullscreen support
[ryzomcore.git] / web / public_php / webtt / cake / libs / cache / memcache.php
bloba1bcc5c55eaa1a84cc73d70a1b9ff7f0cb72c51c
1 <?php
2 /**
3 * Memcache storage engine for cache
6 * PHP versions 4 and 5
8 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
9 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
11 * Licensed under The MIT License
12 * Redistributions of files must retain the above copyright notice.
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)
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.
27 * @package cake
28 * @subpackage cake.cake.libs.cache
30 class MemcacheEngine extends CacheEngine {
32 /**
33 * Memcache wrapper.
35 * @var Memcache
36 * @access private
38 var $__Memcache = null;
40 /**
41 * Settings
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
47 * @var array
48 * @access public
50 var $settings = array();
52 /**
53 * Initialize the Cache Engine
55 * Called automatically by the cache frontend
56 * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
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
62 function init($settings = array()) {
63 if (!class_exists('Memcache')) {
64 return false;
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)
75 if ($this->settings['compress']) {
76 $this->settings['compress'] = MEMCACHE_COMPRESSED;
78 if (!is_array($this->settings['servers'])) {
79 $this->settings['servers'] = array($this->settings['servers']);
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;
90 return $return;
92 return true;
95 /**
96 * Parses the server address into the host/port. Handles both IPv6 and IPv4
97 * addresses
99 * @param string $server The server address string.
100 * @return array Array containing host, port
102 function _parseServerString($server) {
103 if (substr($server, 0, 1) == '[') {
104 $position = strpos($server, ']:');
105 if ($position !== false) {
106 $position++;
108 } else {
109 $position = strpos($server, ':');
111 $port = 11211;
112 $host = $server;
113 if ($position !== false) {
114 $host = substr($server, 0, $position);
115 $port = substr($server, $position + 1);
117 return array($host, $port);
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.
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
132 function write($key, &$value, $duration) {
133 if ($duration > 30 * DAY) {
134 $duration = 0;
136 return $this->__Memcache->set($key, $value, $this->settings['compress'], $duration);
140 * Read a key from the cache
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
146 function read($key) {
147 return $this->__Memcache->get($key);
151 * Increments the value of an integer cached key
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
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);
163 return $this->__Memcache->increment($key, $offset);
167 * Decrements the value of an integer cached key
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
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);
179 return $this->__Memcache->decrement($key, $offset);
183 * Delete a key from the cache
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
189 function delete($key) {
190 return $this->__Memcache->delete($key);
194 * Delete all keys from the cache
196 * @return boolean True if the cache was succesfully cleared, false otherwise
197 * @access public
199 function clear() {
200 return $this->__Memcache->flush();
204 * Connects to a server in connection pool
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
211 function connect($host, $port = 11211) {
212 if ($this->__Memcache->getServerStatus($host, $port) === 0) {
213 if ($this->__Memcache->connect($host, $port)) {
214 return true;
216 return false;
218 return true;