Memcached.php 1.2 KB

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