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