mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-05 05:07:58 +08:00
Merge pull request #1116 from donjan-deng/pr/hyperf/cache
Changed filesystemcache's rule to be consistent with rediscahche's rule.
This commit is contained in:
commit
790ec92432
2
src/cache/src/Collector/FileStorage.php
vendored
2
src/cache/src/Collector/FileStorage.php
vendored
@ -21,7 +21,7 @@ class FileStorage
|
||||
public function __construct($data, $ttl)
|
||||
{
|
||||
$this->data = $data;
|
||||
if (is_numeric($ttl)) {
|
||||
if (is_numeric($ttl) && $ttl > 0) {
|
||||
$this->expiredTime = time() + $ttl;
|
||||
}
|
||||
}
|
||||
|
3
src/cache/src/Driver/Driver.php
vendored
3
src/cache/src/Driver/Driver.php
vendored
@ -13,11 +13,14 @@ declare(strict_types=1);
|
||||
namespace Hyperf\Cache\Driver;
|
||||
|
||||
use Hyperf\Contract\PackerInterface;
|
||||
use Hyperf\Utils\InteractsWithTime;
|
||||
use Hyperf\Utils\Packer\PhpSerializerPacker;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
abstract class Driver implements DriverInterface
|
||||
{
|
||||
use InteractsWithTime;
|
||||
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
|
7
src/cache/src/Driver/FileSystemDriver.php
vendored
7
src/cache/src/Driver/FileSystemDriver.php
vendored
@ -74,8 +74,9 @@ class FileSystemDriver extends Driver
|
||||
|
||||
public function set($key, $value, $ttl = null)
|
||||
{
|
||||
$seconds = $this->secondsUntil($ttl);
|
||||
$file = $this->getCacheKey($key);
|
||||
$content = $this->packer->pack(new FileStorage($value, $ttl));
|
||||
$content = $this->packer->pack(new FileStorage($value, $seconds));
|
||||
|
||||
$result = file_put_contents($file, $content, FILE_BINARY);
|
||||
|
||||
@ -119,9 +120,9 @@ class FileSystemDriver extends Driver
|
||||
if (! is_array($values)) {
|
||||
throw new InvalidArgumentException('The values is invalid!');
|
||||
}
|
||||
|
||||
$seconds = $this->secondsUntil($ttl);
|
||||
foreach ($values as $key => $value) {
|
||||
$this->set($key, $value, $ttl);
|
||||
$this->set($key, $value, $seconds);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
11
src/cache/src/Driver/RedisDriver.php
vendored
11
src/cache/src/Driver/RedisDriver.php
vendored
@ -51,9 +51,10 @@ class RedisDriver extends Driver implements KeyCollectorInterface
|
||||
|
||||
public function set($key, $value, $ttl = null)
|
||||
{
|
||||
$seconds = $this->secondsUntil($ttl);
|
||||
$res = $this->packer->pack($value);
|
||||
if ($ttl > 0) {
|
||||
return $this->redis->set($this->getCacheKey($key), $res, $ttl);
|
||||
if ($seconds > 0) {
|
||||
return $this->redis->set($this->getCacheKey($key), $res, $seconds);
|
||||
}
|
||||
|
||||
return $this->redis->set($this->getCacheKey($key), $res);
|
||||
@ -95,10 +96,10 @@ class RedisDriver extends Driver implements KeyCollectorInterface
|
||||
$cacheKeys[$this->getCacheKey($key)] = $this->packer->pack($value);
|
||||
}
|
||||
|
||||
$ttl = (int) $ttl;
|
||||
if ($ttl > 0) {
|
||||
$seconds = $this->secondsUntil($ttl);
|
||||
if ($seconds > 0) {
|
||||
foreach ($cacheKeys as $key => $value) {
|
||||
$this->redis->set($key, $value, $ttl);
|
||||
$this->redis->set($key, $value, $seconds);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -68,12 +68,12 @@ class FileSystemDriverTest extends TestCase
|
||||
$container = $this->getContainer();
|
||||
$driver = $container->get(CacheManager::class)->getDriver();
|
||||
|
||||
$driver->set('xxx', 'yyy', 0.5);
|
||||
$driver->set('xxx', 'yyy', 1);
|
||||
[$bool, $result] = $driver->fetch('xxx');
|
||||
$this->assertTrue($bool);
|
||||
$this->assertSame('yyy', $result);
|
||||
|
||||
sleep(1);
|
||||
sleep(2);
|
||||
|
||||
[$bool, $result] = $driver->fetch('xxx');
|
||||
$this->assertFalse($bool);
|
||||
|
4
src/cache/tests/Cases/RedisDriverTest.php
vendored
4
src/cache/tests/Cases/RedisDriverTest.php
vendored
@ -83,6 +83,10 @@ class RedisDriverTest extends TestCase
|
||||
|
||||
$redis = $container->get(\Redis::class);
|
||||
$this->assertSame(1, $redis->ttl('c:xxx'));
|
||||
|
||||
$dv = new \DateInterval('PT5S');
|
||||
$driver->set('xxx', 'yyy', $dv);
|
||||
$this->assertSame(5, $redis->ttl('c:xxx'));
|
||||
}
|
||||
|
||||
public function testDelete()
|
||||
|
@ -22,9 +22,8 @@ trait InteractsWithTime
|
||||
* Get the number of seconds until the given DateTime.
|
||||
*
|
||||
* @param \DateInterval|\DateTimeInterface|int $delay
|
||||
* @return int
|
||||
*/
|
||||
protected function secondsUntil($delay)
|
||||
protected function secondsUntil($delay): int
|
||||
{
|
||||
$delay = $this->parseDateInterval($delay);
|
||||
|
||||
@ -37,9 +36,8 @@ trait InteractsWithTime
|
||||
* Get the "available at" UNIX timestamp.
|
||||
*
|
||||
* @param \DateInterval|\DateTimeInterface|int $delay
|
||||
* @return int
|
||||
*/
|
||||
protected function availableAt($delay = 0)
|
||||
protected function availableAt($delay = 0): int
|
||||
{
|
||||
$delay = $this->parseDateInterval($delay);
|
||||
|
||||
@ -65,10 +63,8 @@ trait InteractsWithTime
|
||||
|
||||
/**
|
||||
* Get the current system time as a UNIX timestamp.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function currentTime()
|
||||
protected function currentTime(): int
|
||||
{
|
||||
return Carbon::now()->getTimestamp();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user