Repository navigation
Expand file tree
/
Copy pathMemcachedHandler.php
More file actions
executable file
·80 lines (70 loc) · 2.19 KB
/
Copy pathMemcachedHandler.php
File metadata and controls
executable file
·80 lines (70 loc) · 2.19 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
<?php
/*
* This file is part of the Koded package.
*
* (c) Mihail Binev <mihail@kodeart.com>
*
* Please view the LICENSE distributed with this source code
* for the full copyright and license information.
*/
namespace Koded\Session\Handler;
use Koded\Session\SessionConfiguration;
use SessionHandlerInterface;
use function ini_get;
use function Koded\Caching\simple_cache_factory;
use function session_get_cookie_params;
use function time;
use function trim;
final class MemcachedHandler implements SessionHandlerInterface
{
protected int $ttl;
private \Memcached $client;
private array $settings;
public function __construct(SessionConfiguration $settings)
{
//ini_set('session.gc_probability', '0');
$this->settings = $this->configuration($settings);
if (1 > $this->ttl = (int)session_get_cookie_params()['lifetime']) {
$this->ttl = (int)ini_get('session.gc_maxlifetime');
}
}
public function close(): bool
{
$this->client->quit();
return true;
}
public function destroy($sessionId): bool
{
return $this->client->delete($sessionId);
}
public function open($savePath, $sessionId): bool
{
$this->client = simple_cache_factory('memcached', $this->settings)->client();
return true;
}
public function read($sessionId): string
{
return $this->client->get($sessionId) ?: '';
}
public function write($sessionId, $sessionData): bool
{
return $this->client->set($sessionId, $sessionData, time() + $this->ttl);
}
/**
* @codeCoverageIgnore
*/
public function gc($maxLifetime): int|false
{
return true;
}
private function configuration(SessionConfiguration $settings): array
{
return [
'id' => (string)$settings->get('id', $settings->get('name', ini_get('session.name'))),
'servers' => (array)$settings->get('servers', [['127.0.0.1', 11211]]),
'options' => (array)$settings->get('options', []) + [
\Memcached::OPT_PREFIX_KEY => trim($settings->get('prefix') ?: $settings->get('name') ?: 'session', '.') . '.'
]
];
}
}