|
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) |
|
|
|
|
|
|
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 |
|
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) |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
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
|
|
|
* {@inheritDoc} |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getVersion() |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->memcached->getVersion(); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
?> |
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.