Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
13
 
14
/**
15
 * Memcached based session storage handler based on the Memcached class
16
 * provided by the PHP memcached extension.
17
 *
18
 * @see https://php.net/memcached
19
 *
20
 * @author Drak <drak@zikula.org>
21
 */
22
class MemcachedSessionHandler extends AbstractSessionHandler
23
{
24
    private \Memcached $memcached;
25
 
26
    /**
27
     * Time to live in seconds.
28
     */
29
    private int|\Closure|null $ttl;
30
 
31
    /**
32
     * Key prefix for shared environments.
33
     */
34
    private string $prefix;
35
 
36
    /**
37
     * Constructor.
38
     *
39
     * List of available options:
40
     *  * prefix: The prefix to use for the memcached keys in order to avoid collision
41
     *  * ttl: The time to live in seconds.
42
     *
43
     * @throws \InvalidArgumentException When unsupported options are passed
44
     */
45
    public function __construct(\Memcached $memcached, array $options = [])
46
    {
47
        $this->memcached = $memcached;
48
 
49
        if ($diff = array_diff(array_keys($options), ['prefix', 'expiretime', 'ttl'])) {
50
            throw new \InvalidArgumentException(sprintf('The following options are not supported "%s".', implode(', ', $diff)));
51
        }
52
 
53
        $this->ttl = $options['expiretime'] ?? $options['ttl'] ?? null;
54
        $this->prefix = $options['prefix'] ?? 'sf2s';
55
    }
56
 
57
    public function close(): bool
58
    {
59
        return $this->memcached->quit();
60
    }
61
 
62
    protected function doRead(string $sessionId): string
63
    {
64
        return $this->memcached->get($this->prefix.$sessionId) ?: '';
65
    }
66
 
67
    public function updateTimestamp(string $sessionId, string $data): bool
68
    {
69
        $this->memcached->touch($this->prefix.$sessionId, $this->getCompatibleTtl());
70
 
71
        return true;
72
    }
73
 
74
    protected function doWrite(string $sessionId, string $data): bool
75
    {
76
        return $this->memcached->set($this->prefix.$sessionId, $data, $this->getCompatibleTtl());
77
    }
78
 
79
    private function getCompatibleTtl(): int
80
    {
81
        $ttl = ($this->ttl instanceof \Closure ? ($this->ttl)() : $this->ttl) ?? \ini_get('session.gc_maxlifetime');
82
 
83
        // If the relative TTL that is used exceeds 30 days, memcached will treat the value as Unix time.
84
        // We have to convert it to an absolute Unix time at this point, to make sure the TTL is correct.
85
        if ($ttl > 60 * 60 * 24 * 30) {
86
            $ttl += time();
87
        }
88
 
89
        return $ttl;
90
    }
91
 
92
    protected function doDestroy(string $sessionId): bool
93
    {
94
        $result = $this->memcached->delete($this->prefix.$sessionId);
95
 
96
        return $result || \Memcached::RES_NOTFOUND == $this->memcached->getResultCode();
97
    }
98
 
99
    public function gc(int $maxlifetime): int|false
100
    {
101
        // not required here because memcached will auto expire the records anyhow.
102
        return 0;
103
    }
104
 
105
    /**
106
     * Return a Memcached instance.
107
     */
108
    protected function getMemcached(): \Memcached
109
    {
110
        return $this->memcached;
111
    }
112
}