Elgg  Version 2.3
ElggMemcache.php
Go to the documentation of this file.
1 <?php
2 
7 
11  private $ttl = 86400;
12 
16  private $stash_pool;
17 
29  public function __construct($namespace = 'default', \Stash\Pool $pool = null, $ttl = null) {
30  parent::__construct();
31 
32  $this->setNamespace($namespace);
33 
34  if (!$pool) {
35  $pool = _elgg_services()->memcacheStashPool;
36  if (!$pool) {
37  throw new \ConfigurationException('No memcache servers defined, please populate the $this->CONFIG->memcache_servers variable');
38  }
39  }
40  $this->stash_pool = $pool;
41 
42  if ($ttl === null) {
43  $ttl = _elgg_services()->config->get('memcache_expires');
44  }
45  if (isset($ttl)) {
46  $this->ttl = $ttl;
47  }
48 
49  // make sure memcache is reset
50  _elgg_services()->events->registerHandler('cache:flush', 'system', array($this, 'clear'));
51  }
52 
60  public function setDefaultExpiry($ttl = 0) {
61  $this->ttl = $ttl;
62  }
63 
72  private function makeMemcacheKey($key) {
73  // using stacks grouping http://www.stashphp.com/Grouping.html#stacks
74  // this will allowing clearing the namespace later
75  return "/{$this->getNamespace()}/$key";
76  }
77 
87  public function save($key, $data, $ttl = null) {
88  $key = $this->makeMemcacheKey($key);
89 
90  if ($ttl === null) {
91  $ttl = $this->ttl;
92  }
93 
94  $item = $this->stash_pool->getItem($key);
95 
96  $item->set($data);
97  $item->expiresAfter($ttl);
98  $result = $item->save();
99 
100  if ($result) {
101  _elgg_services()->logger->info("MEMCACHE: SAVE SUCCESS $key");
102  } else {
103  _elgg_services()->logger->error("MEMCACHE: SAVE FAIL $key");
104  }
105 
106  return $result;
107  }
108 
118  public function load($key, $unused1 = 0, $unused2 = null) {
119  $key = $this->makeMemcacheKey($key);
120 
121  $item = $this->stash_pool->getItem($key);
122 
123  $item->setInvalidationMethod(\Stash\Invalidation::NONE);
124  $value = $item->get();
125 
126  if ($item->isMiss()) {
127  _elgg_services()->logger->info("MEMCACHE: LOAD MISS $key");
128  return false;
129  }
130 
131  _elgg_services()->logger->info("MEMCACHE: LOAD HIT $key");
132 
133  return $value;
134  }
135 
143  public function delete($key) {
144  $key = $this->makeMemcacheKey($key);
145  return $this->stash_pool->getItem($key)->clear();
146  }
147 
153  public function clear() {
154  // using stacks grouping http://www.stashphp.com/Grouping.html#stacks
155  // this will clear all key keys beneath it
156  return $this->stash_pool->getItem("/{$this->getNamespace()}")->clear();
157  }
158 
168  public function setNamespace($namespace = "default") {
169  $config_prefix = _elgg_services()->config->getVolatile('memcache_namespace_prefix');
170  if ($config_prefix) {
171  $namespace = $config_prefix . $namespace;
172  }
173 
174  parent::setNamespace($namespace);
175  }
176 }
if(!$items) $item
Definition: delete.php:17
$data
Definition: opendd.php:13
$value
Definition: longtext.php:42
clear()
Clears all values in the namespace of this cache.
Memcache wrapper class.
Definition: ElggMemcache.php:6
$key
Definition: summary.php:34
load($key, $unused1=0, $unused2=null)
Retrieves data.
save($key, $data, $ttl=null)
Saves a name and value to the cache.
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
setNamespace($namespace="default")
Set the namespace of this cache.
setDefaultExpiry($ttl=0)
Set the default TTL.
__construct($namespace= 'default',\Stash\Pool $pool=null, $ttl=null)
Constructor.