Commit 32c941f2 authored by mrweiner's avatar mrweiner

Add cache tags to Slots from their Drawer, drawer invalidation

parent 7712aad1
......@@ -2,6 +2,7 @@
namespace Drupal\cache_register;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
class Drawer implements DrawerInterface {
......@@ -20,6 +21,13 @@ class Drawer implements DrawerInterface {
*/
protected $id;
/**
* The cache tags associated with the drawer.
*
* @var array
*/
protected $cacheTags = [];
/**
* The constructor.
*
......@@ -35,6 +43,7 @@ class Drawer implements DrawerInterface {
$this->cache = $cache;
// Set to null to help flag if it wasn't implemented in child class
$this->id = $drawer_id;
$this->populateCacheTags();
}
/**
......@@ -54,10 +63,19 @@ class Drawer implements DrawerInterface {
/**
* {@inheritDoc}
*/
public function invalidateAll() {
foreach ($this->slots as $slot) {
$slot->invalidateCache();
}
public function invalidateSlots() {
Cache::invalidateTags($this->cacheTags);
}
/**
* {@inheritDoc}
*/
public function getCacheTags() {
return $this->cacheTags;
}
private function populateCacheTags() {
$this->cacheTags[] = 'drawer:' . $this->id;
}
}
......@@ -33,6 +33,14 @@ interface DrawerInterface {
*
* @return mixed
*/
public function invalidateAll();
public function invalidateSlots();
/**
* Gets the cache tags associated with the drawer.
*
* @return array
* The cache tag array.
*/
public function getCacheTags();
}
......@@ -2,6 +2,7 @@
namespace Drupal\cache_register;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
class Slot implements SlotInterface {
......@@ -18,44 +19,55 @@ class Slot implements SlotInterface {
*
* @var \Drupal\cache_register\DrawerInterface
*/
protected $parentCacheRegister;
protected $drawer;
/**
* The name of the item in the cache.
*
* @var string
*/
protected $cacheEntryId;
protected $id;
/**
* T
*
* @var string
*/
protected $drawerCacheTag;
/**
* The constructor.
*
* @param \Drupal\Core\Cache\CacheBackendInterface $cache
* Cache backend.
* @param DrawerInterface $parent_cache_register
* @param DrawerInterface $parent_drawer
* The parent CacheRegister object.
* @param array|string $slot_ids
* The ID(s) to use for the slot's cache identifier.
*/
public function __construct(CacheBackendInterface $cache, DrawerInterface $parent_cache_register, string $item_ids) {
public function __construct(CacheBackendInterface $cache, DrawerInterface $parent_drawer, string $slot_ids) {
$this->cache = $cache;
$this->parentCacheRegister = $parent_cache_register;
$this->cacheEntryId = $this->constructCacheEntryId($slot_ids);
$this->drawer = $parent_drawer;
$this->id = $this->constructCacheEntryId($slot_ids);
}
/**
* {@inheritDoc}
*/
public function getCacheEntryId() {
return $this->cacheEntryId;
public function id() {
return $this->id;
}
public function getDrawer() {
return $this->drawer;
}
/**
* {@inheritDoc}
*/
public function isCached() {
$data_cached = $this->cache->get($this->cacheEntryId);
$data_cached = $this->cache->get($this->id);
if ($data_cached) {
return TRUE;
}
......@@ -66,22 +78,8 @@ class Slot implements SlotInterface {
* {@inheritDoc}
*/
public function setCache($data, $expire = CacheBackendInterface::CACHE_PERMANENT, $tags = []) {
$this->cache->set($this->cacheEntryId, $data, $expire, $tags);
}
/**
* {@inheritDoc}
*/
public function setCacheTags($tags) {
if (!$cache_item = $this->getCache()) {
return FALSE;
}
if (is_string($tags)) {
$tags = [$tags];
}
$this->setCache($cache_item->data, $cache_item->expire, $tags);
return TRUE;
$tags = Cache::mergeTags($tags, $this->getDrawer()->getCacheTags());
$this->cache->set($this->id, $data, $expire, $tags);
}
/**
......@@ -98,7 +96,7 @@ class Slot implements SlotInterface {
$new_tags = is_string($tags) ? [$tags] : $tags;
$existing_tags = $cache_item->tags;
$updated_tags = array_merge($existing_tags, $new_tags);
$updated_tags = Cache::mergeTags($new_tags, $existing_tags);
$this->setCache($cache_item->data, $cache_item->expire, $updated_tags);
return TRUE;
......@@ -109,21 +107,21 @@ class Slot implements SlotInterface {
* {@inheritDoc}
*/
public function getCache() {
return $this->cache->get($this->cacheEntryId);
return $this->cache->get($this->id);
}
/**
* {@inheritDoc}
*/
public function deleteCache() {
$this->cache->delete($this->cacheEntryId);
$this->cache->delete($this->id);
}
/**
* {@inheritDoc}
*/
public function invalidateCache() {
$this->cache->invalidate($this->cacheEntryId);
$this->cache->invalidate($this->id);
}
/**
......@@ -139,7 +137,7 @@ class Slot implements SlotInterface {
*/
private function constructCacheEntryId($slot_cache_ids) {
$cache_id = is_array($slot_cache_ids) ? implode('.', $slot_cache_ids) : $slot_cache_ids;
return "{$this->parentCacheRegister->id()}:$cache_id";
return "{$this->drawer->id()}:$cache_id";
}
}
......@@ -40,17 +40,6 @@ interface SlotInterface {
*/
public function setCache($data, int $expire = CacheBackendInterface::CACHE_PERMANENT, array $tags = []);
/**
* Sets the given tags on the slot's cache entry.
*
* @param array|string $tags
* An array of tags to add to the cache item.
*
* @return bool
* False if the item doesn't exist yet.
*/
public function setCacheTags($tags);
/**
* Adds the given tags to the slot's cache entry.
*
......@@ -84,7 +73,16 @@ interface SlotInterface {
* Get the ID of the slot's cache entry.
*
* @return string
* The ID.
*/
public function id();
/**
* Gets the slots parent drawer.
*
* @return \Drupal\cache_register\DrawerInterface
* The drawer.
*/
public function getCacheEntryId();
public function getDrawer();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment