452574b9dbb9f4dfec9b9516bb0003eb2b83e6b5
[pharext/pharext.org] / app / Github / Storage / Memcache.php
1 <?php
2
3 namespace app\Github\Storage;
4
5 use app\Github\Storage;
6
7 class Memcache implements Storage
8 {
9 private $mc;
10 private $ns;
11
12 function __construct($ns = "github", \Memcached $mc = null) {
13 $this->ns = $ns;
14 if (!$mc) {
15 $mc = new \Memcached("pharext");
16 $mc->addServer("localhost", 11211);
17 }
18 $this->mc = $mc;
19 }
20
21 private function key($key) {
22 return sprintf("%s:%s", $this->ns, $key);
23 }
24
25 function get($key, Item &$item = null, $update = false) {
26 if (!$item = $this->mc->get($this->key($key))) {
27 return false;
28 }
29
30 if (null === $item->getTTL()) {
31 return true;
32 }
33 if ($item->getLTL() >= 0) {
34 if ($update) {
35 $item->setTimestamp();
36 $this->mc->set($this->key($key), $item, $item->getTTL() + 60*60*24);
37 }
38 return true;
39 }
40 return false;
41 }
42
43 function set($key, Item $item) {
44 $this->mc->set($this->key($key), $item, (null !== $item->getTTL()) ? $item->getTTL() + 60*60*24 : 0);
45 return $this;
46 }
47
48 function del($key) {
49 $this->mc->delete($this->key($key));
50 }
51 }