Passed
Pull Request — release-2.1 (#5969)
by John
05:14
created

memcached_cache::isSupported()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
c 0
b 0
f 0
nop 1
dl 0
loc 9
rs 10
nc 4
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
 * Our Cache API class
19
 *
20
 * @package cacheAPI
21
 */
22
class memcached_cache extends cache_api
23
{
24
	/** @var Memcached The memcache instance. */
25
	private $memcached = null;
26
27
	/** @var string[] */
28
	private $servers;
29
30
	/**
31
	 * {@inheritDoc}
32
	 */
33
	public function __construct()
34
	{
35
		global $cache_memcached;
36
37
		$this->servers = explode(',', $cache_memcached);
38
39
		parent::__construct();
40
	}
41
42
	/**
43
	 * {@inheritDoc}
44
	 */
45
	public function isSupported($test = false)
46
	{
47
		global $cache_memcached;
48
49
		$supported = class_exists('Memcached');
50
51
		if ($test)
52
			return $supported;
53
		return parent::isSupported() && $supported && !empty($cache_memcached);
54
	}
55
56
	/**
57
	 * {@inheritDoc}
58
	 */
59
	public function connect()
60
	{
61
		$this->memcached = new Memcached;
62
63
		return $this->addServers();
64
	}
65
66
	/**
67
	 * Add memcached servers.
68
	 *
69
	 * Don't add servers if they already exist. Ideal for persistent connections.
70
	 *
71
	 * @return bool True if there are servers in the daemon, false if not.
72
	 */
73
	protected function addServers()
74
	{
75
		// memcached does not remove servers from the list upon completing the
76
		// script under modes like FastCGI. So check to see if servers exist or not.
77
		$currentServers = $this->memcached->getServerList();
78
		$retVal = !empty($currentServers);
79
		foreach ($this->servers as $server)
80
		{
81
			$tempServer = explode(':', trim($server));
82
			$tempServer[1] = !empty($tempServer[1]) ? $tempServer[1] : 11211;
83
84
			// Figure out if we have this server or not
85
			$foundServer = false;
86
			foreach ($currentServers as $currentServer)
87
			{
88
				if ($tempServer[0] == $currentServer['host'] && $tempServer[1] == $currentServer['port'])
89
				{
90
					$foundServer = true;
91
					break;
92
				}
93
			}
94
95
			// Found it?
96
			if (empty($foundServer))
97
				$retVal |= $this->memcached->addServer($tempServer[0], $tempServer[1]);
98
		}
99
100
		return $retVal;
101
	}
102
103
	/**
104
	 * {@inheritDoc}
105
	 */
106
	public function getData($key, $ttl = null)
107
	{
108
		$key = $this->prefix . strtr($key, ':/', '-_');
109
110
		$value = $this->memcached->get($key);
111
112
		// $value should return either data or false (from failure, key not found or empty array).
113
		if ($value === false)
114
			return null;
115
		return $value;
116
	}
117
118
	/**
119
	 * {@inheritDoc}
120
	 */
121
	public function putData($key, $value, $ttl = null)
122
	{
123
		$key = $this->prefix . strtr($key, ':/', '-_');
124
125
		return $this->memcached->set($key, $value, $ttl !== null ? $ttl : $this->ttl);
126
	}
127
128
	/**
129
	 * {@inheritDoc}
130
	 */
131
	public function cleanCache($type = '')
132
	{
133
		$this->invalidateCache();
134
135
		// Memcached accepts a delay parameter, always use 0 (instant).
136
		return $this->memcached->flush(0);
137
	}
138
139
	/**
140
	 * {@inheritDoc}
141
	 */
142
	public function quit()
143
	{
144
		return $this->memcached->quit();
145
	}
146
147
	/**
148
	 * {@inheritDoc}
149
	 */
150
	public function cacheSettings(array &$config_vars)
151
	{
152
		global $context, $txt;
153
154
		$config_vars[] = $txt['cache_memcache_settings'];
155
		$config_vars[] = array('cache_memcached', $txt['cache_memcache_servers'], 'file', 'text', 0, 'cache_memcached', 'postinput' => '<br><div class="smalltext"><em>' . $txt['cache_memcache_servers_subtext'] . '</em></div>');
156
157
		if (!isset($context['settings_post_javascript']))
158
			$context['settings_post_javascript'] = '';
159
160
		$context['settings_post_javascript'] .= '
161
			$("#cache_accelerator").change(function (e) {
162
				var cache_type = e.currentTarget.value;
163
				$("#cache_memcached").prop("disabled", cache_type != "memcached");
164
			});';
165
	}
166
167
	/**
168
	 * {@inheritDoc}
169
	 */
170
	public function getVersion()
171
	{
172
		return current($this->memcached->getVersion());
173
	}
174
}
175
176
?>