CachePutAspect.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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\Aspect;
  12. use Hyperf\Cache\Annotation\CachePut;
  13. use Hyperf\Cache\AnnotationManager;
  14. use Hyperf\Cache\CacheManager;
  15. use Hyperf\Di\Aop\AbstractAspect;
  16. use Hyperf\Di\Aop\ProceedingJoinPoint;
  17. class CachePutAspect extends AbstractAspect
  18. {
  19. public array $annotations = [
  20. CachePut::class,
  21. ];
  22. public function __construct(protected CacheManager $manager, protected AnnotationManager $annotationManager)
  23. {
  24. }
  25. public function process(ProceedingJoinPoint $proceedingJoinPoint)
  26. {
  27. $className = $proceedingJoinPoint->className;
  28. $method = $proceedingJoinPoint->methodName;
  29. $arguments = $proceedingJoinPoint->arguments['keys'];
  30. /** @var CachePut $annotation */
  31. [$key, $ttl, $group, $annotation] = $this->annotationManager->getCachePutValue($className, $method, $arguments);
  32. $driver = $this->manager->getDriver($group);
  33. $result = $proceedingJoinPoint->process();
  34. if (! in_array($result, (array) $annotation->skipCacheResults, true)) {
  35. $driver->set($key, $result, $ttl);
  36. }
  37. return $result;
  38. }
  39. }