Add Horde_Cache_Session::.
authorMichael M Slusarz <slusarz@curecanti.org>
Fri, 3 Sep 2010 18:26:36 +0000 (12:26 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Fri, 3 Sep 2010 19:59:29 +0000 (13:59 -0600)
framework/Cache/lib/Horde/Cache/Session.php [new file with mode: 0644]
framework/Cache/package.xml

diff --git a/framework/Cache/lib/Horde/Cache/Session.php b/framework/Cache/lib/Horde/Cache/Session.php
new file mode 100644 (file)
index 0000000..9dd3066
--- /dev/null
@@ -0,0 +1,122 @@
+<?php
+/**
+ * This class provides a session storage implementation of the Horde caching
+ * system.
+ *
+ * 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.
+ *
+ * @author   Michael Slusarz <slusarz@curecanti.org>
+ * @category Horde
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @package  Cache
+ */
+class Horde_Cache_Session extends Horde_Cache_Base
+{
+    /**
+     * Pointer to the session entry.
+     *
+     * @var array
+     */
+    protected $_sess;
+
+    /**
+     * Constructor.
+     *
+     * @param array $params  Optional parameters:
+     * <pre>
+     * 'session' - (string) Store session data in this entry.
+     *             DEFAULT: 'horde_cache_session'
+     * </pre>
+     */
+    public function __construct(array $params = array())
+    {
+        $params = array_merge(array(
+            'sess_name' => 'horde_cache_session'
+        ), $params);
+
+        parent::__construct($params);
+
+        if (!isset($_SESSION[$this->_params['sess_name']])) {
+            $_SESSION[$this->_params['sess_name']] = array();
+        }
+        $this->_sess = &$_SESSION[$this->_params['sess_name']];
+    }
+
+    /**
+     * 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)
+    {
+        if ($this->exists($key, $lifetime)) {
+            return $this->_sess[$key]['d'];
+        }
+
+        return 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.
+     */
+    public function set($key, $data, $lifetime = null)
+    {
+        $this->_sess[$key] = array(
+            'd' => $data,
+            'l' => $this->_getLifetime($lifetime)
+        );
+    }
+
+    /**
+     * 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)
+    {
+        if (isset($this->_sess[$key])) {
+            /* 0 means no expire. */
+            if (($lifetime == 0) ||
+                ((time() - $lifetime) <= $this->_sess[$key]['l'])) {
+                return true;
+            }
+
+            unset($this->_sess[$key]);
+        }
+
+        return false;
+    }
+
+    /**
+     * Expire any existing data for the given key.
+     *
+     * @param string $key  Cache key to expire.
+     *
+     * @return boolean  Success or failure.
+     */
+    public function expire($key)
+    {
+        if (isset($this->_sess[$key])) {
+            unset($this->_sess[$key]);
+            return true;
+        }
+
+        return false;
+    }
+
+}
index 6952f37..90c5a62 100644 (file)
@@ -33,7 +33,8 @@ Performance Suite&apos;s content cache), memcached, or an SQL table.
   <api>beta</api>
  </stability>
  <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
- <notes>* Horde_Cache_Base::set() no longer returns a boolean result.
+ <notes>* Added Horde_Cache_Session::.
+ * Horde_Cache_Base::set() no longer returns a boolean result.
  * Added Horde_Cache_Exception::.
  * Removed dependency on Horde Core.
  * Initial Horde 4 package.</notes>
@@ -53,6 +54,7 @@ Performance Suite&apos;s content cache), memcached, or an SQL table.
       <file name="Memcache.php" role="php" />
       <file name="Mock.php" role="php" />
       <file name="Null.php" role="php" />
+      <file name="Session.php" role="php" />
       <file name="Sql.php" role="php" />
       <file name="Stack.php" role="php" />
       <file name="Xcache.php" role="php" />
@@ -110,6 +112,7 @@ Performance Suite&apos;s content cache), memcached, or an SQL table.
    <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/Session.php" as="Horde/Cache/Session.php" />
    <install name="lib/Horde/Cache/Sql.php" as="Horde/Cache/Sql.php" />
    <install name="lib/Horde/Cache/Stack.php" as="Horde/Cache/Stack.php" />
    <install name="lib/Horde/Cache/Xcache.php" as="Horde/Cache/Xcache.php" />