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