|
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 memcache_cache extends cache_api |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var \Memcache The memcache instance. |
|
25
|
|
|
*/ |
|
26
|
|
|
private $memcache = 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('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(); |
|
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 |
|
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) |
|
|
|
|
|
|
87
|
|
|
{ |
|
88
|
|
|
$key = $this->prefix . strtr($key, ':/', '-_'); |
|
89
|
|
|
|
|
90
|
|
|
$value = $this->memcache->get($key); |
|
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); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritDoc} |
|
110
|
|
|
*/ |
|
111
|
|
|
public function quit() |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->memcache->close(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritDoc} |
|
118
|
|
|
*/ |
|
119
|
|
|
public function cleanCache($type = '') |
|
120
|
|
|
{ |
|
121
|
|
|
$this->invalidateCache(); |
|
122
|
|
|
return $this->memcache->flush(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* {@inheritDoc} |
|
127
|
|
|
*/ |
|
128
|
|
View Code Duplication |
public function cacheSettings(array &$config_vars) |
|
|
|
|
|
|
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
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* {@inheritDoc} |
|
147
|
|
|
*/ |
|
148
|
|
|
public function getVersion() |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->memcache->getVersion(); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
?> |
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.