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 * MemcacheSessionHandler.
|
Chris@0
|
16 *
|
Chris@0
|
17 * @author Drak <drak@zikula.org>
|
Chris@0
|
18 */
|
Chris@0
|
19 class MemcacheSessionHandler implements \SessionHandlerInterface
|
Chris@0
|
20 {
|
Chris@0
|
21 /**
|
Chris@0
|
22 * @var \Memcache Memcache driver
|
Chris@0
|
23 */
|
Chris@0
|
24 private $memcache;
|
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 memcache keys in order to avoid collision
|
Chris@0
|
41 * * expiretime: The time to live in seconds
|
Chris@0
|
42 *
|
Chris@0
|
43 * @param \Memcache $memcache A \Memcache instance
|
Chris@0
|
44 * @param array $options An associative array of Memcache options
|
Chris@0
|
45 *
|
Chris@0
|
46 * @throws \InvalidArgumentException When unsupported options are passed
|
Chris@0
|
47 */
|
Chris@0
|
48 public function __construct(\Memcache $memcache, array $options = array())
|
Chris@0
|
49 {
|
Chris@0
|
50 if ($diff = array_diff(array_keys($options), array('prefix', 'expiretime'))) {
|
Chris@0
|
51 throw new \InvalidArgumentException(sprintf(
|
Chris@0
|
52 'The following options are not supported "%s"', implode(', ', $diff)
|
Chris@0
|
53 ));
|
Chris@0
|
54 }
|
Chris@0
|
55
|
Chris@0
|
56 $this->memcache = $memcache;
|
Chris@0
|
57 $this->ttl = isset($options['expiretime']) ? (int) $options['expiretime'] : 86400;
|
Chris@0
|
58 $this->prefix = isset($options['prefix']) ? $options['prefix'] : 'sf2s';
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@0
|
62 * {@inheritdoc}
|
Chris@0
|
63 */
|
Chris@0
|
64 public function open($savePath, $sessionName)
|
Chris@0
|
65 {
|
Chris@0
|
66 return true;
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 /**
|
Chris@0
|
70 * {@inheritdoc}
|
Chris@0
|
71 */
|
Chris@0
|
72 public function close()
|
Chris@0
|
73 {
|
Chris@0
|
74 return true;
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 /**
|
Chris@0
|
78 * {@inheritdoc}
|
Chris@0
|
79 */
|
Chris@0
|
80 public function read($sessionId)
|
Chris@0
|
81 {
|
Chris@0
|
82 return $this->memcache->get($this->prefix.$sessionId) ?: '';
|
Chris@0
|
83 }
|
Chris@0
|
84
|
Chris@0
|
85 /**
|
Chris@0
|
86 * {@inheritdoc}
|
Chris@0
|
87 */
|
Chris@0
|
88 public function write($sessionId, $data)
|
Chris@0
|
89 {
|
Chris@0
|
90 return $this->memcache->set($this->prefix.$sessionId, $data, 0, time() + $this->ttl);
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 /**
|
Chris@0
|
94 * {@inheritdoc}
|
Chris@0
|
95 */
|
Chris@0
|
96 public function destroy($sessionId)
|
Chris@0
|
97 {
|
Chris@0
|
98 return $this->memcache->delete($this->prefix.$sessionId);
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 /**
|
Chris@0
|
102 * {@inheritdoc}
|
Chris@0
|
103 */
|
Chris@0
|
104 public function gc($maxlifetime)
|
Chris@0
|
105 {
|
Chris@0
|
106 // not required here because memcache will auto expire the records anyhow.
|
Chris@0
|
107 return true;
|
Chris@0
|
108 }
|
Chris@0
|
109
|
Chris@0
|
110 /**
|
Chris@0
|
111 * Return a Memcache instance.
|
Chris@0
|
112 *
|
Chris@0
|
113 * @return \Memcache
|
Chris@0
|
114 */
|
Chris@0
|
115 protected function getMemcache()
|
Chris@0
|
116 {
|
Chris@0
|
117 return $this->memcache;
|
Chris@0
|
118 }
|
Chris@0
|
119 }
|