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@0
|
48 public function __construct(\Memcached $memcached, array $options = array())
|
Chris@0
|
49 {
|
Chris@0
|
50 $this->memcached = $memcached;
|
Chris@0
|
51
|
Chris@0
|
52 if ($diff = array_diff(array_keys($options), array('prefix', 'expiretime'))) {
|
Chris@0
|
53 throw new \InvalidArgumentException(sprintf(
|
Chris@0
|
54 'The following options are not supported "%s"', implode(', ', $diff)
|
Chris@0
|
55 ));
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 $this->ttl = isset($options['expiretime']) ? (int) $options['expiretime'] : 86400;
|
Chris@0
|
59 $this->prefix = isset($options['prefix']) ? $options['prefix'] : 'sf2s';
|
Chris@0
|
60 }
|
Chris@0
|
61
|
Chris@0
|
62 /**
|
Chris@0
|
63 * {@inheritdoc}
|
Chris@0
|
64 */
|
Chris@0
|
65 public function close()
|
Chris@0
|
66 {
|
Chris@0
|
67 return true;
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * {@inheritdoc}
|
Chris@0
|
72 */
|
Chris@14
|
73 protected function doRead($sessionId)
|
Chris@0
|
74 {
|
Chris@0
|
75 return $this->memcached->get($this->prefix.$sessionId) ?: '';
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 /**
|
Chris@0
|
79 * {@inheritdoc}
|
Chris@0
|
80 */
|
Chris@14
|
81 public function updateTimestamp($sessionId, $data)
|
Chris@14
|
82 {
|
Chris@14
|
83 $this->memcached->touch($this->prefix.$sessionId, time() + $this->ttl);
|
Chris@14
|
84
|
Chris@14
|
85 return true;
|
Chris@14
|
86 }
|
Chris@14
|
87
|
Chris@14
|
88 /**
|
Chris@14
|
89 * {@inheritdoc}
|
Chris@14
|
90 */
|
Chris@14
|
91 protected function doWrite($sessionId, $data)
|
Chris@0
|
92 {
|
Chris@0
|
93 return $this->memcached->set($this->prefix.$sessionId, $data, time() + $this->ttl);
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96 /**
|
Chris@0
|
97 * {@inheritdoc}
|
Chris@0
|
98 */
|
Chris@14
|
99 protected function doDestroy($sessionId)
|
Chris@0
|
100 {
|
Chris@14
|
101 $result = $this->memcached->delete($this->prefix.$sessionId);
|
Chris@14
|
102
|
Chris@14
|
103 return $result || \Memcached::RES_NOTFOUND == $this->memcached->getResultCode();
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 /**
|
Chris@0
|
107 * {@inheritdoc}
|
Chris@0
|
108 */
|
Chris@0
|
109 public function gc($maxlifetime)
|
Chris@0
|
110 {
|
Chris@0
|
111 // not required here because memcached will auto expire the records anyhow.
|
Chris@0
|
112 return true;
|
Chris@0
|
113 }
|
Chris@0
|
114
|
Chris@0
|
115 /**
|
Chris@0
|
116 * Return a Memcached instance.
|
Chris@0
|
117 *
|
Chris@0
|
118 * @return \Memcached
|
Chris@0
|
119 */
|
Chris@0
|
120 protected function getMemcached()
|
Chris@0
|
121 {
|
Chris@0
|
122 return $this->memcached;
|
Chris@0
|
123 }
|
Chris@0
|
124 }
|