Overview

Packages

  • Cache

Classes

  • Horde_Cache
  • Horde_Cache_Exception
  • Horde_Cache_Storage_Apc
  • Horde_Cache_Storage_Base
  • Horde_Cache_Storage_Eaccelerator
  • Horde_Cache_Storage_File
  • Horde_Cache_Storage_Memcache
  • Horde_Cache_Storage_Mock
  • Horde_Cache_Storage_Null
  • Horde_Cache_Storage_Session
  • Horde_Cache_Storage_Sql
  • Horde_Cache_Storage_Stack
  • Horde_Cache_Storage_Xcache
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * This class provides the API interface to the cache storage drivers.
  4:  *
  5:  * Copyright 1999-2012 Horde LLC (http://www.horde.org/)
  6:  *
  7:  * See the enclosed file COPYING for license information (LGPL). If you
  8:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  9:  *
 10:  * @author   Anil Madhavapeddy <anil@recoil.org>
 11:  * @author   Chuck Hagenbuch <chuck@horde.org>
 12:  * @author   Michael Slusarz <slusarz@horde.org>
 13:  * @category Horde
 14:  * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
 15:  * @package  Cache
 16:  */
 17: class Horde_Cache
 18: {
 19:     /**
 20:      * Cache parameters.
 21:      *
 22:      * @var array
 23:      */
 24:     protected $_params = array(
 25:         'compress' => false,
 26:         'lifetime' => 86400
 27:     );
 28: 
 29:     /**
 30:      * Logger.
 31:      *
 32:      * @var Horde_Log_Logger
 33:      */
 34:     protected $_logger;
 35: 
 36:     /**
 37:      * Storage object.
 38:      *
 39:      * @var Horde_Cache_Storage
 40:      */
 41:     protected $_storage;
 42: 
 43:     /**
 44:      * Constructor.
 45:      *
 46:      * @param Horde_Cache_Storage $storage  The storage object.
 47:      * @param array $params                 Parameter array:
 48:      * <pre>
 49:      * 'compress' - (boolean) Compress data? Requires the 'lzf' PECL
 50:      *              extension.
 51:      *              DEFAULT: false
 52:      * 'lifetime' - (integer) Lifetime of data, in seconds.
 53:      *              DEFAULT: 86400 seconds
 54:      * 'logger' - (Horde_Log_Logger) Log object to use for log/debug messages.
 55:      * </pre>
 56:      */
 57:     public function __construct(Horde_Cache_Storage_Base $storage,
 58:                                 array $params = array())
 59:     {
 60:         if (isset($params['logger'])) {
 61:             $this->_logger = $params['logger'];
 62:             unset($params['logger']);
 63: 
 64:             $storage->setLogger($this->_logger);
 65:         }
 66: 
 67:         if (!empty($params['compress']) && !extension_loaded('lzf')) {
 68:             unset($params['compress']);
 69:         }
 70: 
 71:         $this->_params = array_merge($this->_params, $params);
 72:         $this->_storage = $storage;
 73:     }
 74: 
 75:     /**
 76:      * Attempts to directly output a cached object.
 77:      *
 78:      * @param string $key        Object ID to query.
 79:      * @param integer $lifetime  Lifetime of the object in seconds.
 80:      *
 81:      * @return boolean  True if output or false if no object was found.
 82:      */
 83:     public function output($key, $lifetime = 1)
 84:     {
 85:         $data = $this->get($key, $lifetime);
 86:         if ($data === false) {
 87:             return false;
 88:         }
 89: 
 90:         echo $data;
 91:         return true;
 92:     }
 93: 
 94:     /**
 95:      * Retrieve cached data.
 96:      *
 97:      * @param string $key        Object ID to query.
 98:      * @param integer $lifetime  Lifetime of the object in seconds.
 99:      *
100:      * @return mixed  Cached data, or false if none was found.
101:      */
102:     public function get($key, $lifetime = 1)
103:     {
104:         $res = $this->_storage->get($key, $lifetime);
105: 
106:         return ($this->_params['compress'] && ($res !== false))
107:             // lzf_decompress() returns false on error
108:             ? @lzf_decompress($res)
109:             : $res;
110:     }
111: 
112:     /**
113:      * Store an object in the cache.
114:      *
115:      * @param string $key        Object ID used as the caching key.
116:      * @param string $data       Data to store in the cache.
117:      * @param integer $lifetime  Object lifetime - i.e. the time before the
118:      *                           data becomes available for garbage
119:      *                           collection.  If null use the default Horde GC
120:      *                           time.  If 0 will not be GC'd.
121:      */
122:     public function set($key, $data, $lifetime = null)
123:     {
124:         if ($this->_params['compress']) {
125:             $data = lzf_compress($data);
126:         }
127:         $lifetime = is_null($lifetime)
128:             ? $this->_params['lifetime']
129:             : $lifetime;
130: 
131:         $this->_storage->set($key, $data, $lifetime);
132:     }
133: 
134:     /**
135:      * Checks if a given key exists in the cache, valid for the given
136:      * lifetime.
137:      *
138:      * @param string $key        Cache key to check.
139:      * @param integer $lifetime  Lifetime of the key in seconds.
140:      *
141:      * @return boolean  Existence.
142:      */
143:     public function exists($key, $lifetime = 1)
144:     {
145:         return $this->_storage->exists($key, $lifetime);
146:     }
147: 
148:     /**
149:      * Expire any existing data for the given key.
150:      *
151:      * @param string $key  Cache key to expire.
152:      *
153:      * @return boolean  Success or failure.
154:      */
155:     public function expire($key)
156:     {
157:         return $this->_storage->expire($key);
158:     }
159: 
160:     /**
161:      * Clears all data from the cache.
162:      *
163:      * @throws Horde_Cache_Exception
164:      */
165:     public function clear()
166:     {
167:         return $this->_storage->clear();
168:     }
169: 
170: }
171: 
API documentation generated by ApiGen