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

memcache_cache::isSupported()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 4
nop 1
dl 10
loc 10
rs 9.2
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 memcache_cache extends cache_api
22
{
23
	/**
24
	 * @var string The memcache instance.
25
	 */
26
	private $memcache = 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('memcache');
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 $db_persist, $cache_memcached;
48
49
		$servers = explode(',', $cache_memcached);
50
		$port = 0;
51
52
		// Don't try more times than we have servers!
53
		$connected = false;
54
		$level = 0;
55
56
		// We should keep trying if a server times out, but only for the amount of servers we have.
57
		while (!$connected && $level < count($servers))
58
		{
59
			++$level;
60
			$this->memcache = new Memcache();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Memcache() of type object<Memcache> is incompatible with the declared type string of property $memcache.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
61
			$server = trim($servers[array_rand($servers)]);
62
63
			// Normal host names do not contain slashes, while e.g. unix sockets do. Assume alternative transport pipe with port 0.
64
			if (strpos($server,'/') !== false)
65
				$host = $server;
66 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...
67
			{
68
				$server = explode(':', $server);
69
				$host = $server[0];
70
				$port = isset($server[1]) ? $server[1] : 11211;
71
			}
72
73
			// Don't wait too long: yes, we want the server, but we might be able to run the query faster!
74
			if (empty($db_persist))
75
				$connected = $this->memcache->connect($host, $port);
76
			else
77
				$connected = $this->memcache->pconnect($host, $port);
78
		}
79
80
		return $connected;
81
	}
82
83
	/**
84
	 * {@inheritDoc}
85
	 */
86 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...
87
	{
88
		$key = $this->prefix . strtr($key, ':/', '-_');
89
90
		$value = $this->memcache->get($key);
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->memcache (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
91
92
		// $value should return either data or false (from failure, key not found or empty array).
93
		if ($value === false)
94
			return null;
95
		return $value;
96
	}
97
98
	/**
99
	 * {@inheritDoc}
100
	 */
101
	public function putData($key, $value, $ttl = null)
102
	{
103
		$key = $this->prefix . strtr($key, ':/', '-_');
104
105
		return $this->memcache->set($key, $value, 0, $ttl);
0 ignored issues
show
Bug introduced by
The method set cannot be called on $this->memcache (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
106
	}
107
108
	/**
109
	 * {@inheritDoc}
110
	 */
111
	public function quit()
112
	{
113
		return $this->memcache->close();
0 ignored issues
show
Bug introduced by
The method close cannot be called on $this->memcache (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
114
	}
115
116
	/**
117
	 * {@inheritDoc}
118
	 */
119
	public function cleanCache($type = '')
120
	{
121
		$this->invalidateCache();
122
		return $this->memcache->flush();
0 ignored issues
show
Bug introduced by
The method flush cannot be called on $this->memcache (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
123
	}
124
125
	/**
126
	 * {@inheritDoc}
127
	 */
128 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...
129
	{
130
		global $context, $txt;
131
132
		$config_vars[] = $txt['cache_memcache_settings'];
133
		$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>');
134
135
		if (!isset($context['settings_post_javascript']))
136
			$context['settings_post_javascript'] = '';
137
138
		$context['settings_post_javascript'] .= '
139
			$("#cache_accelerator").change(function (e) {
140
				var cache_type = e.currentTarget.value;
141
				$("#cache_memcached").prop("disabled", cache_type != "memcache");
142
			});';
143
	}
144
}