use starge driver prefix option
authorMichael Wallner <mike@php.net>
Wed, 20 May 2015 14:33:14 +0000 (16:33 +0200)
committerMichael Wallner <mike@php.net>
Wed, 20 May 2015 14:33:14 +0000 (16:33 +0200)
app/Github/Storage/Memcache.php
app/Github/Storage/Redis.php
app/bootstrap/github.php

index 452574b9dbb9f4dfec9b9516bb0003eb2b83e6b5..5e4959b9bc0c028c6523f05e0288e6a4e5fd0e9d 100644 (file)
@@ -14,16 +14,13 @@ class Memcache implements Storage
                if (!$mc) {
                        $mc = new \Memcached("pharext");
                        $mc->addServer("localhost", 11211);
+                       $mc->setOption(\Memcached::OPT_PREFIX_KEY, "$ns:");
                }
                $this->mc = $mc;
        }
 
-       private function key($key) {
-               return sprintf("%s:%s", $this->ns, $key);
-       }
-
        function get($key, Item &$item = null, $update = false) {
-               if (!$item = $this->mc->get($this->key($key))) {
+               if (!$item = $this->mc->get($key)) {
                        return false;
                }
 
@@ -33,7 +30,7 @@ class Memcache implements Storage
                if ($item->getLTL() >= 0) {
                        if ($update) {
                                $item->setTimestamp();
-                               $this->mc->set($this->key($key), $item, $item->getTTL() + 60*60*24);
+                               $this->mc->set($key, $item, $item->getTTL() + 60*60*24);
                        }
                        return true;
                }
@@ -41,11 +38,11 @@ class Memcache implements Storage
        }
 
        function set($key, Item $item) {
-               $this->mc->set($this->key($key), $item, (null !== $item->getTTL()) ? $item->getTTL() + 60*60*24 : 0);
+               $this->mc->set($key, $item, (null !== $item->getTTL()) ? $item->getTTL() + 60*60*24 : 0);
                return $this;
        }
 
        function del($key) {
-               $this->mc->delete($this->key($key));
+               $this->mc->delete($key);
        }
 }
index c971687aa1b0e44f2e4ed811abe6d527635db935..c6db525091c7d1fe1360906382a8ea875aeb3a8b 100644 (file)
@@ -15,16 +15,13 @@ class Redis implements Storage
                        $rd = new \Redis();
                        $rd->open("localhost");
                        $rd->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);
+                       $rd->setOption(\Redis::OPT_PREFIX, "$ns:");
                }
                $this->rd = $rd;
        }
 
-       private function key($key) {
-               return sprintf("%s:%s", $this->ns, $key);
-       }
-
        function get($key, Item &$item = null, $update = false) {
-               if (!$item = $this->rd->get($this->key($key))) {
+               if (!$item = $this->rd->get($key)) {
                        return false;
                }
 
@@ -34,7 +31,7 @@ class Redis implements Storage
                if ($item->getLTL() >= 0) {
                        if ($update) {
                                $item->setTimestamp();
-                               $this->rd->setex($this->key($key), $item->getTTL() + 60*60*24, $item);
+                               $this->rd->setex($key, $item->getTTL() + 60*60*24, $item);
                        }
                        return true;
                }
@@ -43,14 +40,14 @@ class Redis implements Storage
 
        function set($key, Item $item) {
                if (null === $item->getTTL()) {
-                       $this->rd->set($this->key($key), $item);
+                       $this->rd->set($key, $item);
                } else {
-                       $this->rd->setex($this->key($key), $item->getTTL() + 60*60*24, $item);
+                       $this->rd->setex($key, $item->getTTL() + 60*60*24, $item);
                }
                return $this;
        }
 
        function del($key) {
-               $this->rd->delete($this->key($key));
+               $this->rd->delete($key);
        }
 }
index e0defa7bc5482da1f53c647431470e25e1e43eca..e03cba099fd16717fe1c4b09911e2c5be263e1ae 100644 (file)
@@ -16,19 +16,18 @@ $injector->share(Github\API::class)
                                $config->$basic->auth->toArray(),
                                0);
                }
+               $logger = new Github\Logger($config);
+               
                // FIXME: configure through app.ini
                try {
                        $cache = new Github\Storage\Redis("gh-cache");
                } catch (\Exception $ex) {
-                       try {
-                               $cache = new Github\Storage\Memcache("gh-cache");
-                       } catch (\Exception $ex) {
-                               $cache = null;
-                       }
+                       /* Memcached won't throw an exception */
+                       $cache = new Github\Storage\Memcache("gh-cache");
                }
                return new Github\API(
                         $config->github
-                       ,new Github\Logger($config)
+                       ,$logger
                        ,new Github\Storage\Session("gh-tokens")
                        ,$cache
           );