Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make time() to class Time in Cache #6850

Merged
merged 3 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions deptrac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ parameters:
API:
- Format
- HTTP
Cache:
- I18n
Controller:
- HTTP
- Validation
Expand Down
3 changes: 2 additions & 1 deletion system/Cache/Handlers/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\Cache\Handlers;

use CodeIgniter\Cache\Exceptions\CacheException;
use CodeIgniter\I18n\Time;
use Config\Cache;
use Throwable;

Expand Down Expand Up @@ -91,7 +92,7 @@ public function save(string $key, $value, int $ttl = 60)
$key = static::validateKey($key, $this->prefix);

$contents = [
'time' => time(),
'time' => Time::now()->getTimestamp(),
'ttl' => $ttl,
'data' => $value,
];
Expand Down
3 changes: 2 additions & 1 deletion system/Cache/Handlers/MemcachedHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\Cache\Handlers;

use CodeIgniter\Exceptions\CriticalError;
use CodeIgniter\I18n\Time;
use Config\Cache;
use Exception;
use Memcache;
Expand Down Expand Up @@ -155,7 +156,7 @@ public function save(string $key, $value, int $ttl = 60)
if (! $this->config['raw']) {
$value = [
$value,
time(),
Time::now()->getTimestamp(),
$ttl,
];
}
Expand Down
7 changes: 4 additions & 3 deletions system/Cache/Handlers/PredisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\Cache\Handlers;

use CodeIgniter\Exceptions\CriticalError;
use CodeIgniter\I18n\Time;
use Config\Cache;
use Exception;
use Predis\Client;
Expand Down Expand Up @@ -128,7 +129,7 @@ public function save(string $key, $value, int $ttl = 60)
}

if ($ttl) {
$this->redis->expireat($key, time() + $ttl);
$this->redis->expireat($key, Time::now()->getTimestamp() + $ttl);
}

return true;
Expand Down Expand Up @@ -204,11 +205,11 @@ public function getMetaData(string $key)
$data = array_combine(['__ci_value'], $this->redis->hmget($key, ['__ci_value']));

if (isset($data['__ci_value']) && $data['__ci_value'] !== false) {
$time = time();
$time = Time::now()->getTimestamp();
$ttl = $this->redis->ttl($key);

return [
'expire' => $ttl > 0 ? time() + $ttl : null,
'expire' => $ttl > 0 ? $time + $ttl : null,
'mtime' => $time,
'data' => $data['__ci_value'],
];
Expand Down
7 changes: 4 additions & 3 deletions system/Cache/Handlers/RedisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\Cache\Handlers;

use CodeIgniter\Exceptions\CriticalError;
use CodeIgniter\I18n\Time;
use Config\Cache;
use Redis;
use RedisException;
Expand Down Expand Up @@ -154,7 +155,7 @@ public function save(string $key, $value, int $ttl = 60)
}

if ($ttl) {
$this->redis->expireAt($key, time() + $ttl);
$this->redis->expireAt($key, Time::now()->getTimestamp() + $ttl);
}

return true;
Expand Down Expand Up @@ -236,11 +237,11 @@ public function getMetaData(string $key)
$value = $this->get($key);

if ($value !== null) {
$time = time();
$time = Time::now()->getTimestamp();
$ttl = $this->redis->ttl($key);

return [
'expire' => $ttl > 0 ? time() + $ttl : null,
'expire' => $ttl > 0 ? $time + $ttl : null,
'mtime' => $time,
'data' => $value,
];
Expand Down
3 changes: 2 additions & 1 deletion system/Cache/Handlers/WincacheHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace CodeIgniter\Cache\Handlers;

use CodeIgniter\I18n\Time;
use Config\Cache;
use Exception;

Expand Down Expand Up @@ -124,7 +125,7 @@ public function getMetaData(string $key)
$hitcount = $stored['ucache_entries'][1]['hitcount'];

return [
'expire' => $ttl > 0 ? time() + $ttl : null,
'expire' => $ttl > 0 ? Time::now()->getTimestamp() + $ttl : null,
'hitcount' => $hitcount,
'age' => $age,
'ttl' => $ttl,
Expand Down
3 changes: 2 additions & 1 deletion tests/system/Cache/Handlers/AbstractHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace CodeIgniter\Cache\Handlers;

use CodeIgniter\I18n\Time;
use CodeIgniter\Test\CIUnitTestCase;

/**
Expand All @@ -31,7 +32,7 @@ public function testGetMetaDataMiss()

public function testGetMetaData()
{
$time = time();
$time = Time::now()->getTimestamp();
$this->handler->save(self::$key1, 'value');

$actual = $this->handler->getMetaData(self::$key1);
Expand Down
3 changes: 2 additions & 1 deletion tests/system/Cache/Handlers/FileHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use CodeIgniter\Cache\Exceptions\CacheException;
use CodeIgniter\CLI\CLI;
use CodeIgniter\I18n\Time;
use Config\Cache;

/**
Expand Down Expand Up @@ -156,7 +157,7 @@ public function testSavePermanent()
$metaData = $this->handler->getMetaData(self::$key1);

$this->assertNull($metaData['expire']);
$this->assertLessThanOrEqual(1, $metaData['mtime'] - time());
$this->assertLessThanOrEqual(1, $metaData['mtime'] - Time::now()->getTimestamp());
$this->assertSame('value', $metaData['data']);

$this->assertTrue($this->handler->delete(self::$key1));
Expand Down
3 changes: 2 additions & 1 deletion tests/system/Cache/Handlers/MemcachedHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\Cache\Handlers;

use CodeIgniter\CLI\CLI;
use CodeIgniter\I18n\Time;
use Config\Cache;
use Exception;

Expand Down Expand Up @@ -105,7 +106,7 @@ public function testSavePermanent()
$metaData = $this->handler->getMetaData(self::$key1);

$this->assertNull($metaData['expire']);
$this->assertLessThanOrEqual(1, $metaData['mtime'] - time());
$this->assertLessThanOrEqual(1, $metaData['mtime'] - Time::now()->getTimestamp());
$this->assertSame('value', $metaData['data']);

$this->assertTrue($this->handler->delete(self::$key1));
Expand Down
3 changes: 2 additions & 1 deletion tests/system/Cache/Handlers/PredisHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\Cache\Handlers;

use CodeIgniter\CLI\CLI;
use CodeIgniter\I18n\Time;
use Config\Cache;

/**
Expand Down Expand Up @@ -108,7 +109,7 @@ public function testSavePermanent()
$metaData = $this->handler->getMetaData(self::$key1);

$this->assertNull($metaData['expire']);
$this->assertLessThanOrEqual(1, $metaData['mtime'] - time());
$this->assertLessThanOrEqual(1, $metaData['mtime'] - Time::now()->getTimestamp());
$this->assertSame('value', $metaData['data']);

$this->assertTrue($this->handler->delete(self::$key1));
Expand Down
3 changes: 2 additions & 1 deletion tests/system/Cache/Handlers/RedisHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\Cache\Handlers;

use CodeIgniter\CLI\CLI;
use CodeIgniter\I18n\Time;
use Config\Cache;

/**
Expand Down Expand Up @@ -112,7 +113,7 @@ public function testSavePermanent()
$metaData = $this->handler->getMetaData(self::$key1);

$this->assertNull($metaData['expire']);
$this->assertLessThanOrEqual(1, $metaData['mtime'] - time());
$this->assertLessThanOrEqual(1, $metaData['mtime'] - Time::now()->getTimestamp());
$this->assertSame('value', $metaData['data']);

$this->assertTrue($this->handler->delete(self::$key1));
Expand Down