New records could not hold multiple habtm associates using ::set(). Fixes #160.
[akelos.git] / lib / AkCache.php
blobd33ed227b895af165ca75f5307d5d7614e074e05
1 <?php
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 // +----------------------------------------------------------------------+
11 /**
12 * @package ActiveSupport
13 * @subpackage Cache
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');
25 /**
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
31 * system.
33 * By default the cache container is defined in the following
34 * line
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.
47 * <code>
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);
65 * }
67 * // Now you can use data no matter from where did it came from
68 * echo $data;
69 * </code>
71 * This class uses the
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>
80 * @since 0.1
81 * @version $Revision 0.1 $
83 class AkCache extends AkObject
86 /**
87 * Handles an instance of current Cache driver
89 * @access private
90 * @var object $_driverInstance
92 var $_driverInstance = NULL;
94 /**
95 * Ecnables / Disables caching
97 * @access public
98 * @var boolean true
100 var $cache_enabled = true;
104 * Class constructor (ALA Akelos Framework)
106 * This method loads an instance of selected driver in order to
107 * use it class wide.
109 * @access public
110 * @param mixed $options You can pass a number specifying the second for
111 * the cache to expire or an array with the
112 * following options:
114 * <code>
115 * $options = array(
116 * //This options are valid for both cache contains (database and file based)
117 * 'lifeTime' => cache lifetime in seconds
118 * (int),
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)
136 * );
137 * </code>
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
149 * @return void
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) {
156 case 1:
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 if(!is_dir(AK_CACHE_DIR)){
163 Ak::make_dir(AK_CACHE_DIR, array('base_path'=>AK_TMP_DIR));
165 $options['cacheDir'] = AK_CACHE_DIR.DS;
167 $this->_driverInstance =& new Cache_Lite($options);
168 break;
169 case 2:
170 $this->cache_enabled = true;
171 require_once(AK_LIB_DIR.'/AkCache/AkAdodbCache.php');
172 $this->_driverInstance =& new AkAdodbCache();
173 $this->_driverInstance->init($options);
174 break;
175 default:
176 $this->cache_enabled = false;
177 break;
183 * Test if a cache is available and (if yes) return it
185 * @access public
186 * @param string $id Cache id
187 * @param string $group Name of the cache group.
188 * @return mixed Data of the cache (or false if no cache available)
190 function get($id, $group = 'default')
192 return $this->cache_enabled ? $this->_driverInstance->get($id, $group) : false;
197 * Save some data in the cache
199 * @access public
200 * @param string $data Data to put in cache
201 * @param string $id Cache id
202 * @param string $group Name of the cache group
203 * @return boolean True if no problem
205 function save($data, $id = null, $group = 'default')
207 return $this->cache_enabled ? $this->_driverInstance->save($data, $id, $group) : true;
212 * Remove a cache item
214 * @access public
215 * @param string $id Cache id
216 * @param string $group Name of the cache group
217 * @return boolean True if no problem
219 function remove($id, $group = 'default')
221 return $this->cache_enabled ? $this->_driverInstance->remove($id, $group) : true;
226 * Clean the cache
228 * If no group is specified all cache items will be destroyed
229 * else only cache items of the specified group will be
230 * destroyed
232 * @access public
233 * @param string $group Name of the cache group.
234 * If no group is specified all cache items will be
235 * destroyed else only cache items of the specified
236 * group will be destroyed
237 * @param string $mode Flush cache mode. Options are:
239 * - old
240 * - ingroup
241 * - notingroup
242 * @return boolean True if no problem
244 function clean($group = false, $mode = 'ingroup')
246 return $this->cache_enabled ? $this->_driverInstance->clean($group, $mode) : true;