comparison vendor/symfony/http-foundation/Session/Storage/Handler/MemcachedSessionHandler.php @ 0:4c8ae668cc8c

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