Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
StashWrapper.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Cache\Pool;
3 
5 
6 use Stash;
7 
17 final class StashWrapper implements Pool {
18 
20  private $stash;
21 
27  public function __construct(Stash\Pool $stash) {
28  $this->stash = $stash;
29  }
30 
32  public function get($key, callable $callback = null, $default = null) {
33  assert(is_string($key) || is_int($key));
34 
35  $item = $this->stash->getItem((string)$key);
36 
37  $result = $item->get();
38 
39  if ($item->isMiss()) {
40  if (!$callback) {
41  return $default;
42  }
43 
44  $item->lock();
45 
46  $result = call_user_func($callback);
47 
48  $this->stash->save($item->set($result));
49  }
50 
51  return $result;
52  }
53 
55  public function invalidate($key) {
56  assert(is_string($key) || is_int($key));
57 
58  $this->stash->getItem((string)$key)->clear();
59  }
60 
62  public function put($key, $value) {
63  assert(is_string($key) || is_int($key));
64 
65  $this->stash->getItem((string)$key)->set($value);
66  }
67 
73  public static function createEphemeral() {
74  return new self(new Stash\Pool(new Stash\Driver\Ephemeral()));
75  }
76 }
$value
Definition: longtext.php:26
$default
Definition: checkbox.php:35
Defers to Stash for the meat of the caching logic.
$key
Definition: summary.php:34
__construct(Stash\Pool $stash)
Constructor.
$item
Definition: item.php:12
static createEphemeral()
Create an in-memory implementation of the pool.