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