Add a mock cache driver.
authorGunnar Wrobel <p@rdus.de>
Tue, 27 Apr 2010 05:22:04 +0000 (07:22 +0200)
committerGunnar Wrobel <wrobel@temple.(none)>
Tue, 27 Apr 2010 05:34:50 +0000 (07:34 +0200)
framework/Cache/lib/Horde/Cache/Mock.php [new file with mode: 0644]
framework/Cache/package.xml

diff --git a/framework/Cache/lib/Horde/Cache/Mock.php b/framework/Cache/lib/Horde/Cache/Mock.php
new file mode 100644 (file)
index 0000000..55ee5e9
--- /dev/null
@@ -0,0 +1,91 @@
+<?php
+/**
+ * The Horde_Cache_Mock:: class provides a memory based implementation of the
+ * Horde caching system. It persists only during a script run and ignores the
+ * object lifetime because of that.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Horde
+ * @package  Cache
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Cache
+ */
+class Horde_Cache_Mock extends Horde_Cache_Base
+{
+    /**
+     * The storage location for this cache.
+     *
+     * @var array
+     */
+    private $_cache = array();
+
+    /**
+     * Construct a new Horde_Cache_Mock object.
+     *
+     * @param array $params  Configuration parameters:
+     */
+    public function __construct($params = array())
+    {
+        parent::__construct($params);
+    }
+
+    /**
+     * Attempts to retrieve a piece of cached data and return it to the caller.
+     *
+     * @param string $key        Cache key to fetch.
+     * @param integer $lifetime  Lifetime of the key in seconds.
+     *
+     * @return mixed  Cached data, or false if none was found.
+     */
+    public function get($key, $lifetime = 1)
+    {
+        return isset($this->_cache[$key])
+            ? $this->_cache[$key]
+            : false;
+    }
+
+    /**
+     * Attempts to store an object to the cache.
+     *
+     * @param string $key        Cache key (identifier).
+     * @param mixed $data        Data to store in the cache.
+     * @param integer $lifetime  Data lifetime.
+     *
+     * @return boolean  True on success, false on failure.
+     */
+    public function set($key, $data, $lifetime = null)
+    {
+        $this->_cache[$key] = $data;
+    }
+
+    /**
+     * Checks if a given key exists in the cache, valid for the given
+     * lifetime.
+     *
+     * @param string $key        Cache key to check.
+     * @param integer $lifetime  Lifetime of the key in seconds.
+     *
+     * @return boolean  Existence.
+     */
+    public function exists($key, $lifetime = 1)
+    {
+        return isset($this->_cache[$key]);
+    }
+
+    /**
+     * Expire any existing data for the given key.
+     *
+     * @param string $key  Cache key to expire.
+     *
+     * @return boolean  Success or failure.
+     */
+    public function expire($key)
+    {
+        unset($this->_cache[$key]);
+    }
+}
index 4e2a03f..04ae2fd 100644 (file)
@@ -48,6 +48,7 @@ Performance Suite&apos;s content cache), memcached, or an SQL table.
       <file name="Eaccelerator.php" role="php" />
       <file name="File.php" role="php" />
       <file name="Memcache.php" role="php" />
+      <file name="Mock.php" role="php" />
       <file name="Null.php" role="php" />
       <file name="Sql.php" role="php" />
       <file name="Xcache.php" role="php" />
@@ -94,6 +95,7 @@ Performance Suite&apos;s content cache), memcached, or an SQL table.
    <install name="lib/Horde/Cache/Eaccelerator.php" as="Horde/Cache/Eaccelerator.php" />
    <install name="lib/Horde/Cache/File.php" as="Horde/Cache/File.php" />
    <install name="lib/Horde/Cache/Memcache.php" as="Horde/Cache/Memcache.php" />
+   <install name="lib/Horde/Cache/Mock.php" as="Horde/Cache/Mock.php" />
    <install name="lib/Horde/Cache/Null.php" as="Horde/Cache/Null.php" />
    <install name="lib/Horde/Cache/Sql.php" as="Horde/Cache/Sql.php" />
    <install name="lib/Horde/Cache/Xcache.php" as="Horde/Cache/Xcache.php" />