AbstractApiCache.php 731 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace WeWork\ApiCache;
  3. use WeWork\Traits\CacheTrait;
  4. abstract class AbstractApiCache
  5. {
  6. use CacheTrait;
  7. /**
  8. * @return string
  9. */
  10. abstract protected function getCacheKey(): string;
  11. /**
  12. * @return mixed
  13. */
  14. abstract protected function getFromServer();
  15. /**
  16. * @param bool $refresh
  17. * @return mixed
  18. * @throws \Psr\SimpleCache\InvalidArgumentException
  19. */
  20. public function get(bool $refresh = false)
  21. {
  22. $key = $this->getCacheKey();
  23. $value = $this->cache->get($key);
  24. if ($refresh || !$value) {
  25. $value = $this->getFromServer();
  26. $this->cache->set($key, $value, 7100);
  27. }
  28. return $value;
  29. }
  30. }