LoginCache.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * @package Grav\Plugin\Login
  4. *
  5. * @copyright Copyright (C) 2014 - 2017 RocketTheme, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Plugin\Login;
  9. use Doctrine\Common\Cache\FilesystemCache;
  10. use Grav\Common\Grav;
  11. /**
  12. * PSR-16 forward compatible cache.
  13. * @package Grav\Plugin\Login
  14. */
  15. class LoginCache
  16. {
  17. /**
  18. * @var FilesystemCache
  19. */
  20. protected $driver;
  21. protected $lifetime;
  22. /**
  23. * @param string $namespace
  24. * @param null|int $defaultLifetime
  25. * @throws \InvalidArgumentException
  26. */
  27. public function __construct($namespace, $defaultLifetime = null)
  28. {
  29. // Setup cache
  30. $this->lifetime = (int)$defaultLifetime;
  31. $this->driver = new FilesystemCache(Grav::instance()['locator']->findResource('cache://login/' . $namespace, true, true));
  32. }
  33. /**
  34. * Fetches a value from the cache.
  35. *
  36. * @param string $key The unique key of this item in the cache.
  37. * @param mixed $default Default value to return if the key does not exist.
  38. *
  39. * @return mixed The value of the item from the cache, or $default in case of cache miss.
  40. */
  41. public function get($key, $default = null)
  42. {
  43. $value = $this->driver->fetch($key);
  44. // Doctrine cache does not differentiate between no result and cached 'false'. Make sure that we do.
  45. return $value !== false || $this->driver->contains($key) ? $value : $default;
  46. }
  47. /**
  48. * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
  49. *
  50. * @param string $key The key of the item to store.
  51. * @param mixed $value The value of the item to store, must be serializable.
  52. * @param null|int $ttl Optional. The TTL value of this item.
  53. *
  54. * @return bool True on success and false on failure.
  55. */
  56. public function set($key, $value, $ttl = null)
  57. {
  58. $ttl = $ttl !== null ? (int)$ttl : $this->lifetime;
  59. return $this->driver->save($key, $value, $ttl);
  60. }
  61. /**
  62. * Determines whether an item is present in the cache.
  63. *
  64. * @param string $key The cache item key.
  65. *
  66. * @return bool
  67. */
  68. public function has($key)
  69. {
  70. return $this->driver->contains($key);
  71. }
  72. /**
  73. * Delete an item from the cache by its unique key.
  74. *
  75. * @param string $key The unique cache key of the item to delete.
  76. *
  77. * @return bool True if the item was successfully removed. False if there was an error.
  78. */
  79. public function delete($key)
  80. {
  81. return $this->driver->delete($key);
  82. }
  83. /**
  84. * Wipes clean the entire cache's keys.
  85. *
  86. * @return bool True on success and false on failure.
  87. */
  88. public function clear()
  89. {
  90. return $this->driver->flushAll();
  91. }
  92. }