Skip to content

Commit f9d669a

Browse files
authored
Merge pull request #6857 from ping-yee/refactor-replace-time-session
refactor: replace time() with Time into whole `Session` class.
2 parents 5558fe9 + beaffc2 commit f9d669a

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

system/Session/Handlers/FileHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace CodeIgniter\Session\Handlers;
1313

14+
use CodeIgniter\I18n\Time;
1415
use CodeIgniter\Session\Exceptions\SessionException;
1516
use Config\App as AppConfig;
1617
use ReturnTypeWillChange;
@@ -276,7 +277,7 @@ public function gc($max_lifetime)
276277
return false;
277278
}
278279

279-
$ts = time() - $max_lifetime;
280+
$ts = Time::now()->getTimestamp() - $max_lifetime;
280281

281282
$pattern = $this->matchIP === true ? '[0-9a-f]{32}' : '';
282283

system/Session/Handlers/MemcachedHandler.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace CodeIgniter\Session\Handlers;
1313

14+
use CodeIgniter\I18n\Time;
1415
use CodeIgniter\Session\Exceptions\SessionException;
1516
use Config\App as AppConfig;
1617
use Memcached;
@@ -167,7 +168,7 @@ public function write($id, $data): bool
167168
}
168169

169170
if (isset($this->lockKey)) {
170-
$this->memcached->replace($this->lockKey, time(), 300);
171+
$this->memcached->replace($this->lockKey, Time::now()->getTimestamp(), 300);
171172

172173
if ($this->fingerprint !== ($fingerprint = md5($data))) {
173174
if ($this->memcached->set($this->keyPrefix . $id, $data, $this->sessionExpiration)) {
@@ -245,7 +246,7 @@ public function gc($max_lifetime)
245246
protected function lockSession(string $sessionID): bool
246247
{
247248
if (isset($this->lockKey)) {
248-
return $this->memcached->replace($this->lockKey, time(), 300);
249+
return $this->memcached->replace($this->lockKey, Time::now()->getTimestamp(), 300);
249250
}
250251

251252
$lockKey = $this->keyPrefix . $sessionID . ':lock';
@@ -258,7 +259,7 @@ protected function lockSession(string $sessionID): bool
258259
continue;
259260
}
260261

261-
if (! $this->memcached->set($lockKey, time(), 300)) {
262+
if (! $this->memcached->set($lockKey, Time::now()->getTimestamp(), 300)) {
262263
$this->logger->error('Session: Error while trying to obtain lock for ' . $this->keyPrefix . $sessionID);
263264

264265
return false;

system/Session/Handlers/RedisHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace CodeIgniter\Session\Handlers;
1313

14+
use CodeIgniter\I18n\Time;
1415
use CodeIgniter\Session\Exceptions\SessionException;
1516
use Config\App as AppConfig;
1617
use Redis;
@@ -293,7 +294,7 @@ protected function lockSession(string $sessionID): bool
293294
continue;
294295
}
295296

296-
if (! $this->redis->setex($lockKey, 300, (string) time())) {
297+
if (! $this->redis->setex($lockKey, 300, (string) Time::now()->getTimestamp())) {
297298
$this->logger->error('Session: Error while trying to obtain lock for ' . $this->keyPrefix . $sessionID);
298299

299300
return false;

0 commit comments

Comments
 (0)