-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemcacheSessionManager.php
More file actions
88 lines (79 loc) · 2.1 KB
/
MemcacheSessionManager.php
File metadata and controls
88 lines (79 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
require_once 'BaseSessionManager.php';
/**
* Class for accessing to memcache session storage
*/
class MemcacheSessionManager extends BaseSessionManager
{
private $host;
private $port;
private $conn;
public function connect(array $params)
{
if (empty($params['host'])) {
throw new InvalidArgumentException('host not found');
}
if (empty($params['port'])) {
throw new InvalidArgumentException('port not found');
}
$this->host = $params['host'];
$this->port = $params['port'];
$this->conn = new Memcached();
$this->conn->addServer($this->host, $this->port);
}
public function disconnect()
{
unset($this->conn);
}
public function get($key)
{
$key = strval($key);
if (empty($key)) {
throw new InvalidArgumentException('key is empty');
}
$full_key = $this->getPrefix() . $key;
return $this->conn->get($full_key);
}
public function set($key, $value)
{
$key = strval($key);
if (empty($key)) {
throw new InvalidArgumentException('key is empty');
}
$full_key = $this->getPrefix() . $key;
$this->conn->set($full_key, $value);
}
public function delete($key)
{
$key = strval($key);
if (empty($key)) {
throw new InvalidArgumentException('key is empty');
}
$full_key = $this->getPrefix() . $key;
$this->conn->delete($full_key);
}
public function deleteAll()
{
$keys = $this->getAllKeys();
$count = 0;
foreach ($keys as $key) {
$this->delete($key);
$count++;
}
return $count;
}
public function getAllKeys()
{
$keys = $this->conn->getAllKeys();
if (!$keys) {
return array();
}
$ret = array();
foreach ($keys as $k) {
if (starts_with($k, $this->getPrefix())) {
$ret[] = str_replace($this->getPrefix(), '', $k);
}
}
return $ret;
}
}