Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
wincache.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  * WINCACHE cache storage handler
14  *
15  * @package Joomla.Platform
16  * @subpackage Cache
17  * @see http://php.net/manual/en/book.wincache.php
18  * @since 11.1
19  */
21 {
22  /**
23  * Constructor
24  *
25  * @param array $options Optional parameters.
26  *
27  * @since 11.1
28  */
29  public function __construct($options = array())
30  {
31  parent::__construct($options);
32  }
33 
34  /**
35  * Get cached data from WINCACHE by id and group
36  *
37  * @param string $id The cache data id
38  * @param string $group The cache data group
39  * @param boolean $checkTime True to verify cache time expiration threshold
40  *
41  * @return mixed Boolean false on failure or a cached data string
42  *
43  * @since 11.1
44  */
45  public function get($id, $group, $checkTime = true)
46  {
47  $cache_id = $this->_getCacheId($id, $group);
48  $cache_content = wincache_ucache_get($cache_id);
49  return $cache_content;
50  }
51 
52  /**
53  * Get all cached data
54  *
55  * @return array data
56  *
57  * @since 11.1
58  */
59  public function getAll()
60  {
61  parent::getAll();
62 
63  $allinfo = wincache_ucache_info();
64  $keys = $allinfo['cache_entries'];
65  $secret = $this->_hash;
66  $data = array();
67 
68  foreach ($keys as $key)
69  {
70  $name = $key['key_name'];
71  $namearr = explode('-', $name);
72  if ($namearr !== false && $namearr[0] == $secret && $namearr[1] == 'cache')
73  {
74  $group = $namearr[2];
75  if (!isset($data[$group]))
76  {
77  $item = new JCacheStorageHelper($group);
78  }
79  else
80  {
81  $item = $data[$group];
82  }
83  if (isset($key['value_size']))
84  {
85  $item->updateSize($key['value_size'] / 1024);
86  }
87  else
88  {
89  // Dummy, WINCACHE version is too low.
90  $item->updateSize(1);
91  }
92  $data[$group] = $item;
93  }
94  }
95 
96  return $data;
97  }
98 
99  /**
100  * Store the data to WINCACHE by id and group
101  *
102  * @param string $id The cache data id
103  * @param string $group The cache data group
104  * @param string $data The data to store in cache
105  *
106  * @return boolean True on success, false otherwise
107  *
108  * @since 11.1
109  */
110  public function store($id, $group, $data)
111  {
112  $cache_id = $this->_getCacheId($id, $group);
113  return wincache_ucache_set($cache_id, $data, $this->_lifetime);
114  }
115 
116  /**
117  * Remove a cached data entry by id and group
118  *
119  * @param string $id The cache data id
120  * @param string $group The cache data group
121  *
122  * @return boolean True on success, false otherwise
123  *
124  * @since 11.1
125  */
126  public function remove($id, $group)
127  {
128  $cache_id = $this->_getCacheId($id, $group);
129  return wincache_ucache_delete($cache_id);
130  }
131 
132  /**
133  * Clean cache for a group given a mode.
134  *
135  * @param string $group The cache data group
136  * @param string $mode The mode for cleaning cache [group|notgroup]
137  * group mode : cleans all cache in the group
138  * notgroup mode : cleans all cache not in the group
139  *
140  * @return boolean True on success, false otherwise
141  *
142  * @since 11.1
143  */
144  public function clean($group, $mode = null)
145  {
146  $allinfo = wincache_ucache_info();
147  $keys = $allinfo['cache_entries'];
148  $secret = $this->_hash;
149 
150  foreach ($keys as $key)
151  {
152  if (strpos($key['key_name'], $secret . '-cache-' . $group . '-') === 0 xor $mode != 'group')
153  {
154  wincache_ucache_delete($key['key_name']);
155  }
156  }
157  return true;
158  }
159 
160  /**
161  * Force garbage collect expired cache data as items are removed only on get/add/delete/info etc
162  *
163  * @return boolean True on success, false otherwise.
164  *
165  * @since 11.1
166  */
167  public function gc()
168  {
169  $allinfo = wincache_ucache_info();
170  $keys = $allinfo['cache_entries'];
171  $secret = $this->_hash;
172 
173  foreach ($keys as $key)
174  {
175  if (strpos($key['key_name'], $secret . '-cache-'))
176  {
177  wincache_ucache_get($key['key_name']);
178  }
179  }
180  }
181 
182  /**
183  * Test to see if the cache storage is available.
184  *
185  * @return boolean True on success, false otherwise.
186  *
187  * @since 12.1
188  */
189  public static function isSupported()
190  {
191  $test = extension_loaded('wincache') && function_exists('wincache_ucache_get') && !strcmp(ini_get('wincache.ucenabled'), '1');
192  return $test;
193  }
194 }