autoload; cache; tests;
[m6w6/pq-gateway] / lib / pq / Gateway / Table / StaticCache.php
diff --git a/lib/pq/Gateway/Table/StaticCache.php b/lib/pq/Gateway/Table/StaticCache.php
new file mode 100644 (file)
index 0000000..7f31346
--- /dev/null
@@ -0,0 +1,51 @@
+<?php
+
+namespace pq\Gateway\Table;
+
+class StaticCache implements CacheInterface
+{
+       protected static $cache = array();
+       
+       /**
+        * @inheritdoc
+        * @param string $key
+        * @param bool $exists
+        * @return mixed
+        */
+       function get($key, &$exists = null) {
+               if (($exists = array_key_exists($key, static::$cache))) {
+                       list($ttl, $data) = static::$cache[$key];
+                       if ($ttl && $ttl < time()) {
+                               unset(static::$cache[$key]);
+                               $exists = false;
+                               return null;
+                       }
+                       return $data;
+               }
+       }
+       
+       /**
+        * @inheritdoc
+        * @param string $key
+        * @param mixed $val
+        * @param int $ttl
+        * @return \pq\Gateway\Table\StaticCache
+        */
+       function set($key, $val, $ttl = 0) {
+               static::$cache[$key] = array(
+                       $ttl ? $ttl + time() : 0,
+                       $val
+               );
+               return $this;
+       }
+       
+       /**
+        * @inheritdoc
+        * @param string $key
+        * @return \pq\Gateway\Table\StaticCache
+        */
+       function del($key) {
+               unset(static::$cache[$key]);
+               return $this;
+       }
+}