Passed
Pull Request — release-2.1 (#6262)
by Jeremy
04:04
created

cache_api   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 163
rs 10
c 0
b 0
f 0
wmc 19

14 Methods

Rating   Name   Duplication   Size   Complexity  
A isSupported() 0 7 2
A housekeeping() 0 2 1
A setDefaultTTL() 0 5 1
A setPrefix() 0 19 4
A getMinimumVersion() 0 3 1
A getVersion() 0 3 1
A getCompatibleVersion() 0 3 1
A __construct() 0 3 1
A cacheSettings() 0 2 1
A getDefaultTTL() 0 3 1
A getPrefix() 0 3 1
A invalidateCache() 0 10 2
A connect() 0 3 1
A quit() 0 3 1
1
<?php
2
3
/**
4
 * Simple Machines Forum (SMF)
5
 *
6
 * @package SMF
7
 * @author Simple Machines https://www.simplemachines.org
8
 * @copyright 2020 Simple Machines and individual contributors
9
 * @license https://www.simplemachines.org/about/smf/license.php BSD
10
 *
11
 * @version 2.1 RC3
12
 */
13
14
if (!defined('SMF'))
15
	die('Hacking attempt...');
16
17
/**
18
 * Interface cache_api_interface
19
 */
20
interface cache_api_interface
21
{
22
	/**
23
	 * Checks whether we can use the cache method performed by this API.
24
	 *
25
	 * @access public
26
	 * @param bool $test Test if this is supported or enabled.
27
	 * @return bool Whether or not the cache is supported
28
	 */
29
	public function isSupported($test = false);
30
31
	/**
32
	 * Connects to the cache method. This defines our $key. If this fails, we return false, otherwise we return true.
33
	 *
34
	 * @access public
35
	 * @return bool Whether or not the cache method was connected to.
36
	 */
37
	public function connect();
38
39
	/**
40
	 * Overrides the default prefix. If left alone, this will use the default key defined in the class.
41
	 *
42
	 * @access public
43
	 * @param string $key The key to use
44
	 * @return bool If this was successful or not.
45
	 */
46
	public function setPrefix($key = '');
47
48
	/**
49
	 * Gets the prefix as defined from set or the default.
50
	 *
51
	 * @access public
52
	 * @return string the value of $key.
53
	 */
54
	public function getPrefix();
55
56
	/**
57
	 * Sets a default Time To Live, if this isn't specified we let the class define it.
58
	 *
59
	 * @access public
60
	 * @param int $ttl The default TTL
61
	 * @return bool If this was successful or not.
62
	 */
63
	public function setDefaultTTL($ttl = 120);
64
65
	/**
66
	 * Gets the TTL as defined from set or the default.
67
	 *
68
	 * @access public
69
	 * @return int the value of $ttl.
70
	 */
71
	public function getDefaultTTL();
72
73
	/**
74
	 * Retrieves an item from the cache.
75
	 *
76
	 * @access public
77
	 * @param string $key The key to use, the prefix is applied to the key name.
78
	 * @param int    $ttl Overrides the default TTL. Not really used anymore,
79
	 *                    but is kept for backwards compatibility.
80
	 * @return mixed The result from the cache, if there is no data or it is invalid, we return null.
81
	 * @todo Seperate existence checking into its own method
82
	 */
83
	public function getData($key, $ttl = null);
84
85
	/**
86
	 * Stores a value, regardless of whether or not the key already exists (in
87
	 * which case it will overwrite the existing value for that key).
88
	 *
89
	 * @access public
90
	 * @param string $key   The key to use, the prefix is applied to the key name.
91
	 * @param mixed  $value The data we wish to save. Use null to delete.
92
	 * @param int    $ttl   How long (in seconds) the data should be cached for.
93
	 *                      The default TTL will be used if this is null.
94
	 * @return bool Whether or not we could save this to the cache.
95
	 * @todo Seperate deletion into its own method
96
	 */
97
	public function putData($key, $value, $ttl = null);
98
99
	/**
100
	 * Clean out the cache.
101
	 *
102
	 * @param string $type If supported, the type of cache to clear, blank/data or user.
103
	 * @return bool Whether or not we could clean the cache.
104
	 */
105
	public function cleanCache($type = '');
106
107
	/**
108
	 * Invalidate all cached data.
109
	 *
110
	 * @return bool Whether or not we could invalidate the cache.
111
	 */
112
	public function invalidateCache();
113
114
	/**
115
	 * Closes connections to the cache method.
116
	 *
117
	 * @access public
118
	 * @return bool Whether the connections were closed.
119
	 */
120
	public function quit();
121
122
	/**
123
	 * Specify custom settings that the cache API supports.
124
	 *
125
	 * @access public
126
	 * @param array $config_vars Additional config_vars, see ManageSettings.php for usage.
127
	 */
128
	public function cacheSettings(array &$config_vars);
129
130
	/**
131
	 * Gets the latest version of SMF this is compatible with.
132
	 *
133
	 * @access public
134
	 * @return string the value of $key.
135
	 */
136
	public function getCompatibleVersion();
137
138
	/**
139
	 * Gets the min version that we support.
140
	 *
141
	 * @access public
142
	 * @return string the value of $key.
143
	 */
144
	public function getMinimumVersion();
145
146
	/**
147
	 * Gets the Version of the Caching API.
148
	 *
149
	 * @access public
150
	 * @return string the value of $key.
151
	 */
152
	public function getVersion();
153
154
	/**
155
	 * Run housekeeping of this cache
156
	 * exp. clean up old data or do optimization
157
	 *
158
	 * @access public
159
	 * @return void
160
	 */
161
	public function housekeeping();
162
}
163
164
/**
165
 * Class cache_api
166
 */
167
abstract class cache_api implements cache_api_interface
168
{
169
	/**
170
	 * @var string The maximum SMF version that this will work with.
171
	 */
172
	protected $version_compatible = '2.1.999';
173
174
	/**
175
	 * @var string The minimum SMF version that this will work with.
176
	 */
177
	protected $min_smf_version = '2.1 RC1';
178
179
	/**
180
	 * @var string The prefix for all keys.
181
	 */
182
	protected $prefix = '';
183
184
	/**
185
	 * @var int The default TTL.
186
	 */
187
	protected $ttl = 120;
188
189
	/**
190
	 * Does basic setup of a cache method when we create the object but before we call connect.
191
	 *
192
	 * @access public
193
	 */
194
	public function __construct()
195
	{
196
		$this->setPrefix();
197
	}
198
199
	/**
200
	 * {@inheritDoc}
201
	 */
202
	public function isSupported($test = false)
203
	{
204
		global $cache_enable;
205
206
		if ($test)
207
			return true;
208
		return !empty($cache_enable);
209
	}
210
211
	/**
212
	 * {@inheritDoc}
213
	 */
214
	public function connect()
215
	{
216
		return true;
217
	}
218
219
	/**
220
	 * {@inheritDoc}
221
	 */
222
	public function setPrefix($prefix = '')
223
	{
224
		global $boardurl, $cachedir;
225
226
		// Find a valid good file to do mtime checks on.
227
		if (file_exists($cachedir . '/' . 'index.php'))
228
			$filemtime = $cachedir . '/' . 'index.php';
229
		elseif (is_dir($cachedir . '/'))
230
			$filemtime = $cachedir . '/';
231
		else
232
			$filemtime = $boardurl . '/index.php';
233
234
		// Set the default if no prefix was specified.
235
		if (empty($prefix))
236
			$this->prefix = md5($boardurl . filemtime($filemtime)) . '-SMF-';
237
		else
238
			$this->prefix = $prefix;
239
240
		return true;
241
	}
242
243
	/**
244
	 * {@inheritDoc}
245
	 */
246
	public function getPrefix()
247
	{
248
		return $this->prefix;
249
	}
250
251
	/**
252
	 * {@inheritDoc}
253
	 */
254
	public function setDefaultTTL($ttl = 120)
255
	{
256
		$this->ttl = $ttl;
257
258
		return true;
259
	}
260
261
	/**
262
	 * {@inheritDoc}
263
	 */
264
	public function getDefaultTTL()
265
	{
266
		return $this->ttl;
267
	}
268
269
	/**
270
	 * Invalidate all cached data.
271
	 *
272
	 * @return bool Whether or not we could invalidate the cache.
273
	 */
274
	public function invalidateCache()
275
	{
276
		global $cachedir;
277
278
		// Invalidate cache, to be sure!
279
		// ... as long as index.php can be modified, anyway.
280
		if (is_writable($cachedir . '/' . 'index.php'))
281
			@touch($cachedir . '/' . 'index.php');
282
283
		return true;
284
	}
285
286
	/**
287
	 * {@inheritDoc}
288
	 */
289
	public function quit()
290
	{
291
		return true;
292
	}
293
294
	/**
295
	 * {@inheritDoc}
296
	 */
297
	public function cacheSettings(array &$config_vars)
298
	{
299
	}
300
301
	/**
302
	 * {@inheritDoc}
303
	 */
304
	public function getCompatibleVersion()
305
	{
306
		return $this->version_compatible;
307
	}
308
309
	/**
310
	 * {@inheritDoc}
311
	 */
312
	public function getMinimumVersion()
313
	{
314
		return $this->min_smf_version;
315
	}
316
317
	/**
318
	 * {@inheritDoc}
319
	 */
320
	public function getVersion()
321
	{
322
		return $this->min_smf_version;
323
	}
324
325
	/**
326
	 * {@inheritDoc}
327
	 */
328
	public function housekeeping()
329
	{
330
	}
331
}
332
333
?>