8000 [HttpFoundation][Session] Assume that memcache(d) instances are alrea… · symfony/symfony@0216e05 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0216e05

Browse files
committed
[HttpFoundation][Session] Assume that memcache(d) instances are already configured
1 parent 72d21c6 commit 0216e05

File tree

4 files changed

+28
-124
lines changed

4 files changed

+28
-124
lines changed

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MemcacheSessionHandler.php

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -44,92 +44,59 @@ class MemcacheSessionHandler implements \SessionHandlerInterface
4444
*
4545
* @param \Memcache $memcache A \Memcache instance
4646
* @param array $memcacheOptions An associative array of Memcache options
47-
* @param array $options Session configuration options.
4847
*/
49-
public function __construct(\Memcache $memcache, array $memcacheOptions = array(), array $options = array())
48+
public function __construct(\Memcache $memcache, array $memcacheOptions = array())
5049
{
5150
$this->memcache = $memcache;
5251

53-
// defaults
54-
if (!isset($memcacheOptions['serverpool'])) {
55-
$memcacheOptions['serverpool'] = array(array(
56-
'host' => '127.0.0.1',
57-
'port' => 11211,
58-
'timeout' => 1,
59-
'persistent' => false,
60-
'weight' => 1,
61-
'retry_interval' => 15,
62-
));
63-
}
64-
6552
$memcacheOptions['expiretime'] = isset($memcacheOptions['expiretime']) ? (int)$memcacheOptions['expiretime'] : 86400;
6653
$this->prefix = isset($memcacheOptions['prefix']) ? $memcacheOptions['prefix'] : 'sf2s';
6754

6855
$this->memcacheOptions = $memcacheOptions;
6956
}
7057

71-
protected function addServer(array $server)
72-
{
73-
if (!array_key_exists('host', $server)) {
74-
throw new \InvalidArgumentException('host key must be set');
75-
}
76-
77-
$server['port'] = isset($server['port']) ? (int)$server['port'] : 11211;
78-
$server['timeout'] = isset($server['timeout']) ? (int)$server['timeout'] : 1;
79-
$server['persistent'] = isset($server['persistent']) ? (bool)$server['persistent'] : false;
80-
$server['weight'] = isset($server['weight']) ? (int)$server['weight'] : 1;
81-
$server['retry_interval'] = isset($server['retry_interval']) ? (int)$server['retry_interval'] : 15;
82-
83-
$this->memcache->addserver($server['host'], $server['port'], $server['persistent'],$server['weight'],$server['timeout'],$server['retry_interval']);
84-
85-
}
86-
8758
/**
88-
* {@inheritdoc}
59+
* {@inheritDoc}
8960
*/
9061
public function open($savePath, $sessionName)
9162
{
92-
foreach ($this->memcacheOptions['serverpool'] as $server) {
93-
$this->addServer($server);
94-
}
95-
9663
return true;
9764
}
9865

9966
/**
100-
* {@inheritdoc}
67+
* {@inheritDoc}
10168
*/
10269
public function close()
10370
{
10471
return $this->memcache->close();
10572
}
10673

10774
/**
108-
* {@inheritdoc}
75+
* {@inheritDoc}
10976
*/
11077
public function read($sessionId)
11178
{
11279
return $this->memcache->get($this->prefix.$sessionId) ?: '';
11380
}
11481

11582
/**
116-
* {@inheritdoc}
83+
* {@inheritDoc}
11784
*/
11885
public function write($sessionId, $data)
11986
{
120-
return $this->memcache->set($this->prefix.$sessionId, $data, 0, $this->memcacheOptions['expiretime']);
87+
return $this->memcache->set($this->prefix.$sessionId, $data, 0, time() + $this->memcacheOptions['expiretime']);
12188
}
12289

12390
/**
124-
* {@inheritdoc}
91+
* {@inheritDoc}
12592
*/
12693
public function destroy($sessionId)
12794
{
12895
return $this->memcache->delete($this->prefix.$sessionId);
12996
}
13097

