OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / include / mos / class / cache.php
1 <?php
2 /**
3  * @version             $Id: cache.php 1956 2009-06-03 06:24:07Z fishbone $
4  * @package             Joomla.Framework
5  * @subpackage  Cache
6  * @copyright   Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
7  * @license             GNU/GPL, see LICENSE.php
8  * Joomla! is free software. This version may have been modified pursuant to the
9  * GNU General Public License, and as distributed it includes or is derivative
10  * of works licensed under the GNU General Public License or other free or open
11  * source software licenses. See COPYRIGHT.php for copyright notices and
12  * details.
13  */
14
15 // Check to ensure this file is within the rest of the framework
16 //defined('JPATH_BASE') or die();
17
18 //Register the session storage class with the loader
19 //JLoader::register('JCacheStorage', dirname(__FILE__).DS.'storage.php');
20
21 /**
22  * Joomla! Cache base object
23  *
24  * @abstract
25  * @package             Joomla.Framework
26  * @subpackage  Cache
27  * @since               1.5
28  */
29 class JCache extends JObject
30 {
31         /**
32          * Storage Handler
33          * @access      private
34          * @var         object
35          */
36         var $_handler;
37
38         /**
39          * Cache Options
40          * @access      private
41          * @var         array
42          */
43         var $_options;
44
45         /**
46          * Constructor
47          *
48          * @access      protected
49          * @param       array   $options        options
50          */
51         function __construct($options)
52         {
53                 $this->_options =& $options;
54
55                 // Get the default group and caching
56                 if(isset($options['language'])) {
57                         $this->_options['language'] = $options['language'];
58                 } else {
59                         $options['language'] = 'en-GB';
60                 }
61
62                 if(isset($options['cachebase'])) {
63                         $this->_options['cachebase'] = $options['cachebase'];
64                 } else {
65                         $this->_options['cachebase'] = JPATH_ROOT.DS.'cache';
66                 }
67
68                 if(isset($options['defaultgroup'])) {
69                         $this->_options['defaultgroup'] = $options['defaultgroup'];
70                 } else {
71                         $this->_options['defaultgroup'] = 'default';
72                 }
73
74                 if(isset($options['caching'])) {
75                         $this->_options['caching'] =  $options['caching'];
76                 } else {
77                         $this->_options['caching'] = true;
78                 }
79
80                 if( isset($options['storage'])) {
81                         $this->_options['storage'] = $options['storage'];
82                 } else {
83                         $this->_options['storage'] = 'file';
84                 }
85
86                 //Fix to detect if template positions are enabled...
87                 if(JRequest::getCMD('tpl',0)) {
88                         $this->_options['caching'] = false;
89                 }
90         }
91
92         /**
93          * Returns a reference to a cache adapter object, always creating it
94          *
95          * @static
96          * @param       string  $type   The cache object type to instantiate
97          * @return      object  A JCache object
98          * @since       1.5
99          */
100         function &getInstance($type = 'output', $options = array())
101         {
102                 global $gEnvManager;
103                 
104                 $type = strtolower(preg_replace('/[^A-Z0-9_\.-]/i', '', $type));
105
106                 $class = 'JCache'.ucfirst($type);
107                 if(!class_exists($class))
108                 {
109                         //$path = dirname(__FILE__).DS.'handler'.DS.$type.'.php';
110                         $path = $gEnvManager->getJoomlaRootPath() . '/class/cache/callback.php';
111
112                         if (file_exists($path)) {
113                                 require_once($path);
114                         } else {
115                                 JError::raiseError(500, 'Unable to load Cache Handler: '.$type);
116                         }
117                 }
118
119                 $instance = new $class($options);
120
121                 return $instance;
122         }
123
124         /**
125          * Get the storage handlers
126          *
127          * @access public
128          * @return array An array of available storage handlers
129          */
130         function getStores()
131         {
132                 jimport('joomla.filesystem.folder');
133                 $handlers = JFolder::files(dirname(__FILE__).DS.'storage', '.php$');
134
135                 $names = array();
136                 foreach($handlers as $handler)
137                 {
138                         $name = substr($handler, 0, strrpos($handler, '.'));
139                         $class = 'JCacheStorage'.$name;
140
141                         if(!class_exists($class)) {
142                                 require_once(dirname(__FILE__).DS.'storage'.DS.$name.'.php');
143                         }
144
145                         if(call_user_func_array( array( trim($class), 'test' ), null)) {
146                                 $names[] = $name;
147                         }
148                 }
149
150                 return $names;
151         }
152
153         /**
154          * Set caching enabled state
155          *
156          * @access      public
157          * @param       boolean $enabled        True to enable caching
158          * @return      void
159          * @since       1.5
160          */
161         function setCaching($enabled)
162         {
163                 $this->_options['caching'] = $enabled;
164         }
165
166         /**
167          * Set cache lifetime
168          *
169          * @access      public
170          * @param       int     $lt     Cache lifetime
171          * @return      void
172          * @since       1.5
173          */
174         function setLifeTime($lt)
175         {
176                 $this->_options['lifetime'] = $lt;
177         }
178
179         /**
180          * Set cache validation
181          *
182          * @access      public
183          * @return      void
184          * @since       1.5
185          */
186         function setCacheValidation()
187         {
188                 // Deprecated
189         }
190
191         /**
192          * Get cached data by id and group
193          *
194          * @abstract
195          * @access      public
196          * @param       string  $id             The cache data id
197          * @param       string  $group  The cache data group
198          * @return      mixed   Boolean false on failure or a cached data string
199          * @since       1.5
200          */
201         function get($id, $group=null)
202         {
203                 // Get the default group
204                 $group = ($group) ? $group : $this->_options['defaultgroup'];
205
206                 // Get the storage handler
207 /*              $handler =& $this->_getStorage();
208                 if (!JError::isError($handler) && $this->_options['caching']) {
209                         return $handler->get($id, $group, (isset($this->_options['checkTime']))? $this->_options['checkTime'] : true);
210                 }*/
211                 return false;
212         }
213
214         /**
215          * Store the cached data by id and group
216          *
217          * @access      public
218          * @param       string  $id             The cache data id
219          * @param       string  $group  The cache data group
220          * @param       mixed   $data   The data to store
221          * @return      boolean True if cache stored
222          * @since       1.5
223          */
224         function store($data, $id, $group=null)
225         {
226                 // Get the default group
227                 $group = ($group) ? $group : $this->_options['defaultgroup'];
228
229                 // Get the storage handler and store the cached data
230 /*              $handler =& $this->_getStorage();
231                 if (!JError::isError($handler) && $this->_options['caching']) {
232                         return $handler->store($id, $group, $data);
233                 }*/
234                 return false;
235         }
236
237         /**
238          * Remove a cached data entry by id and group
239          *
240          * @abstract
241          * @access      public
242          * @param       string  $id             The cache data id
243          * @param       string  $group  The cache data group
244          * @return      boolean True on success, false otherwise
245          * @since       1.5
246          */
247         function remove($id, $group=null)
248         {
249                 // Get the default group
250                 $group = ($group) ? $group : $this->_options['defaultgroup'];
251
252                 // Get the storage handler
253                 $handler =& $this->_getStorage();
254                 if (!JError::isError($handler)) {
255                         return $handler->remove($id, $group);
256                 }
257                 return false;
258         }
259
260         /**
261          * Clean cache for a group given a mode.
262          *
263          * group mode           : cleans all cache in the group
264          * notgroup mode        : cleans all cache not in the group
265          *
266          * @access      public
267          * @param       string  $group  The cache data group
268          * @param       string  $mode   The mode for cleaning cache [group|notgroup]
269          * @return      boolean True on success, false otherwise
270          * @since       1.5
271          */
272         function clean($group=null, $mode='group')
273         {
274                 // Get the default group
275                 $group = ($group) ? $group : $this->_options['defaultgroup'];
276
277                 // Get the storage handler
278                 $handler =& $this->_getStorage();
279                 if (!JError::isError($handler)) {
280                         return $handler->clean($group, $mode);
281                 }
282                 return false;
283         }
284
285         /**
286          * Garbage collect expired cache data
287          *
288          * @access public
289          * @return boolean  True on success, false otherwise.
290          * @since       1.5
291          */
292         function gc()
293         {
294                 // Get the storage handler
295                 $handler =& $this->_getStorage();
296                 if (!JError::isError($handler)) {
297                         return $handler->gc();
298                 }
299                 return false;
300         }
301
302         /**
303          * Get the cache storage handler
304          *
305          * @access protected
306          * @return object A JCacheStorage object
307          * @since       1.5
308          */
309         function &_getStorage()
310         {
311                 if (is_a($this->_handler, 'JCacheStorage')) {
312                         return $this->_handler;
313                 }
314
315 //              $this->_handler =& JCacheStorage::getInstance($this->_options['storage'], $this->_options);
316                 return $this->_handler;
317         }
318 }