Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
apc.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  * APC cache storage handler
14  *
15  * @package Joomla.Platform
16  * @subpackage Cache
17  * @see http://php.net/manual/en/book.apc.php
18  * @since 11.1
19  */
21 {
22  /**
23  * Get cached data from APC 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  return apc_fetch($cache_id);
37  }
38 
39  /**
40  * Get all cached data
41  *
42  * @return array data
43  *
44  * @since 11.1
45  */
46  public function getAll()
47  {
48  parent::getAll();
49 
50  $allinfo = apc_cache_info('user');
51  $keys = $allinfo['cache_list'];
52  $secret = $this->_hash;
53 
54  $data = array();
55 
56  foreach ($keys as $key)
57  {
58 
59  $name = $key['info'];
60  $namearr = explode('-', $name);
61 
62  if ($namearr !== false && $namearr[0] == $secret && $namearr[1] == 'cache')
63  {
64  $group = $namearr[2];
65 
66  if (!isset($data[$group]))
67  {
68  $item = new JCacheStorageHelper($group);
69  }
70  else
71  {
72  $item = $data[$group];
73  }
74 
75  $item->updateSize($key['mem_size'] / 1024);
76 
77  $data[$group] = $item;
78  }
79  }
80 
81  return $data;
82  }
83 
84  /**
85  * Store the data to APC by id and group
86  *
87  * @param string $id The cache data id
88  * @param string $group The cache data group
89  * @param string $data The data to store in cache
90  *
91  * @return boolean True on success, false otherwise
92  *
93  * @since 11.1
94  */
95  public function store($id, $group, $data)
96  {
97  $cache_id = $this->_getCacheId($id, $group);
98  return apc_store($cache_id, $data, $this->_lifetime);
99  }
100 
101  /**
102  * Remove a cached data entry by id and group
103  *
104  * @param string $id The cache data id
105  * @param string $group The cache data group
106  *
107  * @return boolean True on success, false otherwise
108  *
109  * @since 11.1
110  */
111  public function remove($id, $group)
112  {
113  $cache_id = $this->_getCacheId($id, $group);
114  return apc_delete($cache_id);
115  }
116 
117  /**
118  * Clean cache for a group given a mode.
119  *
120  * group mode : cleans all cache in the group
121  * notgroup mode : cleans all cache not in the group
122  *
123  * @param string $group The cache data group
124  * @param string $mode The mode for cleaning cache [group|notgroup]
125  *
126  * @return boolean True on success, false otherwise
127  *
128  * @since 11.1
129  */
130  public function clean($group, $mode = null)
131  {
132  $allinfo = apc_cache_info('user');
133  $keys = $allinfo['cache_list'];
134  $secret = $this->_hash;
135 
136  foreach ($keys as $key)
137  {
138 
139  if (strpos($key['info'], $secret . '-cache-' . $group . '-') === 0 xor $mode != 'group')
140  {
141  apc_delete($key['info']);
142  }
143  }
144  return true;
145  }
146 
147  /**
148  * Force garbage collect expired cache data as items are removed only on fetch!
149  *
150  * @return boolean True on success, false otherwise.
151  *
152  * @since 11.1
153  */
154  public function gc()
155  {
156  $allinfo = apc_cache_info('user');
157  $keys = $allinfo['cache_list'];
158  $secret = $this->_hash;
159 
160  foreach ($keys as $key)
161  {
162  if (strpos($key['info'], $secret . '-cache-'))
163  {
164  apc_fetch($key['info']);
165  }
166  }
167  }
168 
169  /**
170  * Test to see if the cache storage is available.
171  *
172  * @return boolean True on success, false otherwise.
173  *
174  * @since 12.1
175  */
176  public static function isSupported()
177  {
178  return extension_loaded('apc');
179  }
180 
181  /**
182  * Lock cached item - override parent as this is more efficient
183  *
184  * @param string $id The cache data id
185  * @param string $group The cache data group
186  * @param integer $locktime Cached item max lock time
187  *
188  * @return object Properties are lock and locklooped
189  *
190  * @since 11.1
191  */
192  public function lock($id, $group, $locktime)
193  {
194  $returning = new stdClass;
195  $returning->locklooped = false;
196 
197  $looptime = $locktime * 10;
198 
199  $cache_id = $this->_getCacheId($id, $group) . '_lock';
200 
201  $data_lock = apc_add($cache_id, 1, $locktime);
202 
203  if ($data_lock === false)
204  {
205 
206  $lock_counter = 0;
207 
208  // Loop until you find that the lock has been released.
209  // That implies that data get from other thread has finished
210  while ($data_lock === false)
211  {
212 
213  if ($lock_counter > $looptime)
214  {
215  $returning->locked = false;
216  $returning->locklooped = true;
217  break;
218  }
219 
220  usleep(100);
221  $data_lock = apc_add($cache_id, 1, $locktime);
222  $lock_counter++;
223  }
224 
225  }
226  $returning->locked = $data_lock;
227 
228  return $returning;
229  }
230 
231  /**
232  * Unlock cached item - override parent for cacheid compatibility with lock
233  *
234  * @param string $id The cache data id
235  * @param string $group The cache data group
236  *
237  * @return boolean True on success, false otherwise.
238  *
239  * @since 11.1
240  */
241  public function unlock($id, $group = null)
242  {
243  $cache_id = $this->_getCacheId($id, $group) . '_lock';
244 
245  $unlock = apc_delete($cache_id);
246  return $unlock;
247  }
248 }