Cacheable.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Cache\Annotation;
  12. use Attribute;
  13. use Hyperf\Cache\CacheListenerCollector;
  14. use Hyperf\Di\Annotation\AbstractAnnotation;
  15. use Hyperf\Di\Annotation\AnnotationCollector;
  16. #[Attribute(Attribute::TARGET_METHOD)]
  17. class Cacheable extends AbstractAnnotation
  18. {
  19. /**
  20. * @param null|int $ttl the max offset for ttl
  21. */
  22. public function __construct(
  23. public ?string $prefix = null,
  24. public ?string $value = null,
  25. public ?int $ttl = null,
  26. public ?string $listener = null,
  27. public int $offset = 0,
  28. public string $group = 'default',
  29. public bool $collect = false,
  30. public ?array $skipCacheResults = null
  31. ) {
  32. }
  33. public function collectMethod(string $className, ?string $target): void
  34. {
  35. if (isset($this->listener)) {
  36. CacheListenerCollector::setListener($this->listener, [
  37. 'className' => $className,
  38. 'method' => $target,
  39. ]);
  40. }
  41. AnnotationCollector::collectMethod($className, $target, static::class, $this);
  42. }
  43. }