Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
|
Chris@0
|
13
|
Chris@14
|
14 @trigger_error(sprintf('The class %s is deprecated since Symfony 3.4 and will be removed in 4.0. Use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcachedSessionHandler instead.', MemcacheSessionHandler::class), E_USER_DEPRECATED);
|
Chris@14
|
15
|
Chris@0
|
16 /**
|
Chris@14
|
17 * @author Drak <drak@zikula.org>
|
Chris@0
|
18 *
|
Chris@14
|
19 * @deprecated since version 3.4, to be removed in 4.0. Use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcachedSessionHandler instead.
|
Chris@0
|
20 */
|
Chris@0
|
21 class MemcacheSessionHandler implements \SessionHandlerInterface
|
Chris@0
|
22 {
|
Chris@0
|
23 private $memcache;
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * @var int Time to live in seconds
|
Chris@0
|
27 */
|
Chris@0
|
28 private $ttl;
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * @var string Key prefix for shared environments
|
Chris@0
|
32 */
|
Chris@0
|
33 private $prefix;
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * Constructor.
|
Chris@0
|
37 *
|
Chris@0
|
38 * List of available options:
|
Chris@0
|
39 * * prefix: The prefix to use for the memcache keys in order to avoid collision
|
Chris@0
|
40 * * expiretime: The time to live in seconds
|
Chris@0
|
41 *
|
Chris@0
|
42 * @param \Memcache $memcache A \Memcache instance
|
Chris@0
|
43 * @param array $options An associative array of Memcache options
|
Chris@0
|
44 *
|
Chris@0
|
45 * @throws \InvalidArgumentException When unsupported options are passed
|
Chris@0
|
46 */
|
Chris@17
|
47 public function __construct(\Memcache $memcache, array $options = [])
|
Chris@0
|
48 {
|
Chris@17
|
49 if ($diff = array_diff(array_keys($options), ['prefix', 'expiretime'])) {
|
Chris@17
|
50 throw new \InvalidArgumentException(sprintf('The following options are not supported "%s"', implode(', ', $diff)));
|
Chris@0
|
51 }
|
Chris@0
|
52
|
Chris@0
|
53 $this->memcache = $memcache;
|
Chris@0
|
54 $this->ttl = isset($options['expiretime']) ? (int) $options['expiretime'] : 86400;
|
Chris@0
|
55 $this->prefix = isset($options['prefix']) ? $options['prefix'] : 'sf2s';
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * {@inheritdoc}
|
Chris@0
|
60 */
|
Chris@0
|
61 public function open($savePath, $sessionName)
|
Chris@0
|
62 {
|
Chris@0
|
63 return true;
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 /**
|
Chris@0
|
67 * {@inheritdoc}
|
Chris@0
|
68 */
|
Chris@0
|
69 public function close()
|
Chris@0
|
70 {
|
Chris@0
|
71 return true;
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 /**
|
Chris@0
|
75 * {@inheritdoc}
|
Chris@0
|
76 */
|
Chris@0
|
77 public function read($sessionId)
|
Chris@0
|
78 {
|
Chris@0
|
79 return $this->memcache->get($this->prefix.$sessionId) ?: '';
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * {@inheritdoc}
|
Chris@0
|
84 */
|
Chris@0
|
85 public function write($sessionId, $data)
|
Chris@0
|
86 {
|
Chris@0
|
87 return $this->memcache->set($this->prefix.$sessionId, $data, 0, time() + $this->ttl);
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 /**
|
Chris@0
|
91 * {@inheritdoc}
|
Chris@0
|
92 */
|
Chris@0
|
93 public function destroy($sessionId)
|
Chris@0
|
94 {
|
Chris@14
|
95 $this->memcache->delete($this->prefix.$sessionId);
|
Chris@14
|
96
|
Chris@14
|
97 return true;
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@0
|
100 /**
|
Chris@0
|
101 * {@inheritdoc}
|
Chris@0
|
102 */
|
Chris@0
|
103 public function gc($maxlifetime)
|
Chris@0
|
104 {
|
Chris@0
|
105 // not required here because memcache will auto expire the records anyhow.
|
Chris@0
|
106 return true;
|
Chris@0
|
107 }
|
Chris@0
|
108
|
Chris@0
|
109 /**
|
Chris@0
|
110 * Return a Memcache instance.
|
Chris@0
|
111 *
|
Chris@0
|
112 * @return \Memcache
|
Chris@0
|
113 */
|
Chris@0
|
114 protected function getMemcache()
|
Chris@0
|
115 {
|
Chris@0
|
116 return $this->memcache;
|
Chris@0
|
117 }
|
Chris@0
|
118 }
|