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:
黄朝晖 2019-12-11 16:49:04 +08:00 committed by GitHub
commit 790ec92432
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 23 additions and 18 deletions

View File

@ -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;
}
}

View File

@ -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
*/

View File

@ -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;

View File

@ -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;

View File

@ -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);

View File

@ -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()

View File

@ -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();
}