Completed
Branch release-2.1 (e49a83)
by Mert
04:10
created

xcache_cache::cleanCache()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 11
nc 5
nop 1
dl 0
loc 21
rs 7.551
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Simple Machines Forum (SMF)
5
 *
6
 * @package SMF
7
 * @author Simple Machines http://www.simplemachines.org
8
 * @copyright 2016 Simple Machines and individual contributors
9
 * @license http://www.simplemachines.org/about/smf/license.php BSD
10
 *
11
 * @version 2.1 Beta 3
12
 */
13
14
if (!defined('SMF'))
15
	die('Hacking attempt...');
16
17
/**
18
 * Our Cache API class
19
 * @package cacheAPI
20
 */
21
class xcache_cache extends cache_api
22
{
23
	/**
24
	 * {@inheritDoc}
25
	 */
26
	public function __construct()
27
	{
28
		global $modSettings;
29
30
		parent::__construct();
31
32
		// Xcache requuires a admin username and password in order to issue a clear.
33
		if (!empty($modSettings['xcache_adminuser']) && !empty($modSettings['xcache_adminpass']))
34
		{
35
			ini_set('xcache.admin.user', $modSettings['xcache_adminuser']);
36
			ini_set('xcache.admin.pass', md5($modSettings['xcache_adminpass']));
37
		}
38
	}
39
40
	/**
41
	 * {@inheritDoc}
42
	 */
43
	public function isSupported($test = false)
44
	{
45
		global $cache_memcached;
46
47
		$supported = function_exists('xcache_get') && function_exists('xcache_set') && ini_get('xcache.var_size') > 0;
48
49
		if ($test)
50
			return $supported;
51
		return parent::isSupported() && $supported;
52
	}
53
54
	/**
55
	 * {@inheritDoc}
56
	 */
57
	public function getData($key, $ttl = null)
58
	{
59
		$key = $this->prefix . strtr($key, ':/', '-_');
60
61
		return xcache_get($key);
62
	}
63
64
	/**
65
	 * {@inheritDoc}
66
	 */
67
	public function putData($key, $value, $ttl = null)
68
	{
69
		$key = $this->prefix . strtr($key, ':/', '-_');
70
71
		if ($value === null)
72
			return xcache_unset($key);
73
		else
74
			return xcache_set($key, $value, $ttl);
75
	}
76
77
	/**
78
	 * {@inheritDoc}
79
	 */
80
	public function cleanCache($type = '')
81
	{
82
		global $modSettings;
83
84
		// Xcache requuires a admin username and password in order to issue a clear. Ideally this would log an error, but it seems like something that could fill up the error log quickly.
85
		if (empty($modSettings['xcache_adminuser']) || empty($modSettings['xcache_adminpass']))
86
		{
87
			// We are going to at least invalidate it.
88
			$this->invalidateCache();
89
			return false;
90
		}
91
92
		// if passed a type, clear that type out
93
		if ($type === '' || $type === 'user')
94
			xcache_clear_cache(XC_TYPE_VAR, 0);
95
		if ($type === '' || $type === 'data')
96
			xcache_clear_cache(XC_TYPE_PHP, 0);
97
98
		$this->invalidateCache();
99
		return true;
100
	}
101
102
	/**
103
	 * {@inheritDoc}
104
	 */
105
	public function cacheSettings(array &$config_vars)
106
	{
107
		global $context, $txt;
108
109
		$config_vars[] = $txt['cache_xcache_settings'];
110
		$config_vars[] = array('xcache_adminuser', $txt['cache_xcache_adminuser'], 'db', 'text', 0, 'xcache_adminuser');
111
112
		// While we could md5 this when saving, this could be tricky to be sure it doesn't get corrupted on additional saves.
113
		$config_vars[] = array('xcache_adminpass', $txt['cache_xcache_adminpass'], 'db', 'text', 0);
114
115
		if (!isset($context['settings_post_javascript']))
116
			$context['settings_post_javascript'] = '';
117
118
		$context['settings_post_javascript'] .= '
119
			$("#cache_accelerator").change(function (e) {
120
				var cache_type = e.currentTarget.value;
121
				$("#xcache_adminuser").prop("disabled", cache_type != "xcache");
122
				$("#xcache_adminpass").prop("disabled", cache_type != "xcache");
123
			});';
124
	}
125
}