Cache.php 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Illuminate\Filesystem;
  3. use Illuminate\Contracts\Cache\Repository;
  4. use League\Flysystem\Cached\Storage\AbstractCache;
  5. class Cache extends AbstractCache
  6. {
  7. /**
  8. * The cache repository implementation.
  9. *
  10. * @var \Illuminate\Contracts\Cache\Repository
  11. */
  12. protected $repository;
  13. /**
  14. * The cache key.
  15. *
  16. * @var string
  17. */
  18. protected $key;
  19. /**
  20. * The cache expiration time in minutes.
  21. *
  22. * @var int
  23. */
  24. protected $expire;
  25. /**
  26. * Create a new cache instance.
  27. *
  28. * @param \Illuminate\Contracts\Cache\Repository $repository
  29. * @param string $key
  30. * @param int|null $expire
  31. */
  32. public function __construct(Repository $repository, $key = 'flysystem', $expire = null)
  33. {
  34. $this->key = $key;
  35. $this->repository = $repository;
  36. if (! is_null($expire)) {
  37. $this->expire = (int) ceil($expire / 60);
  38. }
  39. }
  40. /**
  41. * Load the cache.
  42. *
  43. * @return void
  44. */
  45. public function load()
  46. {
  47. $contents = $this->repository->get($this->key);
  48. if (! is_null($contents)) {
  49. $this->setFromStorage($contents);
  50. }
  51. }
  52. /**
  53. * Persist the cache.
  54. *
  55. * @return void
  56. */
  57. public function save()
  58. {
  59. $contents = $this->getForStorage();
  60. if (! is_null($this->expire)) {
  61. $this->repository->put($this->key, $contents, $this->expire);
  62. } else {
  63. $this->repository->forever($this->key, $contents);
  64. }
  65. }
  66. }