StaticFileCacheBackend.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace Drupal\Tests\Component\FileCache;
  3. use Drupal\Component\FileCache\FileCacheBackendInterface;
  4. /**
  5. * Allows to cache data based on file modification dates in a static cache.
  6. */
  7. class StaticFileCacheBackend implements FileCacheBackendInterface {
  8. /**
  9. * Internal static cache.
  10. *
  11. * @var array
  12. */
  13. protected static $cache = [];
  14. /**
  15. * Bin used for storing the data in the static cache.
  16. *
  17. * @var string
  18. */
  19. protected $bin;
  20. /**
  21. * Constructs a PHP Storage FileCache backend.
  22. *
  23. * @param array $configuration
  24. * (optional) Configuration used to configure this object.
  25. */
  26. public function __construct($configuration) {
  27. $this->bin = isset($configuration['bin']) ? $configuration['bin'] : 'file_cache';
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function fetch(array $cids) {
  33. $result = [];
  34. foreach ($cids as $cid) {
  35. if (isset(static::$cache[$this->bin][$cid])) {
  36. $result[$cid] = static::$cache[$this->bin][$cid];
  37. }
  38. }
  39. return $result;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function store($cid, $data) {
  45. static::$cache[$this->bin][$cid] = $data;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function delete($cid) {
  51. unset(static::$cache[$this->bin][$cid]);
  52. }
  53. /**
  54. * Allows tests to reset the static cache to avoid side effects.
  55. */
  56. public static function reset() {
  57. static::$cache = [];
  58. }
  59. }