Elgg  Version 1.11
ElggMemcache.php
Go to the documentation of this file.
1 <?php
14  private $CONFIG;
15 
20  private static $MINSERVERVERSION = '1.1.12';
21 
25  private $memcache;
26 
30  private $expires = 86400;
31 
35  private $version = 0;
36 
45  public function __construct($namespace = 'default') {
46  global $CONFIG;
47  $this->CONFIG = $CONFIG;
48 
49  $this->setNamespace($namespace);
50 
51  // Do we have memcache?
52  if (!class_exists('Memcache')) {
53  throw new \ConfigurationException('PHP memcache module not installed, you must install php5-memcache');
54  }
55 
56  // Create memcache object
57  $this->memcache = new Memcache;
58 
59  // Now add servers
60  if (!$this->CONFIG->memcache_servers) {
61  throw new \ConfigurationException('No memcache servers defined, please populate the $this->CONFIG->memcache_servers variable');
62  }
63 
64  if (is_callable(array($this->memcache, 'addServer'))) {
65  foreach ($this->CONFIG->memcache_servers as $server) {
66  if (is_array($server)) {
67  $this->memcache->addServer(
68  $server[0],
69  isset($server[1]) ? $server[1] : 11211,
70  isset($server[2]) ? $server[2] : false,
71  isset($server[3]) ? $server[3] : 1,
72  isset($server[4]) ? $server[4] : 1,
73  isset($server[5]) ? $server[5] : 15,
74  isset($server[6]) ? $server[6] : true
75  );
76 
77  } else {
78  $this->memcache->addServer($server, 11211);
79  }
80  }
81  } else {
82  // don't use _elgg_services()->translator->translate() here because most of the config hasn't been loaded yet
83  // and it caches the language, which is hard coded in $this->CONFIG->language as en.
84  // overriding it with real values later has no effect because it's already cached.
85  _elgg_services()->logger->error("This version of the PHP memcache API doesn't support multiple servers.");
86 
87  $server = $this->CONFIG->memcache_servers[0];
88  if (is_array($server)) {
89  $this->memcache->connect($server[0], $server[1]);
90  } else {
91  $this->memcache->addServer($server, 11211);
92  }
93  }
94 
95  // Get version
96  $this->version = $this->memcache->getVersion();
97  if (version_compare($this->version, \ElggMemcache::$MINSERVERVERSION, '<')) {
98  $msg = vsprintf('Memcache needs at least version %s to run, you are running %s',
99  array(\ElggMemcache::$MINSERVERVERSION,
100  $this->version
101  ));
102 
103  throw new \ConfigurationException($msg);
104  }
105 
106  // Set some defaults
107  if (isset($this->CONFIG->memcache_expires)) {
108  $this->expires = $this->CONFIG->memcache_expires;
109  }
110  }
111 
119  public function setDefaultExpiry($expires = 0) {
120  $this->expires = $expires;
121  }
122 
131  private function makeMemcacheKey($key) {
132  $prefix = $this->getNamespace() . ":";
133 
134  if (strlen($prefix . $key) > 250) {
135  $key = md5($key);
136  }
137 
138  return $prefix . $key;
139  }
140 
150  public function save($key, $data, $expires = null) {
151  $key = $this->makeMemcacheKey($key);
152 
153  if ($expires === null) {
154  $expires = $this->expires;
155  }
156 
157  $result = $this->memcache->set($key, $data, null, $expires);
158  if ($result === false) {
159  _elgg_services()->logger->error("MEMCACHE: SAVE FAIL $key");
160  } else {
161  _elgg_services()->logger->info("MEMCACHE: SAVE SUCCESS $key");
162  }
163 
164  return $result;
165  }
166 
176  public function load($key, $offset = 0, $limit = null) {
177  $key = $this->makeMemcacheKey($key);
178 
179  $result = $this->memcache->get($key);
180  if ($result === false) {
181  _elgg_services()->logger->info("MEMCACHE: LOAD MISS $key");
182  } else {
183  _elgg_services()->logger->info("MEMCACHE: LOAD HIT $key");
184  }
185 
186  return $result;
187  }
188 
196  public function delete($key) {
197  $key = $this->makeMemcacheKey($key);
198 
199  return $this->memcache->delete($key, 0);
200  }
201 
209  public function clear() {
210  // DISABLE clearing for now - you must use delete on a specific key.
211  return true;
212 
213  // @todo Namespaces as in #532
214  }
215 }
$data
Definition: opendd.php:13
if(!$count) $offset
Definition: pagination.php:25
clear()
Clears the entire cache?
getNamespace()
Get the namespace currently defined.
string version
Definition: conf.py:51
setDefaultExpiry($expires=0)
Set the default expiry.
$limit
Definition: userpicker.php:31
$key
Definition: summary.php:34
load($key, $offset=0, $limit=null)
Retrieves data.
_elgg_services()
Definition: autoloader.php:14
__construct($namespace= 'default')
Connect to memcache.
setNamespace($namespace="default")
Set the namespace of this cache.
save($key, $data, $expires=null)
Saves a name and value to the cache.
elgg global
Pointer to the global context.
Definition: elgglib.js:12