mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-05 05:07:58 +08:00
Merge pull request #1195 from limingxinleo/1.1-cache
Added max offset for Cacheable and CachePut.
This commit is contained in:
commit
138bfa9325
@ -1,5 +1,9 @@
|
||||
# v1.1.13 - TBD
|
||||
|
||||
## Added
|
||||
|
||||
- [#1195](https://github.com/hyperf/hyperf/pull/1195) Added max offset for `Cacheable` and `CachePut`.
|
||||
|
||||
## Fixed
|
||||
|
||||
- [#1175](https://github.com/hyperf/hyperf/pull/1175) Fixed `Hyperf\Utils\Collection::random` does not works when the number is null.
|
||||
|
7
src/cache/src/Annotation/CachePut.php
vendored
7
src/cache/src/Annotation/CachePut.php
vendored
@ -35,6 +35,12 @@ class CachePut extends AbstractAnnotation
|
||||
*/
|
||||
public $ttl;
|
||||
|
||||
/**
|
||||
* The max offset for ttl.
|
||||
* @var int
|
||||
*/
|
||||
public $offset = 0;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
@ -44,5 +50,6 @@ class CachePut extends AbstractAnnotation
|
||||
{
|
||||
parent::__construct($value);
|
||||
$this->ttl = (int) $this->ttl;
|
||||
$this->offset = (int) $this->offset;
|
||||
}
|
||||
}
|
||||
|
7
src/cache/src/Annotation/Cacheable.php
vendored
7
src/cache/src/Annotation/Cacheable.php
vendored
@ -42,6 +42,12 @@ class Cacheable extends AbstractAnnotation
|
||||
*/
|
||||
public $listener;
|
||||
|
||||
/**
|
||||
* The max offset for ttl.
|
||||
* @var int
|
||||
*/
|
||||
public $offset = 0;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
@ -56,6 +62,7 @@ class Cacheable extends AbstractAnnotation
|
||||
{
|
||||
parent::__construct($value);
|
||||
$this->ttl = (int) $this->ttl;
|
||||
$this->offset = (int) $this->offset;
|
||||
}
|
||||
|
||||
public function collectMethod(string $className, ?string $target): void
|
||||
|
13
src/cache/src/AnnotationManager.php
vendored
13
src/cache/src/AnnotationManager.php
vendored
@ -50,7 +50,7 @@ class AnnotationManager
|
||||
$group = $annotation->group;
|
||||
$ttl = $annotation->ttl ?? $this->config->get("cache.{$group}.ttl", 3600);
|
||||
|
||||
return [$key, $ttl, $group, $annotation];
|
||||
return [$key, $ttl + $this->getRandomOffset($annotation->offset), $group, $annotation];
|
||||
}
|
||||
|
||||
public function getCacheEvictValue(string $className, string $method, array $arguments): array
|
||||
@ -79,7 +79,7 @@ class AnnotationManager
|
||||
$group = $annotation->group;
|
||||
$ttl = $annotation->ttl ?? $this->config->get("cache.{$group}.ttl", 3600);
|
||||
|
||||
return [$key, $ttl, $group, $annotation];
|
||||
return [$key, $ttl + $this->getRandomOffset($annotation->offset), $group, $annotation];
|
||||
}
|
||||
|
||||
public function getFailCacheValue(string $className, string $method, array $arguments): array
|
||||
@ -95,6 +95,15 @@ class AnnotationManager
|
||||
return [$key, $ttl, $group, $annotation];
|
||||
}
|
||||
|
||||
protected function getRandomOffset(int $offset): int
|
||||
{
|
||||
if ($offset > 0) {
|
||||
return rand(0, $offset);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
protected function getAnnotation(string $annotation, string $className, string $method): AbstractAnnotation
|
||||
{
|
||||
$collector = AnnotationCollector::get($className);
|
||||
|
35
src/cache/tests/Cases/AnnotationTest.php
vendored
35
src/cache/tests/Cases/AnnotationTest.php
vendored
@ -14,6 +14,10 @@ namespace HyperfTest\Cache\Cases;
|
||||
|
||||
use Hyperf\Cache\Annotation\Cacheable;
|
||||
use Hyperf\Cache\Annotation\CachePut;
|
||||
use Hyperf\Cache\AnnotationManager;
|
||||
use Hyperf\Contract\ConfigInterface;
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Mockery;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
@ -43,9 +47,40 @@ class AnnotationTest extends TestCase
|
||||
$annotation = new CachePut([
|
||||
'prefix' => 'test',
|
||||
'ttl' => '3600',
|
||||
'offset' => '100',
|
||||
]);
|
||||
|
||||
$this->assertSame('test', $annotation->prefix);
|
||||
$this->assertSame(3600, $annotation->ttl);
|
||||
$this->assertSame(100, $annotation->offset);
|
||||
}
|
||||
|
||||
public function testAnnotationManager()
|
||||
{
|
||||
$cacheable = new Cacheable(['prefix' => 'test', 'ttl' => 3600, 'offset' => 100]);
|
||||
$cacheable2 = new Cacheable(['prefix' => 'test', 'ttl' => 3600]);
|
||||
$cacheput = new CachePut(['prefix' => 'test', 'ttl' => 3600, 'offset' => 100]);
|
||||
$config = Mockery::mock(ConfigInterface::class);
|
||||
$logger = Mockery::mock(StdoutLoggerInterface::class);
|
||||
/** @var AnnotationManager $manager */
|
||||
$manager = Mockery::mock(AnnotationManager::class . '[getAnnotation]', [$config, $logger]);
|
||||
$manager->shouldAllowMockingProtectedMethods();
|
||||
$manager->shouldReceive('getAnnotation')->with(Cacheable::class, Mockery::any(), Mockery::any())->once()->andReturn($cacheable);
|
||||
$manager->shouldReceive('getAnnotation')->with(Cacheable::class, Mockery::any(), Mockery::any())->once()->andReturn($cacheable2);
|
||||
$manager->shouldReceive('getAnnotation')->with(CachePut::class, Mockery::any(), Mockery::any())->once()->andReturn($cacheput);
|
||||
|
||||
[$key, $ttl] = $manager->getCacheableValue('Foo', 'test', ['id' => $id = uniqid()]);
|
||||
$this->assertSame('test:' . $id, $key);
|
||||
$this->assertGreaterThanOrEqual(3600, $ttl);
|
||||
$this->assertLessThanOrEqual(3700, $ttl);
|
||||
|
||||
[$key, $ttl] = $manager->getCachePutValue('Foo', 'test', ['id' => $id = uniqid()]);
|
||||
$this->assertSame('test:' . $id, $key);
|
||||
$this->assertGreaterThanOrEqual(3600, $ttl);
|
||||
$this->assertLessThanOrEqual(3700, $ttl);
|
||||
|
||||
[$key, $ttl] = $manager->getCacheableValue('Foo', 'test', ['id' => $id = uniqid()]);
|
||||
$this->assertSame('test:' . $id, $key);
|
||||
$this->assertSame(3600, $ttl);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user