Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
xcache.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Cache
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * XCache cache storage handler
14  *
15  * @package Joomla.Platform
16  * @subpackage Cache
17  * @link http://xcache.lighttpd.net/
18  * @since 11.1
19  */
21 {
22  /**
23  * Get cached data by id and group
24  *
25  * @param string $id The cache data id
26  * @param string $group The cache data group
27  * @param boolean $checkTime True to verify cache time expiration threshold
28  *
29  * @return mixed Boolean false on failure or a cached data string
30  *
31  * @since 11.1
32  */
33  public function get($id, $group, $checkTime = true)
34  {
35  $cache_id = $this->_getCacheId($id, $group);
36  $cache_content = xcache_get($cache_id);
37 
38  if ($cache_content === null)
39  {
40  return false;
41  }
42 
43  return $cache_content;
44  }
45 
46  /**
47  * Get all cached data
48  *
49  * This requires the php.ini setting xcache.admin.enable_auth = Off.
50  *
51  * @return array data
52  *
53  * @since 11.1
54  */
55  public function getAll()
56  {
57  parent::getAll();
58 
59  $allinfo = xcache_list(XC_TYPE_VAR, 0);
60  $keys = $allinfo['cache_list'];
61  $secret = $this->_hash;
62 
63  $data = array();
64 
65  foreach ($keys as $key)
66  {
67 
68  $namearr = explode('-', $key['name']);
69 
70  if ($namearr !== false && $namearr[0] == $secret && $namearr[1] == 'cache')
71  {
72  $group = $namearr[2];
73 
74  if (!isset($data[$group]))
75  {
76  $item = new JCacheStorageHelper($group);
77  }
78  else
79  {
80  $item = $data[$group];
81  }
82 
83  $item->updateSize($key['size'] / 1024);
84 
85  $data[$group] = $item;
86  }
87  }
88 
89  return $data;
90  }
91 
92  /**
93  * Store the data by id and group
94  *
95  * @param string $id The cache data id
96  * @param string $group The cache data group
97  * @param string $data The data to store in cache
98  *
99  * @return boolean True on success, false otherwise
100  *
101  * @since 11.1
102  */
103  public function store($id, $group, $data)
104  {
105  $cache_id = $this->_getCacheId($id, $group);
106  $store = xcache_set($cache_id, $data, $this->_lifetime);
107  return $store;
108  }
109 
110  /**
111  * Remove a cached data entry by id and group
112  *
113  * @param string $id The cache data id
114  * @param string $group The cache data group
115  *
116  * @return boolean True on success, false otherwise
117  *
118  * @since 11.1
119  */
120  public function remove($id, $group)
121  {
122  $cache_id = $this->_getCacheId($id, $group);
123 
124  if (!xcache_isset($cache_id))
125  {
126  return true;
127  }
128 
129  return xcache_unset($cache_id);
130  }
131 
132  /**
133  * Clean cache for a group given a mode.
134  *
135  * This requires the php.ini setting xcache.admin.enable_auth = Off.
136  *
137  * @param string $group The cache data group
138  * @param string $mode The mode for cleaning cache [group|notgroup]
139  * group mode : cleans all cache in the group
140  * notgroup mode : cleans all cache not in the group
141  *
142  * @return boolean True on success, false otherwise
143  *
144  * @since 11.1
145  */
146  public function clean($group, $mode = null)
147  {
148  $allinfo = xcache_list(XC_TYPE_VAR, 0);
149  $keys = $allinfo['cache_list'];
150 
151  $secret = $this->_hash;
152  foreach ($keys as $key)
153  {
154  if (strpos($key['name'], $secret . '-cache-' . $group . '-') === 0 xor $mode != 'group')
155  {
156  xcache_unset($key['name']);
157  }
158  }
159  return true;
160  }
161 
162  /**
163  * Garbage collect expired cache data
164  *
165  * This is a dummy, since xcache has built in garbage collector, turn it
166  * on in php.ini by changing default xcache.gc_interval setting from
167  * 0 to 3600 (=1 hour)
168  *
169  * @return boolean True on success, false otherwise.
170  *
171  * @since 11.1
172  */
173  public function gc()
174  {
175  /*
176  $now = time();
177 
178  $cachecount = xcache_count(XC_TYPE_VAR);
179 
180  for ($i = 0; $i < $cachecount; $i ++) {
181 
182  $allinfo = xcache_list(XC_TYPE_VAR, $i);
183  $keys = $allinfo ['cache_list'];
184 
185  foreach($keys as $key) {
186 
187  if (strstr($key['name'], $this->_hash)) {
188  if (($key['ctime'] + $this->_lifetime ) < $this->_now) xcache_unset($key['name']);
189  }
190  }
191  }
192 
193  */
194 
195  return true;
196  }
197 
198  /**
199  * Test to see if the cache storage is available.
200  *
201  * @return boolean True on success, false otherwise.
202  *
203  * @since 12.1
204  */
205  public static function isSupported()
206  {
207  return (extension_loaded('xcache'));
208  }
209 }