Completed
Pull Request — patch_1-1-4 (#3202)
by Spuds
15:49
created

Cache.subs.php ➔ clean_cache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file contains functions that deal with getting and setting cache values.
5
 *
6
 * @name      ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
9
 *
10
 * This file contains code covered by:
11
 * copyright:	2011 Simple Machines (http://www.simplemachines.org)
12
 * license:  	BSD, See included LICENSE.TXT for terms and conditions.
13
 *
14
 * @version 1.1
15
 *
16
 */
17
18
/**
19
 * Try to retrieve a cache entry. On failure, call the appropriate function.
20
 * This callback is sent as $file to include, and $function to call, with
21
 * $params parameters.
22
 *
23
 * @param string $key cache entry key
24
 * @param string $file file to include
25
 * @param string $function function to call
26
 * @param mixed[] $params parameters sent to the function
27
 * @param int $level = 1
28
 *
29
 * @return mixed
30
 */
31
function cache_quick_get($key, $file, $function, $params, $level = 1)
32
{
33
	return Cache::instance()->quick_get($key, $file, $function, $params, $level);
34
}
35
36
/**
37
 * Puts value in the cache under key for ttl seconds.
38
 *
39
 * - It may "miss" so shouldn't be depended on
40
 * - Uses the cache engine chosen in the ACP and saved in settings.php
41
 * - It supports:
42
 *     Xcache: http://xcache.lighttpd.net/wiki/XcacheApi
43
 *     memcache: http://www.php.net/memcache
44
 *     APC: http://www.php.net/apc
45
 *     Zend: http://files.zend.com/help/Zend-Platform/zend_cache_functions.htm
46
 *
47
 * @param string $key
48
 * @param string|int|mixed[]|null $value
49
 * @param int $ttl = 120
50
 */
51
function cache_put_data($key, $value, $ttl = 120)
52
{
53
	Cache::instance()->put($key, $value, $ttl);
54
}
55
56
/**
57
 * Gets the value from the cache specified by key, so long as it is not older than ttl seconds.
58
 *
59
 * - It may often "miss", so shouldn't be depended on.
60
 * - It supports the same as Cache::instance()->put().
61
 *
62
 * @param string $key
63
 * @param int $ttl = 120
64
 */
65
function cache_get_data($key, $ttl = 120)
66
{
67
	return Cache::instance()->get($key, $ttl);
68
}
69
70
/**
71
 * Empty out the cache in use as best it can
72
 *
73
 * It may only remove the files of a certain type (if the $type parameter is given)
74
 * Type can be user, data or left blank
75
 *  - user clears out user data
76
 *  - data clears out system / opcode data
77
 *  - If no type is specified will perform a complete cache clearing
78
 * For cache engines that do not distinguish on types, a full cache flush will be done
79
 *
80
 * @param string $type = ''
81
 */
82
function clean_cache($type = '')
83
{
84 1
	Cache::instance()->clean($type);
85 1
}
86
87
/**
88
 * Finds all the caching engines available and loads some details depending on
89
 * parameters.
90
 *
91
 * - Caching engines must follow the naming convention of XyzCache.class.php and
92
 * have a class name of Xyz_Cache
93
 *
94
 * @param bool $supported_only If true, for each engine supported by the server
95
 *             an array with 'title' and 'version' is returned.
96
 *             If false, for each engine available an array with 'title' (string)
97
 *             and 'supported' (bool) is returned.
98
 *
99
 * @return mixed[]
100
 */
101
function loadCacheEngines($supported_only = true)
102
{
103 1
	$engines = array();
104
105 1
	$classes = new GlobIterator(SUBSDIR . '/CacheMethod/*.php', FilesystemIterator::SKIP_DOTS);
106
107 1
	foreach ($classes as $file_path)
108
	{
109
		// Get the engine name from the file name
110 1
		$parts = explode('.', $file_path->getBasename());
111 1
		$engine_name = $parts[0];
112 1
		$class = '\\ElkArte\\sources\\subs\\CacheMethod\\' . $parts[0];
113
114
		// Validate the class name exists
115 1
		if (class_exists($class))
116 1
		{
117 1
			$obj = new $class(array());
118 1
			if ($obj instanceof ElkArte\sources\subs\CacheMethod\Cache_Method_Abstract)
119 1
			{
120 1
				if ($supported_only && $obj->isAvailable())
121 1
				{
122
					$engines[strtolower($engine_name)] = $obj->details();
123
				}
124 1
				elseif ($supported_only === false)
125
				{
126 1
					$engines[strtolower($engine_name)] = $obj;
127 1
				}
128 1
			}
129 1
		}
130 1
	}
131
132 1
	return $engines;
133
}
134