2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4 // +----------------------------------------------------------------------+
5 // | Akelos Framework - http://www.akelos.org |
6 // +----------------------------------------------------------------------+
7 // | Copyright (c) 2002-2006, Akelos Media, S.L. & Bermi Ferrer Martinez |
8 // | Released under the GNU Lesser General Public License, see LICENSE.txt|
9 // +----------------------------------------------------------------------+
12 * @package ActiveSupport
14 * @author Bermi Ferrer <bermi a.t akelos c.om>
15 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
16 * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
20 // ---- Required Files ---- //
21 require_once(AK_LIB_DIR
.DS
.'Ak.php');
22 require_once(AK_LIB_DIR
.DS
.'AkObject.php');
26 * Easy to use class for caching data using a database as
27 * container or the file system.
29 * Akelos Framework provides an easy to use functionality for
30 * caching data using a database as container or the file
33 * By default the cache container is defined in the following
36 * <code>define ('AK_CACHE_HANDLER', 1);</code>
38 * in the ''config/config.php'' file
40 * Possible values are:
42 * - 0: No cache at all
43 * - 1: File based cache using the folder defined at AK_CACHE_DIR or the system /tmp dir
44 * - 2: Database based cache. This one has a performance penalty, but works on most servers
46 * Here is a small code spinet of how this works.
48 * // First we include the cache class and
49 * // create a cache instance
50 * include_once(AK_LIB_DIR.'/AkCache.php');
51 * $Cache =& new AkCache();
53 * // Now we define some details for this cache
54 * $seconds = 3600; // seconds of life for this cache
55 * $cache_id = 'unique identifier for accesing this cache element';
57 * // Now we call the $Cache constructor (ALA AkFramework)
58 * $Cache->init($seconds);
60 * // If the data is not cached, we catch it now
61 * // if it was on cache, $data will hold its content
62 * if (!$data = $Cache->get($cache_id)) {
63 * $data = some_heavy_function_that_takes_too_many_time_or_resources();
64 * $Cache->save($data);
67 * // Now you can use data no matter from where did it came from
72 * [http://pear.php.net/manual/en/package.caching.cache-lite.php
73 * pear Cache_Lite] as driver for file based cache.
74 * In fact you can access an instance of Cache_Lite by
75 * accesing $Cache->_driverInstance.
77 * @author Bermi Ferrer <bermi at akelos dot com>
78 * @copyright Copyright (c) 2002-2005, Akelos Media, S.L. http://www.akelos.org
79 * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
81 * @version $Revision 0.1 $
83 class AkCache
extends AkObject
87 * Handles an instance of current Cache driver
90 * @var object $_driverInstance
92 var $_driverInstance = NULL;
95 * Ecnables / Disables caching
100 var $cache_enabled = true;
104 * Class constructor (ALA Akelos Framework)
106 * This method loads an instance of selected driver in order to
110 * @param mixed $options You can pass a number specifying the second for
111 * the cache to expire or an array with the
116 * //This options are valid for both cache contains (database and file based)
117 * 'lifeTime' => cache lifetime in seconds
119 * 'memoryCaching' => enable / disable memory caching (boolean),
120 * 'automaticSerialization' => enable / disable automatic serialization (boolean)
122 * //This options are for file based cache
123 * 'cacheDir' => directory where to put the cache files (string),
124 * 'caching' => enable / disable caching (boolean),
125 * 'fileLocking' => enable / disable fileLocking (boolean),
126 * 'writeControl' => enable / disable write control (boolean),
127 * 'readControl' => enable / disable read control (boolean),
128 * 'readControlType' => type of read control
129 * 'crc32', 'md5', 'strlen' (string),
130 * 'pearErrorMode' => pear error mode (when raiseError is called) (cf PEAR doc) (int),
131 * 'onlyMemoryCaching' => enable / disable only memory caching (boolean),
132 * 'memoryCachingLimit' => max nbr of records to store into memory caching (int),
133 * 'fileNameProtection' => enable / disable automatic file name protection (boolean),
134 * 'automaticCleaningFactor' => distable / tune automatic cleaning process (int)
135 * 'hashedDirectoryLevel' => level of the hashed directory system (int)
138 * @param integer $cache_type The default value is set by defining the constant AK_CACHE_HANDLER in the following line
140 * <code>define ('AK_CACHE_HANDLER', 1);</code>
142 * in the ''config/config.php'' file
144 * Possible values are:
146 * - 0: No cache at all
147 * - 1: File based cache using the folder defined at AK_CACHE_DIR or the system /tmp dir
148 * - 2: Database based cache. This one has a performance penalty, but works on most servers
151 function init($options = null, $cache_type = AK_CACHE_HANDLER
)
153 $options = is_int($options) ?
array('lifeTime'=>$options) : (is_array($options) ?
$options : array());
155 switch ($cache_type) {
157 $this->cache_enabled
= true;
158 if(!class_exists('Cache_Lite')){
159 require_once(AK_CONTRIB_DIR
.'/pear/Cache_Lite/Lite.php');
161 if(!isset($options['cacheDir'])){
162 $options['cacheDir'] = AK_CACHE_DIR
.DS
;
164 $this->_driverInstance
=& new Cache_Lite($options);
167 $this->cache_enabled
= true;
168 require_once(AK_LIB_DIR
.'/AkCache/AkAdodbCache.php');
169 $this->_driverInstance
=& new AkAdodbCache();
170 $this->_driverInstance
->init($options);
173 $this->cache_enabled
= false;
180 * Test if a cache is available and (if yes) return it
183 * @param string $id Cache id
184 * @param string $group Name of the cache group.
185 * @return mixed Data of the cache (or false if no cache available)
187 function get($id, $group = 'default')
189 return $this->cache_enabled ?
$this->_driverInstance
->get($id, $group) : false;
194 * Save some data in the cache
197 * @param string $data Data to put in cache
198 * @param string $id Cache id
199 * @param string $group Name of the cache group
200 * @return boolean True if no problem
202 function save($data, $id = null, $group = 'default')
204 return $this->cache_enabled ?
$this->_driverInstance
->save($data, $id, $group) : true;
209 * Remove a cache item
212 * @param string $id Cache id
213 * @param string $group Name of the cache group
214 * @return boolean True if no problem
216 function remove($id, $group = 'default')
218 return $this->cache_enabled ?
$this->_driverInstance
->remove($id, $group) : true;
225 * If no group is specified all cache items will be destroyed
226 * else only cache items of the specified group will be
230 * @param string $group Name of the cache group.
231 * If no group is specified all cache items will be
232 * destroyed else only cache items of the specified
233 * group will be destroyed
234 * @param string $mode Flush cache mode. Options are:
239 * @return boolean True if no problem
241 function clean($group = false, $mode = 'ingroup')
243 return $this->cache_enabled ?
$this->_driverInstance
->clean($group, $mode) : true;