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