From be050a6554f134d0bbb11e67f8334e23fd6588f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Fri, 27 Dec 2019 12:51:19 +0800 Subject: [PATCH 1/3] Added offset for Cacheable and CachePut. --- src/cache/src/Annotation/CachePut.php | 7 +++++++ src/cache/src/Annotation/Cacheable.php | 7 +++++++ src/cache/src/AnnotationManager.php | 13 +++++++++++-- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/cache/src/Annotation/CachePut.php b/src/cache/src/Annotation/CachePut.php index 440f75903..67d325e23 100644 --- a/src/cache/src/Annotation/CachePut.php +++ b/src/cache/src/Annotation/CachePut.php @@ -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; } } diff --git a/src/cache/src/Annotation/Cacheable.php b/src/cache/src/Annotation/Cacheable.php index 7a4397cf4..ea8483fa6 100644 --- a/src/cache/src/Annotation/Cacheable.php +++ b/src/cache/src/Annotation/Cacheable.php @@ -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 diff --git a/src/cache/src/AnnotationManager.php b/src/cache/src/AnnotationManager.php index a657aa78d..89a5ebccb 100644 --- a/src/cache/src/AnnotationManager.php +++ b/src/cache/src/AnnotationManager.php @@ -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); From 7d5394bd4c526c56fc3a918517482001c7291aef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Fri, 27 Dec 2019 13:37:21 +0800 Subject: [PATCH 2/3] Added test cases. --- src/cache/tests/Cases/AnnotationTest.php | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/cache/tests/Cases/AnnotationTest.php b/src/cache/tests/Cases/AnnotationTest.php index 8db67f16b..dd7e08e04 100644 --- a/src/cache/tests/Cases/AnnotationTest.php +++ b/src/cache/tests/Cases/AnnotationTest.php @@ -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); } } From 5268176d7e3add5a5c91af1f6d64992f9b77450f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Mon, 30 Dec 2019 09:44:03 +0800 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f476d0145..53d9dd54f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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.