1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Cache
5 *
6 * @copyright Copyright (C) 2005 - 2017 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 * @link https://secure.php.net/manual/en/book.wincache.php
16 * @since 11.1
17 */
18 class JCacheStorageWincache extends JCacheStorage
19 {
20 /**
21 * Check if the cache contains data stored by ID and group
22 *
23 * @param string $id The cache data ID
24 * @param string $group The cache data group
25 *
26 * @return boolean
27 *
28 * @since 3.7.0
29 */
30 public function contains($id, $group)
31 {
32 return wincache_ucache_exists($this->_getCacheId($id, $group));
33 }
34
35 /**
36 * Get cached data by ID and group
37 *
38 * @param string $id The cache data ID
39 * @param string $group The cache data group
40 * @param boolean $checkTime True to verify cache time expiration threshold
41 *
42 * @return mixed Boolean false on failure or a cached data object
43 *
44 * @since 11.1
45 */
46 public function get($id, $group, $checkTime = true)
47 {
48 return wincache_ucache_get($this->_getCacheId($id, $group));
49 }
50
51 /**
52 * Get all cached data
53 *
54 * @return mixed Boolean false on failure or a cached data object
55 *
56 * @since 11.1
57 */
58 public function getAll()
59 {
60 $allinfo = wincache_ucache_info();
61 $keys = $allinfo['ucache_entries'];
62 $secret = $this->_hash;
63 $data = array();
64
65 foreach ($keys as $key)
66 {
67 $name = $key['key_name'];
68 $namearr = explode('-', $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 if (isset($key['value_size']))
84 {
85 $item->updateSize($key['value_size']);
86 }
87 else
88 {
89 // Dummy, WINCACHE version is too low.
90 $item->updateSize(1);
91 }
92
93 $data[$group] = $item;
94 }
95 }
96
97 return $data;
98 }
99
100 /**
101 * Store the data to cache by ID and group
102 *
103 * @param string $id The cache data ID
104 * @param string $group The cache data group
105 * @param string $data The data to store in cache
106 *
107 * @return boolean
108 *
109 * @since 11.1
110 */
111 public function store($id, $group, $data)
112 {
113 return wincache_ucache_set($this->_getCacheId($id, $group), $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
123 *
124 * @since 11.1
125 */
126 public function remove($id, $group)
127 {
128 return wincache_ucache_delete($this->_getCacheId($id, $group));
129 }
130
131 /**
132 * Clean cache for a group given a mode.
133 *
134 * group mode : cleans all cache in the group
135 * notgroup mode : cleans all cache not in the group
136 *
137 * @param string $group The cache data group
138 * @param string $mode The mode for cleaning cache [group|notgroup]
139 *
140 * @return boolean
141 *
142 * @since 11.1
143 */
144 public function clean($group, $mode = null)
145 {
146 $allinfo = wincache_ucache_info();
147 $keys = $allinfo['ucache_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
158 return true;
159 }
160
161 /**
162 * Garbage collect expired cache data
163 *
164 * @return boolean
165 *
166 * @since 11.1
167 */
168 public function gc()
169 {
170 $allinfo = wincache_ucache_info();
171 $keys = $allinfo['ucache_entries'];
172 $secret = $this->_hash;
173
174 foreach ($keys as $key)
175 {
176 if (strpos($key['key_name'], $secret . '-cache-'))
177 {
178 wincache_ucache_get($key['key_name']);
179 }
180 }
181
182 return true;
183 }
184
185 /**
186 * Test to see if the storage handler is available.
187 *
188 * @return boolean
189 *
190 * @since 12.1
191 */
192 public static function isSupported()
193 {
194 return extension_loaded('wincache') && function_exists('wincache_ucache_get') && !strcmp(ini_get('wincache.ucenabled'), '1');
195 }
196 }
197