Completed
Push — release-2.1 ( aa21c4...7040ad )
by Mathias
09:20
created

xcache_cache::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
rs 9.4285
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 2017 Simple Machines and individual contributors
9
 * @license http://www.simplemachines.org/about/smf/license.php BSD
10
 *
11
 * @version 2.1 Beta 4
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
		$supported = function_exists('xcache_get') && function_exists('xcache_set') && ini_get('xcache.var_size') > 0;
46
47
		if ($test)
48
			return $supported;
49
		return parent::isSupported() && $supported;
50
	}
51
52
	/**
53
	 * {@inheritDoc}
54
	 */
55
	public function getData($key, $ttl = null)
56
	{
57
		$key = $this->prefix . strtr($key, ':/', '-_');
58
59
		return xcache_get($key);
60
	}
61
62
	/**
63
	 * {@inheritDoc}
64
	 */
65
	public function putData($key, $value, $ttl = null)
66
	{
67
		$key = $this->prefix . strtr($key, ':/', '-_');
68
69
		if ($value === null)
70
			return xcache_unset($key);
71
		else
72
			return xcache_set($key, $value, $ttl);
73
	}
74
75
	/**
76
	 * {@inheritDoc}
77
	 */
78
	public function cleanCache($type = '')
79
	{
80
		global $modSettings;
81
82
		// 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.
83
		if (empty($modSettings['xcache_adminuser']) || empty($modSettings['xcache_adminpass']))
84
		{
85
			// We are going to at least invalidate it.
86
			$this->invalidateCache();
87
			return false;
88
		}
89
90
		// if passed a type, clear that type out
91
		if ($type === '' || $type === 'user')
92
			xcache_clear_cache(XC_TYPE_VAR, 0);
93
		if ($type === '' || $type === 'data')
94
			xcache_clear_cache(XC_TYPE_PHP, 0);
95
96
		$this->invalidateCache();
97
		return true;
98
	}
99
100
	/**
101
	 * {@inheritDoc}
102
	 */
103
	public function cacheSettings(array &$config_vars)
104
	{
105
		global $context, $txt;
106
107
		$config_vars[] = $txt['cache_xcache_settings'];
108
		$config_vars[] = array('xcache_adminuser', $txt['cache_xcache_adminuser'], 'db', 'text', 0, 'xcache_adminuser');
109
110
		// While we could md5 this when saving, this could be tricky to be sure it doesn't get corrupted on additional saves.
111
		$config_vars[] = array('xcache_adminpass', $txt['cache_xcache_adminpass'], 'db', 'text', 0);
112
113
		if (!isset($context['settings_post_javascript']))
114
			$context['settings_post_javascript'] = '';
115
116
		$context['settings_post_javascript'] .= '
117
			$("#cache_accelerator").change(function (e) {
118
				var cache_type = e.currentTarget.value;
119
				$("#xcache_adminuser").prop("disabled", cache_type != "xcache");
120
				$("#xcache_adminpass").prop("disabled", cache_type != "xcache");
121
			});';
122
	}
123
}
124
125
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...