Completed
Pull Request — release-2.1 (#5969)
by John
05:07
created

cache_api::housekeeping()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nop 0
dl 0
loc 2
rs 10
nc 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 RC2
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 string the value of $ttl.
70
	 */
71
	public function getDefaultTTL();
72
73
	/**
74
	 * Gets data 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 string $ttl Overrides the default TTL.
79
	 * @return mixed The result from the cache, if there is no data or it is invalid, we return null.
80
	 */
81
	public function getData($key, $ttl = null);
82
83
	/**
84
	 * Saves to data the cache.
85
	 *
86
	 * @access public
87
	 * @param string $key The key to use, the prefix is applied to the key name.
88
	 * @param mixed $value The data we wish to save.
89
	 * @param string $ttl Overrides the default TTL.
90
	 * @return bool Whether or not we could save this to the cache.
91
	 */
92
	public function putData($key, $value, $ttl = null);
93
94
	/**
95
	 * Clean out the cache.
96
	 *
97
	 * @param string $type If supported, the type of cache to clear, blank/data or user.
98
	 * @return bool Whether or not we could clean the cache.
99
	 */
100
	public function cleanCache($type = '');
101
102
	/**
103
	 * Invalidate all cached data.
104
	 *
105
	 * @return bool Whether or not we could invalidate the cache.
106
	 */
107
	public function invalidateCache();
108
109
	/**
110
	 * Closes connections to the cache method.
111
	 *
112
	 * @access public
113
	 * @return bool Whether the connections were closed.
114
	 */
115
	public function quit();
116
117
	/**
118
	 * Specify custom settings that the cache API supports.
119
	 *
120
	 * @access public
121
	 * @param array $config_vars Additional config_vars, see ManageSettings.php for usage.
122
	 */
123
	public function cacheSettings(array &$config_vars);
124
125
	/**
126
	 * Gets the latest version of SMF this is compatible with.
127
	 *
128
	 * @access public
129
	 * @return string the value of $key.
130
	 */
131
	public function getCompatibleVersion();
132
133
	/**
134
	 * Gets the min version that we support.
135
	 *
136
	 * @access public
137
	 * @return string the value of $key.
138
	 */
139
	public function getMinimumVersion();
140
141
	/**
142
	 * Gets the Version of the Caching API.
143
	 *
144
	 * @access public
145
	 * @return string the value of $key.
146
	 */
147
	public function getVersion();
148
149
	/**
150
	 * Run housekeeping of this cache
151
	 * exp. clean up old data or do optimization
152
	 *
153
	 * @access public
154
	 * @return void
155
	 */
156
	public function housekeeping();
157
}
158
159
/**
160
 * Class cache_api
161
 */
162
abstract class cache_api implements cache_api_interface
163
{
164
	/**
165
	 * @var string The maximum SMF version that this will work with.
166
	 */
167
	protected $version_compatible = '2.1.999';
168
169
	/**
170
	 * @var string The minimum SMF version that this will work with.
171
	 */
172
	protected $min_smf_version = '2.1 RC1';
173
174
	/**
175
	 * @var string The prefix for all keys.
176
	 */
177
	protected $prefix = '';
178
179
	/**
180
	 * @var int The default TTL.
181
	 */
182
	protected $ttl = 120;
183
184
	/**
185
	 * Does basic setup of a cache method when we create the object but before we call connect.
186
	 *
187
	 * @access public
188
	 */
189
	public function __construct()
190
	{
191
		$this->setPrefix();
192
	}
193
194
	/**
195
	 * {@inheritDoc}
196
	 */
197
	public function isSupported($test = false)
198
	{
199
		global $cache_enable;
200
201
		if ($test)
202
			return true;
203
		return !empty($cache_enable);
204
	}
205
206
	/**
207
	 * {@inheritDoc}
208
	 */
209
	public function connect()
210
	{
211
		return true;
212
	}
213
214
	/**
215
	 * {@inheritDoc}
216
	 */
217
	public function setPrefix($prefix = '')
218
	{
219
		global $boardurl, $cachedir;
220
221
		// Find a valid good file to do mtime checks on.
222
		if (file_exists($cachedir . '/' . 'index.php'))
223
			$filemtime = $cachedir . '/' . 'index.php';
224
		elseif (is_dir($cachedir . '/'))
225
			$filemtime = $cachedir . '/';
226
		else
227
			$filemtime = $boardurl . '/index.php';
228
229
		// Set the default if no prefix was specified.
230
		if (empty($prefix))
231
			$this->prefix = md5($boardurl . filemtime($filemtime)) . '-SMF-';
232
		else
233
			$this->prefix = $prefix;
234
235
		return true;
236
	}
237
238
	/**
239
	 * {@inheritDoc}
240
	 */
241
	public function getPrefix()
242
	{
243
		return $this->prefix;
244
	}
245
246
	/**
247
	 * {@inheritDoc}
248
	 */
249
	public function setDefaultTTL($ttl = 120)
250
	{
251
		$this->ttl = $ttl;
252
253
		return true;
254
	}
255
256
	/**
257
	 * {@inheritDoc}
258
	 */
259
	public function getDefaultTTL()
260
	{
261
		return $this->ttl;
262
	}
263
264
	/**
265
	 * Invalidate all cached data.
266
	 *
267
	 * @return bool Whether or not we could invalidate the cache.
268
	 */
269
	public function invalidateCache()
270
	{
271
		global $cachedir;
272
273
		// Invalidate cache, to be sure!
274
		// ... as long as index.php can be modified, anyway.
275
		if (is_writable($cachedir . '/' . 'index.php'))
276
			@touch($cachedir . '/' . 'index.php');
277
278
		return true;
279
	}
280
281
	/**
282
	 * {@inheritDoc}
283
	 */
284
	public function quit()
285
	{
286
		return true;
287
	}
288
289
	/**
290
	 * {@inheritDoc}
291
	 */
292
	public function cacheSettings(array &$config_vars)
293
	{
294
	}
295
296
	/**
297
	 * {@inheritDoc}
298
	 */
299
	public function getCompatibleVersion()
300
	{
301
		return $this->version_compatible;
302
	}
303
304
	/**
305
	 * {@inheritDoc}
306
	 */
307
	public function getMinimumVersion()
308
	{
309
		return $this->min_smf_version;
310
	}
311
312
	/**
313
	 * {@inheritDoc}
314
	 */
315
	public function getVersion()
316
	{
317
		return $this->min_smf_version;
318
	}
319
320
	/**
321
	 * {@inheritDoc}
322
	 */
323
	public function housekeeping()
324
	{
325
	}
326
}
327
328
?>