use starge driver prefix option
[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 $mc->setOption(\Memcached::OPT_PREFIX_KEY, "$ns:");
18 }
19 $this->mc = $mc;
20 }
21
22 function get($key, Item &$item = null, $update = false) {
23 if (!$item = $this->mc->get($key)) {
24 return false;
25 }
26
27 if (null === $item->getTTL()) {
28 return true;
29 }
30 if ($item->getLTL() >= 0) {
31 if ($update) {
32 $item->setTimestamp();
33 $this->mc->set($key, $item, $item->getTTL() + 60*60*24);
34 }
35 return true;
36 }
37 return false;
38 }
39
40 function set($key, Item $item) {
41 $this->mc->set($key, $item, (null !== $item->getTTL()) ? $item->getTTL() + 60*60*24 : 0);
42 return $this;
43 }
44
45 function del($key) {
46 $this->mc->delete($key);
47 }
48 }