1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Simple Machines Forum (SMF) |
5
|
|
|
* |
6
|
|
|
* @package SMF |
7
|
|
|
* @author Simple Machines http://www.simplemachines.org |
8
|
|
|
* @copyright 2019 Simple Machines and individual contributors |
9
|
|
|
* @license http://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 memcache_cache extends cache_api |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var \Memcache The memcache instance. |
26
|
|
|
*/ |
27
|
|
|
private $memcache = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritDoc} |
31
|
|
|
*/ |
32
|
|
|
public function isSupported($test = false) |
33
|
|
|
{ |
34
|
|
|
global $cache_memcached; |
35
|
|
|
|
36
|
|
|
$supported = class_exists('memcache'); |
37
|
|
|
|
38
|
|
|
if ($test) |
39
|
|
|
return $supported; |
40
|
|
|
return parent::isSupported() && $supported && !empty($cache_memcached); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritDoc} |
45
|
|
|
*/ |
46
|
|
|
public function connect() |
47
|
|
|
{ |
48
|
|
|
global $db_persist, $cache_memcached; |
49
|
|
|
|
50
|
|
|
$servers = explode(',', $cache_memcached); |
51
|
|
|
$port = 0; |
52
|
|
|
|
53
|
|
|
// Don't try more times than we have servers! |
54
|
|
|
$connected = false; |
55
|
|
|
$level = 0; |
56
|
|
|
|
57
|
|
|
// We should keep trying if a server times out, but only for the amount of servers we have. |
58
|
|
|
while (!$connected && $level < count($servers)) |
59
|
|
|
{ |
60
|
|
|
++$level; |
61
|
|
|
$this->memcache = new Memcache(); |
62
|
|
|
$server = trim($servers[array_rand($servers)]); |
63
|
|
|
|
64
|
|
|
// Normal host names do not contain slashes, while e.g. unix sockets do. Assume alternative transport pipe with port 0. |
65
|
|
|
if (strpos($server, '/') !== false) |
66
|
|
|
$host = $server; |
67
|
|
|
else |
68
|
|
|
{ |
69
|
|
|
$server = explode(':', $server); |
70
|
|
|
$host = $server[0]; |
71
|
|
|
$port = isset($server[1]) ? $server[1] : 11211; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Don't wait too long: yes, we want the server, but we might be able to run the query faster! |
75
|
|
|
if (empty($db_persist)) |
76
|
|
|
$connected = $this->memcache->connect($host, $port); |
|
|
|
|
77
|
|
|
else |
78
|
|
|
$connected = $this->memcache->pconnect($host, $port); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $connected; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritDoc} |
86
|
|
|
*/ |
87
|
|
|
public function getData($key, $ttl = null) |
88
|
|
|
{ |
89
|
|
|
$key = $this->prefix . strtr($key, ':/', '-_'); |
90
|
|
|
|
91
|
|
|
$value = $this->memcache->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->memcache->set($key, $value, 0, $ttl); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritDoc} |
111
|
|
|
*/ |
112
|
|
|
public function quit() |
113
|
|
|
{ |
114
|
|
|
return $this->memcache->close(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritDoc} |
119
|
|
|
*/ |
120
|
|
|
public function cleanCache($type = '') |
121
|
|
|
{ |
122
|
|
|
$this->invalidateCache(); |
123
|
|
|
return $this->memcache->flush(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritDoc} |
128
|
|
|
*/ |
129
|
|
|
public function cacheSettings(array &$config_vars) |
130
|
|
|
{ |
131
|
|
|
global $context, $txt; |
132
|
|
|
|
133
|
|
|
$config_vars[] = $txt['cache_memcache_settings']; |
134
|
|
|
$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>'); |
135
|
|
|
|
136
|
|
|
if (!isset($context['settings_post_javascript'])) |
137
|
|
|
$context['settings_post_javascript'] = ''; |
138
|
|
|
|
139
|
|
|
$context['settings_post_javascript'] .= ' |
140
|
|
|
$("#cache_accelerator").change(function (e) { |
141
|
|
|
var cache_type = e.currentTarget.value; |
142
|
|
|
$("#cache_memcached").prop("disabled", cache_type != "memcache"); |
143
|
|
|
});'; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritDoc} |
148
|
|
|
*/ |
149
|
|
|
public function getVersion() |
150
|
|
|
{ |
151
|
|
|
return $this->memcache->getVersion(); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
?> |