13198
/**
132-
* {@inheritdoc}
99+
* {@inheritDoc}
133100
*/
134101
public function gc($lifetime)
135102
{

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MemcachedSessionHandler.php

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -37,94 +37,75 @@ class MemcachedSessionHandler implements \SessionHandlerInterface
3737
*/
3838
private $memcachedOptions;
3939

40+
/**
41+
* Key prefix for shared environments.
42+
*
43+
* @var string
44+
*/
45+
private $prefix;
46+
4047
/**
4148
* Constructor.
4249
*
4350
* @param \Memcached $memcached A \Memcached instance
4451
* @param array $memcachedOptions An associative array of Memcached options
45-
* @param array $options Session configuration options.
4652
*/
47-
public function __construct(\Memcached $memcached, array $memcachedOptions = array(), array $options = array())
53+
public function __construct(\Memcached $memcached, array $memcachedOptions = array())
4854
{
4955
$this->memcached = $memcached;
5056

51-
// defaults
52-
if (!isset($memcachedOptions['serverpool'])) {
53-
$memcachedOptions['serverpool'][] = array(
54-
'host' => '127.0.0.1',
55-
'port' => 11211,
56-
'weight' => 1);
57-
}
58-
59-
$memcachedOptions['expiretime'] = isset($memcachedOptions['expiretime']) ? (int)$memcachedOptions['expiretime'] : 86400;
60-
61-
$this->memcached->setOption(\Memcached::OPT_PREFIX_KEY, isset($memcachedOptions['prefix']) ? $memcachedOptions['prefix'] : 'sf2s');
57+
$memcachedOptions['expiretime'] = isset($memcachedOptions['expiretime']) ? (int) $memcachedOptions['expiretime'] : 86400;
58+
$this->prefix = isset($memcachedOptions['prefix']) ? $memcachedOptions['prefix'] : 'sf2s';
6259

6360
$this->memcachedOptions = $memcachedOptions;
6461
}
6562

6663
/**
67-
* {@inheritdoc}
64+
* {@inheritDoc}
6865
*/
6966
public function open($savePath, $sessionName)
7067
{
71-
return $this->memcached->addServers($this->memcachedOptions['serverpool']);
68+
return true;
7269
}
7370

7471
/**
75-
* {@inheritdoc}< F42D /span>
72+
* {@inheritDoc}
7673
*/
7774
public function close()
7875
{
7976
return true;
8077
}
8178

8279
/**
83-
* {@inheritdoc}
80+
* {@inheritDoc}
8481
*/
8582
public function read($sessionId)
8683
{
87-
return $this->memcached->get($sessionId) ?: '';
84+
return $this->memcached->get($this->prefix.$sessionId) ?: '';
8885
}
8986

9087
/**
91-
* {@inheritdoc}
88+
* {@inheritDoc}
9289
*/
9390
public function write($sessionId, $data)
9491
{
95-
return $this->memcached->set($sessionId, $data, $this->memcachedOptions['expiretime']);
92+
return $this->memcached->set($this->prefix.$sessionId, $data, time() + $this->memcachedOptions['expiretime']);
9693
}
9794

9895
/**
99-
* {@inheritdoc}
96+
* {@inheritDoc}
10097
*/
10198
public function destroy($sessionId)
10299
{
103-
return $this->memcached->delete($sessionId);
100+
return $this->memcached->delete($this->prefix.$sessionId);
104101
}
105102

106103
/**
107-
* {@inheritdoc}
104+
* {@inheritDoc}
108105
*/
109106
public function gc($lifetime)
110107
{
111108
// not required here because memcached will auto expire the records anyhow.
112109
return true;
113110
}
114-
115-
/**
116-
* Adds a server to the memcached handler.
117-
*
118-
* @param array $server
119-
*/
120-
protected function addServer(array $server)
121-
{
122-
if (array_key_exists('host', $server)) {
123-
throw new \InvalidArgumentException('host key must be set');
124-
}
125-
$server['port'] = isset($server['port']) ? (int)$server['port'] : 11211;
126-
$server['timeout'] = isset($server['timeout']) ? (int)$server['timeout'] : 1;
127-
$server['presistent'] = isset($server['presistent']) ? (bool)$server['presistent'] : false;
128-
$server['weight'] = isset($server['weight']) ? (bool)$server['weight'] : 1;
129-
}
130111
}

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MemcacheSessionHandlerTest.php

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -40,49 +40,9 @@ protected function tearDown()
4040

4141
public function testOpenSession()
4242
{
43-
$this->memcache->expects($this->atLeastOnce())
44-
->method('addServer')
45-
->with('127.0.0.1', 11211, false, 1, 1, 15);
46-
4743
$this->assertTrue($this->storage->open('', ''));
4844
}
4945

50-
public function testConstructingWithServerPool()
51-
{
52-
$mock = $this->getMock('Memcache');
53-
54-
$storage = new MemcacheSessionHandler($mock, array(
55-
'serverpool' => array(
56-
array('host' => '127.0.0.2'),
57-
array('host' => '127.0.0.3',
58-
'port' => 11212,
59-
'timeout' => 10,
60-
'persistent' => true,
61-
'weight' => 5,
62-
'retry_interval' => 39,
63-
),
64-
array('host' => '127.0.0.4',
65-
'port' => 11211,
66-
'weight' => 2
67-
),
68-
),
69-
));
70-
71-
$matcher = $mock
72-
->expects($this->at(0))
73-
->method('addServer')
74-
->with('127.0.0.2', 11211, false, 1, 1, 15);
75-
$matcher = $mock
76-
->expects($this->at(1))
77-
->method('addServer')
78-
->with('127.0.0.3', 11212, true, 5, 10, 39);
79-
$matcher = $mock
80-
->expects($this->at(2))
81-
->method('addServer')
82-
->with('127.0.0.4', 11211, false, 2, 1, 15);
83-
$this->assertTrue($storage->open('', ''));
84-
}
85-
8646
public function testCloseSession()
8747
{
8848
$this->memcache->expects($this->once())

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MemcachedSessionHandlerTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ protected function tearDown()
4040

4141
public function testOpenSession()
4242
{
43-
$this->memcached->expects($this->atLeastOnce())
44-
->method('addServers')
45-
->will($this->returnValue(true));
46-
4743
$this->assertTrue($this->storage->open('', ''));
4844
}
4945

0 commit comments

Comments
 (0)
0