/
MemcacheStorage.php
136 lines (118 loc) · 3.45 KB
/
MemcacheStorage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Session\Storage;
/**
* MemcacheStorage.
*
* @author Drak <drak@zikula.org>
*/
class MemcacheStorage extends AbstractStorage implements SaveHandlerInterface
{
/**
* Memcache driver.
*
* @var Memcache
*/
private $memcache;
/**
* Configuration options.
*
* @var array
*/
private $memcacheOptions;
/**
* Key prefix for shared environments.
*
* @var string
*/
private $prefix;
/**
* Constructor.
*
* @param \Memcache $memcache A \Memcache instance
* @param array $memcacheOptions An associative array of Memcachge options
* @param array $options Session configuration options.
*
* @see AbstractStorage::__construct()
*/
public function __construct(\Memcache $memcache, array $memcacheOptions = array(), array $options = array())
{
$this->memcache = $memcache;
// defaults
if (!isset($memcacheOptions['serverpool'])) {
$memcacheOptions['serverpool'] = array(
'host' => '127.0.0.1',
'port' => 11211,
'timeout' => 1,
'persistent' => false,
'weight' => 1);
}
$memcacheOptions['expiretime'] = isset($memcacheOptions['expiretime']) ? (int)$memcacheOptions['expiretime'] : 86400;
$this->prefix = isset($memcachedOptions['prefix']) ? $memcachedOptions['prefix'] : 'sf2s';
$this->memcacheOptions = $memcacheOptions;
parent::__construct($options);
}
protected function addServer(array $server)
{
if (array_key_exists('host', $server)) {
throw new \InvalidArgumentException('host key must be set');
}
$server['port'] = isset($server['port']) ? (int)$server['port'] : 11211;
$server['timeout'] = isset($server['timeout']) ? (int)$server['timeout'] : 1;
$server['presistent'] = isset($server['presistent']) ? (bool)$server['presistent'] : false;
$server['weight'] = isset($server['weight']) ? (bool)$server['weight'] : 1;
}
/**
* {@inheritdoc}
*/
public function openSession($savePath, $sessionName)
{
foreach ($this->memcacheOptions['serverpool'] as $server) {
$this->addServer($server);
}
return true;
}
/**
* {@inheritdoc}
*/
public function closeSession()
{
return $this->memcache->close();
}
/**
* {@inheritdoc}
*/
public function readSession($sessionId)
{
return $this->memcache->get($this->prefix.$sessionId) ?: '';
}
/**
* {@inheritdoc}
*/
public function writeSession($sessionId, $data)
{
return $this->memcache->set($this->prefix.$sessionId, $data, $this->memcacheOptions['expiretime']);
}
/**
* {@inheritdoc}
*/
public function destroySession($sessionId)
{
return $this->memcache->delete($this->prefix.$sessionId);
}
/**
* {@inheritdoc}
*/
public function gcSession($lifetime)
{
// not required here because memcache will auto expire the records anyhow.
return true;
}
}