1: <?php
2: /**
3: * Cache engine For XOOPS
4: *
5: * You may not change or alter any portion of this comment or credits
6: * of supporting developers from this source code or any supporting source code
7: * which is considered copyrighted (c) material of the original comment or credit authors.
8: * This program is distributed in the hope that it will be useful,
9: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11: *
12: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
13: * @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14: * @package class
15: * @subpackage cache
16: * @since 2.3.0
17: * @author Taiwen Jiang <phppp@users.sourceforge.net>
18: */
19: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
20:
21: /**
22: * Memcache storage engine for cache
23: *
24: *
25: * PHP versions 4 and 5
26: *
27: * CakePHP(tm) : Rapid Development Framework <https://www.cakephp.org/>
28: * Copyright 2005-2008, Cake Software Foundation, Inc.
29: * 1785 E. Sahara Avenue, Suite 490-204
30: * Las Vegas, Nevada 89104
31: *
32: * Licensed under The MIT License
33: * Redistributions of files must retain the above copyright notice.
34: *
35: * @filesource
36: * @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
37: * @link https://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
38: * @package cake
39: * @subpackage cake.cake.libs.cache
40: * @since CakePHP(tm) v 1.2.0.4933
41: * @modifiedby $LastChangedBy$
42: * @lastmodified $Date$
43: * @license https://www.opensource.org/licenses/mit-license.php The MIT License
44: */
45:
46: /**
47: * Memcache storage engine for cache
48: *
49: * @package cake
50: * @subpackage cake.cake.libs.cache
51: */
52: class XoopsCacheMemcache extends XoopsCacheEngine
53: {
54: /**
55: * Memcache wrapper.
56: *
57: * @var object
58: * @access private
59: */
60: private $memcache;
61:
62: /**
63: * settings
64: * servers = string or array of memcache servers, default => 127.0.0.1
65: * compress = boolean, default => false
66: *
67: * @var array
68: * @access public
69: */
70: public $settings = array();
71:
72: /**
73: * Initialize the Cache Engine
74: *
75: * Called automatically by the cache frontend
76: * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
77: *
78: * @param array $settings array of setting for the engine
79: *
80: * @return boolean True if the engine has been successfully initialized, false if not
81: * @access public
82: */
83: public function init($settings = array())
84: {
85: if (!class_exists('Memcache')) {
86: return false;
87: }
88: parent::init($settings);
89: $defaults = array(
90: 'servers' => array(
91: '127.0.0.1'),
92: 'compress' => false);
93: $this->settings = array_merge($defaults, $this->settings);
94:
95: if (!$this->settings['compress']) {
96: $this->settings['compress'] = MEMCACHE_COMPRESSED;
97: }
98: if (!is_array($this->settings['servers'])) {
99: $this->settings['servers'] = array($this->settings['servers']);
100: }
101: $this->memcache = null;
102: $this->memcache = new Memcache();
103: foreach ($this->settings['servers'] as $server) {
104: $parts = explode(':', $server);
105: $host = $parts[0];
106: $port = 11211;
107: if (isset($parts[1])) {
108: $port = $parts[1];
109: }
110: if ($this->memcache->addServer($host, $port)) {
111: return true;
112: }
113: }
114:
115: return false;
116: }
117:
118: /**
119: * Write data for key into cache
120: *
121: * @param string $key Identifier for the data
122: * @param mixed $value Data to be cached
123: * @param integer $duration How long to cache the data, in seconds
124: * @return boolean True if the data was successfully cached, false on failure
125: * @access public
126: */
127: public function write($key, $value, $duration = null)
128: {
129: return $this->memcache->set($key, $value, $this->settings['compress'], $duration);
130: }
131:
132: /**
133: * Read a key from the cache
134: *
135: * @param string $key Identifier for the data
136: * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
137: * @access public
138: */
139: public function read($key)
140: {
141: return $this->memcache->get($key);
142: }
143:
144: /**
145: * Delete a key from the cache
146: *
147: * @param string $key Identifier for the data
148: * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
149: * @access public
150: */
151: public function delete($key)
152: {
153: return $this->memcache->delete($key);
154: }
155:
156: /**
157: * Delete all keys from the cache
158: *
159: * @return boolean True if the cache was successfully cleared, false otherwise
160: * @access public
161: */
162: public function clear($check = null)
163: {
164: return $this->memcache->flush();
165: }
166:
167: /**
168: * Connects to a server in connection pool
169: *
170: * @param string $host host ip address or name
171: * @param integer $port Server port
172: * @return boolean True if memcache server was connected
173: * @access public
174: */
175: public function connect($host, $port = 11211)
176: {
177: if ($this->memcache->getServerStatus($host, $port) === 0) {
178: if ($this->memcache->connect($host, $port)) {
179: return true;
180: }
181:
182: return false;
183: }
184:
185: return true;
186: }
187: }
188: