From 4f1148daaa40f372a884092fac3a77925a5e4de2 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Fri, 3 Sep 2010 12:26:36 -0600 Subject: [PATCH] Add Horde_Cache_Session::. --- framework/Cache/lib/Horde/Cache/Session.php | 122 ++++++++++++++++++++++++++++ framework/Cache/package.xml | 5 +- 2 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 framework/Cache/lib/Horde/Cache/Session.php diff --git a/framework/Cache/lib/Horde/Cache/Session.php b/framework/Cache/lib/Horde/Cache/Session.php new file mode 100644 index 000000000..9dd306662 --- /dev/null +++ b/framework/Cache/lib/Horde/Cache/Session.php @@ -0,0 +1,122 @@ + + * @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: + *
+     * 'session' - (string) Store session data in this entry.
+     *             DEFAULT: 'horde_cache_session'
+     * 
+ */ + 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; + } + +} diff --git a/framework/Cache/package.xml b/framework/Cache/package.xml index 6952f37d4..90c5a627a 100644 --- a/framework/Cache/package.xml +++ b/framework/Cache/package.xml @@ -33,7 +33,8 @@ Performance Suite's content cache), memcached, or an SQL table. beta LGPL - * Horde_Cache_Base::set() no longer returns a boolean result. + * 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. @@ -53,6 +54,7 @@ Performance Suite's content cache), memcached, or an SQL table. + @@ -110,6 +112,7 @@ Performance Suite's content cache), memcached, or an SQL table. + -- 2.11.0