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@0
|
14 /**
|
Chris@0
|
15 * Memcached based session storage handler based on the Memcached class
|
Chris@0
|
16 * provided by the PHP memcached extension.
|
Chris@0
|
17 *
|
Chris@0
|
18 * @see http://php.net/memcached
|
Chris@0
|
19 *
|
Chris@0
|
20 * @author Drak <drak@zikula.org>
|
Chris@0
|
21 */
|
Chris@14
|
22 class MemcachedSessionHandler extends AbstractSessionHandler
|
Chris@0
|
23 {
|
Chris@0
|
24 private $memcached;
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * @var int Time to live in seconds
|
Chris@0
|
28 */
|
Chris@0
|
29 private $ttl;
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * @var string Key prefix for shared environments
|
Chris@0
|
33 */
|
Chris@0
|
34 private $prefix;
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * Constructor.
|
Chris@0
|
38 *
|
Chris@0
|
39 * List of available options:
|
Chris@0
|
40 * * prefix: The prefix to use for the memcached keys in order to avoid collision
|
Chris@14
|
41 * * expiretime: The time to live in seconds.
|
Chris@0
|
42 *
|
Chris@0
|
43 * @param \Memcached $memcached A \Memcached instance
|
Chris@0
|
44 * @param array $options An associative array of Memcached options
|
Chris@0
|
45 *
|
Chris@0
|
46 * @throws \InvalidArgumentException When unsupported options are passed
|
Chris@0
|
47 */
|
Chris@17
|
48 public function __construct(\Memcached $memcached, array $options = [])
|
Chris@0
|
49 {
|
Chris@0
|
50 $this->memcached = $memcached;
|
Chris@0
|
51
|
Chris@17
|
52 if ($diff = array_diff(array_keys($options), ['prefix', 'expiretime'])) {
|
Chris@17
|
53 throw new \InvalidArgumentException(sprintf('The following options are not supported "%s"', implode(', ', $diff)));
|
Chris@0
|
54 }
|
Chris@0
|
55
|
Chris@0
|
56 $this->ttl = isset($options['expiretime']) ? (int) $options['expiretime'] : 86400;
|
Chris@0
|
57 $this->prefix = isset($options['prefix']) ? $options['prefix'] : 'sf2s';
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 /**
|
Chris@0
|
61 * {@inheritdoc}
|
Chris@0
|
62 */
|
Chris@0
|
63 public function close()
|
Chris@0
|
64 {
|
Chris@17
|
65 return $this->memcached->quit();
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 /**
|
Chris@0
|
69 * {@inheritdoc}
|
Chris@0
|
70 */
|
Chris@14
|
71 protected function doRead($sessionId)
|
Chris@0
|
72 {
|
Chris@0
|
73 return $this->memcached->get($this->prefix.$sessionId) ?: '';
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 /**
|
Chris@0
|
77 * {@inheritdoc}
|
Chris@0
|
78 */
|
Chris@14
|
79 public function updateTimestamp($sessionId, $data)
|
Chris@14
|
80 {
|
Chris@14
|
81 $this->memcached->touch($this->prefix.$sessionId, time() + $this->ttl);
|
Chris@14
|
82
|
Chris@14
|
83 return true;
|
Chris@14
|
84 }
|
Chris@14
|
85
|
Chris@14
|
86 /**
|
Chris@14
|
87 * {@inheritdoc}
|
Chris@14
|
88 */
|
Chris@14
|
89 protected function doWrite($sessionId, $data)
|
Chris@0
|
90 {
|
Chris@0
|
91 return $this->memcached->set($this->prefix.$sessionId, $data, time() + $this->ttl);
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 /**
|
Chris@0
|
95 * {@inheritdoc}
|
Chris@0
|
96 */
|
Chris@14
|
97 protected function doDestroy($sessionId)
|
Chris@0
|
98 {
|
Chris@14
|
99 $result = $this->memcached->delete($this->prefix.$sessionId);
|
Chris@14
|
100
|
Chris@14
|
101 return $result || \Memcached::RES_NOTFOUND == $this->memcached->getResultCode();
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 /**
|
Chris@0
|
105 * {@inheritdoc}
|
Chris@0
|
106 */
|
Chris@0
|
107 public function gc($maxlifetime)
|
Chris@0
|
108 {
|
Chris@0
|
109 // not required here because memcached will auto expire the records anyhow.
|
Chris@0
|
110 return true;
|
Chris@0
|
111 }
|
Chris@0
|
112
|
Chris@0
|
113 /**
|
Chris@0
|
114 * Return a Memcached instance.
|
Chris@0
|
115 *
|
Chris@0
|
116 * @return \Memcached
|
Chris@0
|
117 */
|
Chris@0
|
118 protected function getMemcached()
|
Chris@0
|
119 {
|
Chris@0
|
120 return $this->memcached;
|
Chris@0
|
121 }
|
Chris@0
|
122 }
|