| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* SVN FILE: $Id: memcache.php 7945 2008-12-19 02:16:01Z gwoo $ */
|
|
|
3 |
/**
|
|
|
4 |
* Memcache storage engine for cache
|
|
|
5 |
*
|
|
|
6 |
*
|
|
|
7 |
* PHP versions 4 and 5
|
|
|
8 |
*
|
|
|
9 |
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
|
|
|
10 |
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
11 |
*
|
|
|
12 |
* Licensed under The MIT License
|
|
|
13 |
* Redistributions of files must retain the above copyright notice.
|
|
|
14 |
*
|
|
|
15 |
* @filesource
|
|
|
16 |
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
17 |
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
18 |
* @package cake
|
|
|
19 |
* @subpackage cake.cake.libs.cache
|
|
|
20 |
* @since CakePHP(tm) v 1.2.0.4933
|
|
|
21 |
* @version $Revision: 7945 $
|
|
|
22 |
* @modifiedby $LastChangedBy: gwoo $
|
|
|
23 |
* @lastmodified $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
|
|
|
24 |
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
25 |
*/
|
|
|
26 |
/**
|
|
|
27 |
* Memcache storage engine for cache
|
|
|
28 |
*
|
|
|
29 |
* @package cake
|
|
|
30 |
* @subpackage cake.cake.libs.cache
|
|
|
31 |
*/
|
|
|
32 |
class MemcacheEngine extends CacheEngine {
|
|
|
33 |
/**
|
|
|
34 |
* Memcache wrapper.
|
|
|
35 |
*
|
|
|
36 |
* @var object
|
|
|
37 |
* @access private
|
|
|
38 |
*/
|
|
|
39 |
var $__Memcache = null;
|
|
|
40 |
/**
|
|
|
41 |
* settings
|
|
|
42 |
* servers = string or array of memcache servers, default => 127.0.0.1
|
|
|
43 |
* compress = boolean, default => false
|
|
|
44 |
*
|
|
|
45 |
* @var array
|
|
|
46 |
* @access public
|
|
|
47 |
*/
|
|
|
48 |
var $settings = array();
|
|
|
49 |
/**
|
|
|
50 |
* Initialize the Cache Engine
|
|
|
51 |
*
|
|
|
52 |
* Called automatically by the cache frontend
|
|
|
53 |
* To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
|
|
|
54 |
*
|
|
|
55 |
* @param array $setting array of setting for the engine
|
|
|
56 |
* @return boolean True if the engine has been successfully initialized, false if not
|
|
|
57 |
* @access public
|
|
|
58 |
*/
|
|
|
59 |
function init($settings = array()) {
|
|
|
60 |
if (!class_exists('Memcache')) {
|
|
|
61 |
return false;
|
|
|
62 |
}
|
|
|
63 |
parent::init(array_merge(array(
|
|
|
64 |
'engine'=> 'Memcache', 'prefix' => Inflector::slug(APP_DIR) . '_', 'servers' => array('127.0.0.1'), 'compress'=> false
|
|
|
65 |
), $settings)
|
|
|
66 |
);
|
|
|
67 |
|
|
|
68 |
if ($this->settings['compress']) {
|
|
|
69 |
$this->settings['compress'] = MEMCACHE_COMPRESSED;
|
|
|
70 |
}
|
|
|
71 |
if (!is_array($this->settings['servers'])) {
|
|
|
72 |
$this->settings['servers'] = array($this->settings['servers']);
|
|
|
73 |
}
|
|
|
74 |
if (!isset($this->__Memcache)) {
|
|
|
75 |
$return = false;
|
|
|
76 |
$this->__Memcache =& new Memcache();
|
|
|
77 |
foreach ($this->settings['servers'] as $server) {
|
|
|
78 |
$parts = explode(':', $server);
|
|
|
79 |
$host = $parts[0];
|
|
|
80 |
$port = 11211;
|
|
|
81 |
if (isset($parts[1])) {
|
|
|
82 |
$port = $parts[1];
|
|
|
83 |
}
|
|
|
84 |
if ($this->__Memcache->addServer($host, $port)) {
|
|
|
85 |
$return = true;
|
|
|
86 |
}
|
|
|
87 |
}
|
|
|
88 |
return $return;
|
|
|
89 |
}
|
|
|
90 |
return true;
|
|
|
91 |
}
|
|
|
92 |
/**
|
|
|
93 |
* Write data for key into cache
|
|
|
94 |
*
|
|
|
95 |
* @param string $key Identifier for the data
|
|
|
96 |
* @param mixed $value Data to be cached
|
|
|
97 |
* @param integer $duration How long to cache the data, in seconds
|
|
|
98 |
* @return boolean True if the data was succesfully cached, false on failure
|
|
|
99 |
* @access public
|
|
|
100 |
*/
|
|
|
101 |
function write($key, &$value, $duration) {
|
|
|
102 |
$expires = time() + $duration;
|
|
|
103 |
$this->__Memcache->set($key.'_expires', $expires, $this->settings['compress'], $duration);
|
|
|
104 |
return $this->__Memcache->set($key, $value, $this->settings['compress'], $duration);
|
|
|
105 |
}
|
|
|
106 |
/**
|
|
|
107 |
* Read a key from the cache
|
|
|
108 |
*
|
|
|
109 |
* @param string $key Identifier for the data
|
|
|
110 |
* @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
|
|
|
111 |
* @access public
|
|
|
112 |
*/
|
|
|
113 |
function read($key) {
|
|
|
114 |
$time = time();
|
|
|
115 |
$cachetime = intval($this->__Memcache->get($key.'_expires'));
|
|
|
116 |
if ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime) {
|
|
|
117 |
return false;
|
|
|
118 |
}
|
|
|
119 |
return $this->__Memcache->get($key);
|
|
|
120 |
}
|
|
|
121 |
/**
|
|
|
122 |
* Delete a key from the cache
|
|
|
123 |
*
|
|
|
124 |
* @param string $key Identifier for the data
|
|
|
125 |
* @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed
|
|
|
126 |
* @access public
|
|
|
127 |
*/
|
|
|
128 |
function delete($key) {
|
|
|
129 |
return $this->__Memcache->delete($key);
|
|
|
130 |
}
|
|
|
131 |
/**
|
|
|
132 |
* Delete all keys from the cache
|
|
|
133 |
*
|
|
|
134 |
* @return boolean True if the cache was succesfully cleared, false otherwise
|
|
|
135 |
* @access public
|
|
|
136 |
*/
|
|
|
137 |
function clear() {
|
|
|
138 |
return $this->__Memcache->flush();
|
|
|
139 |
}
|
|
|
140 |
/**
|
|
|
141 |
* Connects to a server in connection pool
|
|
|
142 |
*
|
|
|
143 |
* @param string $host host ip address or name
|
|
|
144 |
* @param integer $port Server port
|
|
|
145 |
* @return boolean True if memcache server was connected
|
|
|
146 |
* @access public
|
|
|
147 |
*/
|
|
|
148 |
function connect($host, $port = 11211) {
|
|
|
149 |
if ($this->__Memcache->getServerStatus($host, $port) === 0) {
|
|
|
150 |
if ($this->__Memcache->connect($host, $port)) {
|
|
|
151 |
return true;
|
|
|
152 |
}
|
|
|
153 |
return false;
|
|
|
154 |
}
|
|
|
155 |
return true;
|
|
|
156 |
}
|
|
|
157 |
}
|
|
|
158 |
?>
|