Elgg  Version 1.9
ElggMemcache.php
Go to the documentation of this file.
1 <?php
13  private static $MINSERVERVERSION = '1.1.12';
14 
18  private $memcache;
19 
23  private $expires = 86400;
24 
28  private $version = 0;
29 
38  public function __construct($namespace = 'default') {
40 
41  $this->setNamespace($namespace);
42 
43  // Do we have memcache?
44  if (!class_exists('Memcache')) {
45  throw new ConfigurationException('PHP memcache module not installed, you must install php5-memcache');
46  }
47 
48  // Create memcache object
49  $this->memcache = new Memcache;
50 
51  // Now add servers
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  // don't use elgg_echo() here because most of the config hasn't been loaded yet
75  // and it caches the language, which is hard coded in $CONFIG->language as en.
76  // overriding it with real values later has no effect because it's already cached.
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  // Get version
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  // Set some defaults
99  if (isset($CONFIG->memcache_expires)) {
100  $this->expires = $CONFIG->memcache_expires;
101  }
102  }
103 
111  public function setDefaultExpiry($expires = 0) {
112  $this->expires = $expires;
113  }
114 
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 
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: SAVE FAIL $key", 'ERROR');
152  } else {
153  elgg_log("MEMCACHE: SAVE SUCCESS $key", 'INFO');
154  }
155 
156  return $result;
157  }
158 
168  public function load($key, $offset = 0, $limit = null) {
169  $key = $this->makeMemcacheKey($key);
170 
171  $result = $this->memcache->get($key);
172  if ($result === false) {
173  elgg_log("MEMCACHE: LOAD MISS $key", 'INFO');
174  } else {
175  elgg_log("MEMCACHE: LOAD HIT $key", 'INFO');
176  }
177 
178  return $result;
179  }
180 
188  public function delete($key) {
189  $key = $this->makeMemcacheKey($key);
190 
191  return $this->memcache->delete($key, 0);
192  }
193 
201  public function clear() {
202  // DISABLE clearing for now - you must use delete on a specific key.
203  return true;
204 
205  // @todo Namespaces as in #532
206  }
207 }
if(elgg_in_context('widget')) $offset
Definition: pagination.php:20
$data
Definition: opendd.php:13
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:33
$key
Definition: summary.php:34
load($key, $offset=0, $limit=null)
Retrieves data.
__construct($namespace= 'default')
Connect to memcache.
global $CONFIG
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
elgg_log($message, $level= 'NOTICE')
Display or log a message.
Definition: elgglib.php:1083