1: <?php
2: 3: 4: 5: 6: 7:
8: class ElggMemcache extends ElggSharedMemoryCache {
9: 10: 11: 12:
13: private static $MINSERVERVERSION = '1.1.12';
14:
15: 16: 17:
18: private $memcache;
19:
20: 21: 22:
23: private $expires = 86400;
24:
25: 26: 27:
28: private $version = 0;
29:
30: 31: 32: 33: 34: 35: 36: 37:
38: function __construct($namespace = 'default') {
39: global $CONFIG;
40:
41: $this->setNamespace($namespace);
42:
43:
44: if (!class_exists('Memcache')) {
45: throw new ConfigurationException('PHP memcache module not installed, you must install php5-memcache');
46: }
47:
48:
49: $this->memcache = new Memcache;
50:
51:
52: if (!$CONFIG->memcache_servers) {
53: throw new ConfigurationException('No memcache servers defined, please populate the $CONFIG->memcache_servers variable');
54: }
55:
56: if (is_callable(array($this->memcache, 'addServer'))) {
57: foreach ($CONFIG->memcache_servers as $server) {
58: if (is_array($server)) {
59: $this->memcache->addServer(
60: $server[0],
61: isset($server[1]) ? $server[1] : 11211,
62: isset($server[2]) ? $server[2] : FALSE,
63: isset($server[3]) ? $server[3] : 1,
64: isset($server[4]) ? $server[4] : 1,
65: isset($server[5]) ? $server[5] : 15,
66: isset($server[6]) ? $server[6] : TRUE
67: );
68:
69: } else {
70: $this->memcache->addServer($server, 11211);
71: }
72: }
73: } else {
74:
75:
76:
77: elgg_log("This version of the PHP memcache API doesn't support multiple servers.", 'ERROR');
78:
79: $server = $CONFIG->memcache_servers[0];
80: if (is_array($server)) {
81: $this->memcache->connect($server[0], $server[1]);
82: } else {
83: $this->memcache->addServer($server, 11211);
84: }
85: }
86:
87:
88: $this->version = $this->memcache->getVersion();
89: if (version_compare($this->version, ElggMemcache::$MINSERVERVERSION, '<')) {
90: $msg = vsprintf('Memcache needs at least version %s to run, you are running %s',
91: array(ElggMemcache::$MINSERVERVERSION,
92: $this->version
93: ));
94:
95: throw new ConfigurationException($msg);
96: }
97:
98:
99: if (isset($CONFIG->memcache_expires)) {
100: $this->expires = $CONFIG->memcache_expires;
101: }
102: }
103:
104: 105: 106: 107: 108: 109: 110:
111: public function setDefaultExpiry($expires = 0) {
112: $this->expires = $expires;
113: }
114:
115: 116: 117: 118: 119: 120: 121: 122:
123: private function makeMemcacheKey($key) {
124: $prefix = $this->getNamespace() . ":";
125:
126: if (strlen($prefix . $key) > 250) {
127: $key = md5($key);
128: }
129:
130: return $prefix . $key;
131: }
132:
133: 134: 135: 136: 137: 138: 139: 140: 141:
142: public function save($key, $data, $expires = null) {
143: $key = $this->makeMemcacheKey($key);
144:
145: if ($expires === null) {
146: $expires = $this->expires;
147: }
148:
149: $result = $this->memcache->set($key, $data, null, $expires);
150: if ($result === false) {
151: elgg_log("MEMCACHE: FAILED TO SAVE $key", 'ERROR');
152: }
153:
154: return $result;
155: }
156:
157: 158: 159: 160: 161: 162: 163: 164: 165:
166: public function load($key, $offset = 0, $limit = null) {
167: $key = $this->makeMemcacheKey($key);
168:
169: $result = $this->memcache->get($key);
170: if ($result === false) {
171: elgg_log("MEMCACHE: FAILED TO LOAD $key", 'ERROR');
172: }
173:
174: return $result;
175: }
176:
177: 178: 179: 180: 181: 182: 183:
184: public function delete($key) {
185: $key = $this->makeMemcacheKey($key);
186:
187: return $this->memcache->delete($key, 0);
188: }
189:
190: 191: 192: 193: 194: 195: 196:
197: public function clear() {
198:
199: return true;
200:
201:
202: }
203: }
204: