Psr6Cache.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace League\Flysystem\Cached\Storage;
  3. use Psr\Cache\CacheItemPoolInterface;
  4. class Psr6Cache extends AbstractCache
  5. {
  6. /**
  7. * @var CacheItemPoolInterface
  8. */
  9. private $pool;
  10. /**
  11. * @var string storage key
  12. */
  13. protected $key;
  14. /**
  15. * @var int|null seconds until cache expiration
  16. */
  17. protected $expire;
  18. /**
  19. * Constructor.
  20. *
  21. * @param CacheItemPoolInterface $pool
  22. * @param string $key storage key
  23. * @param int|null $expire seconds until cache expiration
  24. */
  25. public function __construct(CacheItemPoolInterface $pool, $key = 'flysystem', $expire = null)
  26. {
  27. $this->pool = $pool;
  28. $this->key = $key;
  29. $this->expire = $expire;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function save()
  35. {
  36. $item = $this->pool->getItem($this->key);
  37. $item->set($this->getForStorage());
  38. $item->expiresAfter($this->expire);
  39. $this->pool->save($item);
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function load()
  45. {
  46. $item = $this->pool->getItem($this->key);
  47. if ($item->isHit()) {
  48. $this->setFromStorage($item->get());
  49. }
  50. }
  51. }