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

memcached_cache::cleanCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 7
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 memcached_cache extends cache_api
22
{
23
	/**
24
	 * @var \Memcached The memcache instance.
25
	 */
26
	private $memcached = null;
27
28
	/**
29
	 * {@inheritDoc}
30
	 */
31 View Code Duplication
	public function isSupported($test = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
	{
33
		global $cache_memcached;
34
35
		$supported = class_exists('memcached');
36
37
		if ($test)
38
			return $supported;
39
		return parent::isSupported() && $supported && !empty($cache_memcached);
40
	}
41
42
	/**
43
	 * {@inheritDoc}
44
	 */
45
	public function connect()
46
	{
47
		global $cache_memcached;
48
49
		$servers = explode(',', $cache_memcached);
50
51
		// memcached does not remove servers from the list upon completing the script under modes like FastCGI. So check to see if servers exist or not.
52
		$this->memcached = new Memcached;
53
		$currentServers = $this->memcached->getServerList();
54
		foreach ($servers as $server)
55
		{
56
			if (strpos($server,'/') !== false)
57
				$tempServer = array($server, 0);
58 View Code Duplication
			else
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
			{
60
				$server = explode(':', $server);
61
				$tempServer = array($server[0], isset($server[1]) ? $server[1] : 11211);
62
			}
63
64
			// Figure out if we have this server or not
65
			$foundServer = false;
66
			foreach ($currentServers as $currentServer)
67
			{
68
				if ($tempServer[0] == $currentServer['host'] && $tempServer[1] == $currentServer['port'])
69
				{
70
					$foundServer = true;
71
					break;
72
				}
73
			}
74
75
			// Found it?
76
			if (empty($foundServer))
77
				$this->memcached->addServer($tempServer[0], $tempServer[1]);
78
		}
79
80
		// Best guess is this worked.
81
		return true;
82
	}
83
84
	/**
85
	 * {@inheritDoc}
86
	 */
87 View Code Duplication
	public function getData($key, $ttl = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
	{
89
		$key = $this->prefix . strtr($key, ':/', '-_');
90
91
		$value = $this->memcached->get($key);
92
93
		// $value should return either data or false (from failure, key not found or empty array).
94
		if ($value === false)
95
			return null;
96
		return $value;
97
	}
98
99
	/**
100
	 * {@inheritDoc}
101
	 */
102
	public function putData($key, $value, $ttl = null)
103
	{
104
		$key = $this->prefix . strtr($key, ':/', '-_');
105
106
		return $this->memcached->set($key, $value, $ttl);
107
	}
108
109
	/**
110
	 * {@inheritDoc}
111
	 */
112
	public function cleanCache($type = '')
113
	{
114
		$this->invalidateCache();
115
116
		// Memcached accepts a delay parameter, always use 0 (instant).
117
		return $this->memcached->flush(0);
118
	}
119
120
	/**
121
	 * {@inheritDoc}
122
	 */
123
	public function quit()
124
	{
125
		return $this->memcached->quit();
126
	}
127
128
	/**
129
	 * {@inheritDoc}
130
	 */
131 View Code Duplication
	public function cacheSettings(array &$config_vars)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
	{
133
		global $context, $txt;
134
135
		$config_vars[] = $txt['cache_memcache_settings'];
136
		$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>');
137
138
		if (!isset($context['settings_post_javascript']))
139
			$context['settings_post_javascript'] = '';
140
141
		$context['settings_post_javascript'] .= '
142
			$("#cache_accelerator").change(function (e) {
143
				var cache_type = e.currentTarget.value;
144
				$("#cache_memcached").prop("disabled", cache_type != "memcached");
145
			});';
146
	}
147
}
148
149
?>
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...