3 namespace Drupal\webprofiler\Cache;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Cache\CacheBackendInterface;
7 use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
8 use Drupal\webprofiler\DataCollector\CacheDataCollector;
11 * Wraps an existing cache backend to track calls to the cache backend.
13 class CacheBackendWrapper implements CacheBackendInterface, CacheTagsInvalidatorInterface {
16 * The data collector to register the calls.
18 * @var \Drupal\webprofiler\DataCollector\CacheDataCollector
20 protected $cacheDataCollector;
23 * The wrapped cache backend.
25 * @var \Drupal\Core\Cache\CacheBackendInterface
27 protected $cacheBackend;
30 * The name of the wrapped cache bin.
37 * Constructs a new CacheBackendWrapper.
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.
44 * The name of the wrapped cache bin.
46 public function __construct(CacheDataCollector $cacheDataCollector, CacheBackendInterface $cacheBackend, $bin) {
47 $this->cacheDataCollector = $cacheDataCollector;
48 $this->cacheBackend = $cacheBackend;
55 public function get($cid, $allow_invalid = FALSE) {
56 $cache = $this->cacheBackend->get($cid, $allow_invalid);
59 $cacheCopy = new \stdClass();
60 $cacheCopy->cid = $cache->cid;
61 $cacheCopy->expire = $cache->expire;
62 $cacheCopy->tags = $cache->tags;
64 $this->cacheDataCollector->registerCacheHit($this->bin, $cacheCopy);
67 $this->cacheDataCollector->registerCacheMiss($this->bin, $cid);
76 public function getMultiple(&$cids, $allow_invalid = FALSE) {
78 $cache = $this->cacheBackend->getMultiple($cids, $allow_invalid);
80 foreach ($cidsCopy as $cid) {
81 if (in_array($cid, $cids)) {
82 $this->cacheDataCollector->registerCacheMiss($this->bin, $cid);
85 $cacheCopy = new \stdClass();
86 $cacheCopy->cid = $cache[$cid]->cid;
87 $cacheCopy->expire = $cache[$cid]->expire;
88 $cacheCopy->tags = $cache[$cid]->tags;
90 $this->cacheDataCollector->registerCacheHit($this->bin, $cacheCopy);
100 public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = []) {
101 return $this->cacheBackend->set($cid, $data, $expire, $tags);
107 public function setMultiple(array $items) {
108 return $this->cacheBackend->setMultiple($items);
114 public function delete($cid) {
115 return $this->cacheBackend->delete($cid);
121 public function deleteMultiple(array $cids) {
122 return $this->cacheBackend->deleteMultiple($cids);
128 public function deleteAll() {
129 return $this->cacheBackend->deleteAll();
135 public function invalidate($cid) {
136 return $this->cacheBackend->invalidate($cid);
142 public function invalidateMultiple(array $cids) {
143 return $this->cacheBackend->invalidateMultiple($cids);
149 public function invalidateTags(array $tags) {
150 if ($this->cacheBackend instanceof CacheTagsInvalidatorInterface) {
151 $this->cacheBackend->invalidateTags($tags);
158 public function invalidateAll() {
159 return $this->cacheBackend->invalidateAll();
165 public function garbageCollection() {
166 return $this->cacheBackend->garbageCollection();
172 public function removeBin() {
173 return $this->cacheBackend->removeBin();