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