8000 Use relative timestamps · symfony/symfony@3e65ba6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3e65ba6

Browse files
committed
Use relative timestamps
1 parent 73e03fb commit 3e65ba6

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ protected function doRead(string $sessionId)
7777
#[\ReturnTypeWillChange]
7878
public function updateTimestamp($sessionId, $data)
7979
{
80-
$this->memcached->touch($this->prefix.$sessionId, time() + (int) ($this->ttl ?? \ini_get('session.gc_maxlifetime')));
80+
$this->memcached->touch($this->prefix.$sessionId, $this->getCompatibleTtl());
8181

8282
return true;
8383
}
@@ -87,7 +87,20 @@ public function updateTimestamp($sessionId, $data)
8787
*/
8888
protected function doWrite(string $sessionId, string $data)
8989
{
90-
return $this->memcached->set($this->prefix.$sessionId, $data, time() + (int) ($this->ttl ?? \ini_get('session.gc_maxlifetime')));
90+
return $this->memcached->set($this->prefix.$sessionId, $data, $this->getCompatibleTtl());
91+
}
92+
93+
private function getCompatibleTtl(): int
94+
{
95+
$ttl = (int) ($this->ttl ?? \ini_get('session.gc_maxlifetime'));
96+
97+
// If the relative TTL that is used exceeds 30 days, memcached will treat the value as Unix time.
98+
// We have to convert it to an absolute Unix time at this point, to make sure the TTL is correct.
99+
if ($ttl > 60*60*24*30) {
100+
$ttl += time();
101+
}
102+
103+
return $ttl;
91104
}
92105

93106
/**

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ protected function setUp(): void
5151
$this->memcached,
5252
['prefix' => self::PREFIX, 'expiretime' => self::TTL]
5353
);
54+
55+
$this->largeTtlStorage = new MemcachedSessionHandler(
56+
$this->memcached,
57+
['prefix' => self::PREFIX, 'expiretime' => self::TTL + 60*60*24*30]
58+
);
5459
}
5560

5661
protected function tearDown(): void
@@ -92,13 +97,25 @@ public function testWriteSession()
9297
$this->memcached
9398
->expects($this->once())
9499
->method('set')
95-
->with(self::PREFIX.'id', 'data', $this->equalTo(time() + self::TTL, 2))
100+
->with(self::PREFIX.'id', 'data', $this->equalTo(self::TTL, 2))
96101
->willReturn(true)
97102
;
98103

99104
$this->assertTrue($this->storage->write('id', 'data'));
100105
}
101106

107+
public function testWriteSessionWithLargeTTL()
108+
{
109+
$this->memcached
110+
->expects($this->once())
111+
->method('set')
112+
->with(self::PREFIX.'id', 'data', $this->equalTo(time() + self::TTL + 60*60*24*30, 2))
113+
->willReturn(true)
114+
;
115+
116+
$this->assertTrue($this->largeTtlStorage->write('id', 'data'));
117+
}
118+
102119
public function testDestroySession()
103120
{
104121
$this->memcached

0 commit comments

Comments
 (0)
0