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