4050b41036338463090edb2ae5e9b6546b2882c6
[yaffs-website] / web / modules / contrib / devel / webprofiler / src / Cache / CacheBackendWrapper.php
1 <?php
2
3 namespace Drupal\webprofiler\Cache;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Cache\CacheBackendInterface;
7 use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
8 use Drupal\webprofiler\DataCollector\CacheDataCollector;
9
10 /**
11  * Wraps an existing cache backend to track calls to the cache backend.
12  */
13 class CacheBackendWrapper implements CacheBackendInterface, CacheTagsInvalidatorInterface {
14
15   /**
16    * The data collector to register the calls.
17    *
18    * @var \Drupal\webprofiler\DataCollector\CacheDataCollector
19    */
20   protected $cacheDataCollector;
21
22   /**
23    * The wrapped cache backend.
24    *
25    * @var \Drupal\Core\Cache\CacheBackendInterface
26    */
27   protected $cacheBackend;
28
29   /**
30    * The name of the wrapped cache bin.
31    *
32    * @var string
33    */
34   protected $bin;
35
36   /**
37    * Constructs a new CacheBackendWrapper.
38    *
39    * @param \Drupal\webprofiler\DataCollector\CacheDataCollector $cacheDataCollector
40    *   The cache data collector to inform about cache get calls.
41    * @param \Drupal\Core\Cache\CacheBackendInterface $cacheBackend
42    *   The wrapped cache backend.
43    * @param string $bin
44    *   The name of the wrapped cache bin.
45    */
46   public function __construct(CacheDataCollector $cacheDataCollector, CacheBackendInterface $cacheBackend, $bin) {
47     $this->cacheDataCollector = $cacheDataCollector;
48     $this->cacheBackend = $cacheBackend;
49     $this->bin = $bin;
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public function get($cid, $allow_invalid = FALSE) {
56     $cache = $this->cacheBackend->get($cid, $allow_invalid);
57
58     if ($cache) {
59       $cacheCopy = new \stdClass();
60       $cacheCopy->cid = $cache->cid;
61       $cacheCopy->expire = $cache->expire;
62       $cacheCopy->tags = $cache->tags;
63
64       $this->cacheDataCollector->registerCacheHit($this->bin, $cacheCopy);
65     }
66     else {
67       $this->cacheDataCollector->registerCacheMiss($this->bin, $cid);
68     }
69
70     return $cache;
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function getMultiple(&$cids, $allow_invalid = FALSE) {
77     $cidsCopy = $cids;
78     $cache = $this->cacheBackend->getMultiple($cids, $allow_invalid);
79
80     foreach ($cidsCopy as $cid) {
81       if (in_array($cid, $cids)) {
82         $this->cacheDataCollector->registerCacheMiss($this->bin, $cid);
83       }
84       else {
85         $cacheCopy = new \stdClass();
86         $cacheCopy->cid = $cache[$cid]->cid;
87         $cacheCopy->expire = $cache[$cid]->expire;
88         $cacheCopy->tags = $cache[$cid]->tags;
89
90         $this->cacheDataCollector->registerCacheHit($this->bin, $cacheCopy);
91       }
92     }
93
94     return $cache;
95   }
96
97   /**
98    * {@inheritdoc}
99    */
100   public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = []) {
101     return $this->cacheBackend->set($cid, $data, $expire, $tags);
102   }
103
104   /**
105    * {@inheritdoc}
106    */
107   public function setMultiple(array $items) {
108     return $this->cacheBackend->setMultiple($items);
109   }
110
111   /**
112    * {@inheritdoc}
113    */
114   public function delete($cid) {
115     return $this->cacheBackend->delete($cid);
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   public function deleteMultiple(array $cids) {
122     return $this->cacheBackend->deleteMultiple($cids);
123   }
124
125   /**
126    * {@inheritdoc}
127    */
128   public function deleteAll() {
129     return $this->cacheBackend->deleteAll();
130   }
131
132   /**
133    * {@inheritdoc}
134    */
135   public function invalidate($cid) {
136     return $this->cacheBackend->invalidate($cid);
137   }
138
139   /**
140    * {@inheritdoc}
141    */
142   public function invalidateMultiple(array $cids) {
143     return $this->cacheBackend->invalidateMultiple($cids);
144   }
145
146   /**
147    * {@inheritdoc}
148    */
149   public function invalidateTags(array $tags) {
150     if ($this->cacheBackend instanceof CacheTagsInvalidatorInterface) {
151       $this->cacheBackend->invalidateTags($tags);
152     }
153   }
154
155   /**
156    * {@inheritdoc}
157    */
158   public function invalidateAll() {
159     return $this->cacheBackend->invalidateAll();
160   }
161
162   /**
163    * {@inheritdoc}
164    */
165   public function garbageCollection() {
166     return $this->cacheBackend->garbageCollection();
167   }
168
169   /**
170    * {@inheritdoc}
171    */
172   public function removeBin() {
173     return $this->cacheBackend->removeBin();
174   }
175
176 }