PhpRedis.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace League\Flysystem\Cached\Storage;
  3. use Redis;
  4. class PhpRedis extends AbstractCache
  5. {
  6. /**
  7. * @var Redis PhpRedis Client
  8. */
  9. protected $client;
  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 Redis|null $client phpredis client
  22. * @param string $key storage key
  23. * @param int|null $expire seconds until cache expiration
  24. */
  25. public function __construct(Redis $client = null, $key = 'flysystem', $expire = null)
  26. {
  27. $this->client = $client ?: new Redis();
  28. $this->key = $key;
  29. $this->expire = $expire;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function load()
  35. {
  36. $contents = $this->client->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. $this->client->set($this->key, $contents);
  48. if ($this->expire !== null) {
  49. $this->client->expire($this->key, $this->expire);
  50. }
  51. }
  52. }