Commit b8acff3e authored by mrweiner's avatar mrweiner

Add WIP module files

parent da70a9d6
name: 'Cache Register'
type: module
description: "Simplifies interactions with Drupal's cache."
core: 8.x
package: 'Custom'
services:
cache_register.factory:
class: Drupal\cache_register\CacheRegisterFactory
arguments: [ '@cache.data' ]
#
# Not really sure if there's a benefit to doing this,
# and also not sure how to inject it if implemented.
#
# cache.cache_register_cache:
# class: Drupal\Core\Cache\CacheBackendInterface
# tags:
# - { name: cache.bin }
# factory: cache_factory:get
# arguments: [ cache_register_cache ]
<?php
namespace Drupal\cache_register;
use Drupal\Core\Cache\CacheBackendInterface;
class CacheRegister implements CacheRegisterInterface {
/**
* The cache backend service.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cache;
/**
* @var string Used to name the cache.
*/
protected $registerName;
/**
* Array of items in the register.
*
* @var CacheRegisterItem[]
*/
protected $registerItems = [];
/**
* The constructor.
*
* @param \Drupal\Core\Cache\CacheBackendInterface $cache
* Cache backend.
*
* @param string $cache_register_name
* Prefixes the cache entry IDs of child CacheRegisterItems.
* This should generally be prefixed by the implementor's name,
* so something like my_module.bookshelf
*/
public function __construct(CacheBackendInterface $cache, string $cache_register_name) {
$this->cache = $cache;
// Set to null to help flag if it wasn't implemented in child class
$this->registerName = $cache_register_name;
}
/**
* {@inheritDoc}
*/
public function createRegisterItem($item_ids) {
$item = new CacheRegisterItem($this->cache, $this, $item_ids);
$this->registerItems[] = $item;
return $item;
}
/**
* {@inheritDoc}
*/
public function getName() {
return $this->registerName;
}
/**
* {@inheritDoc}
*/
public function getItems() {
return $this->registerItems;
}
/**
* {@inheritDoc}
*/
public function deleteAll() {
foreach ($this->registerItems as $item) {
$item->deleteCache();
}
}
/**
* {@inheritDoc}
*/
public function invalidateAll() {
foreach ($this->registerItems as $item) {
$item->invalidateCache();
}
}
}
<?php
namespace Drupal\cache_register;
use Drupal\Core\Cache\CacheBackendInterface;
/**
* Class QuickCacheFactory.
*/
class CacheRegisterFactory implements CacheRegisterFactoryInterface {
/**
* Drupal\Core\Cache\CacheBackendInterface definition.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cache;
/**
* Constructs a new CacheRegisterFactory object.
*
* @param CacheBackendInterface $cache
* The CacheBackendInterface.
*/
public function __construct(CacheBackendInterface $cache) {
$this->cache = $cache;
}
/**
* {@inheritDoc}
*/
public function createRegister(string $implementor_id, $cache_register_id = NULL) {
$cache_register_name = $implementor_id;
if ($cache_register_id) {
$bucket_suffix = is_array($cache_register_id)
? implode('.', $cache_register_id)
: $cache_register_id;
$cache_register_name .= ".$bucket_suffix";
}
return new CacheRegister($this->cache, $cache_register_name);
}
}
<?php
namespace Drupal\cache_register;
interface CacheRegisterFactoryInterface {
/**
* Creates an EasyCache interaction object.
*
* @param string $implementor_id
* This should generally be the implementing module's name.
* It will be used as the beginning of all child items'
* cache entry IDs.
* @param string|array|null $cache_register_id
* An identifier for the register itself.
* It will be appended to the $implementor_id when
* determining child item's cache entry IDs.
* - If passed an array, items will be imploded.
*
* @return \Drupal\cache_register\CacheRegisterInterface
*/
public function createRegister(string $implementor_id, $cache_register_id = NULL);
}
<?php
namespace Drupal\cache_register;
interface CacheRegisterInterface {
/**
* Creates a new CacheRegisterItem in the register.
*
* @note This does note create/populate the associated
* cache entry. You must implement $this->set($data)
* after instantiating the CacheRegisterItem to
* populate its cache entry.
*
* @param array|string $item_ids
* The ID(s) to use for the item's cache identifier.
* This might be something like a UID if you are
* caching information about a specific user.
*
* @return \Drupal\cache_register\CacheRegisterItemInterface
*/
public function createRegisterItem($item_ids);
/**
* @return string
* The register's name.
*/
public function getName();
/**
* @return \Drupal\cache_register\CacheRegisterItemInterface[]
*/
public function getItems();
/**
* Invalidate (delete) all child item cache entries.
*
* @return mixed
*/
public function deleteAll();
/**
* Invalidate (delete) all child item cache entries.
*
* @return mixed
*/
public function invalidateAll();
}
<?php
namespace Drupal\cache_register;
use Drupal\Core\Cache\CacheBackendInterface;
class CacheRegisterItem implements CacheRegisterItemInterface {
/**
* The cache backend service.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cache;
/**
* The parent CacheRegister.
*
* @var \Drupal\cache_register\CacheRegisterInterface
*/
protected $parentCacheRegister;
/**
* The name of the item in the cache.
*
* @var string
*/
protected $cacheEntryId;
/**
* The constructor.
*
* @param \Drupal\Core\Cache\CacheBackendInterface $cache
* Cache backend.
* @param CacheRegisterInterface $parent_cache_register
* The parent CacheRegister object.
* @param array|string $item_ids
* The ID(s) to use for the item's cache identifier.
*/
public function __construct(CacheBackendInterface $cache, CacheRegisterInterface $parent_cache_register, string $item_ids) {
$this->cache = $cache;
$this->parentCacheRegister = $parent_cache_register;
$this->cacheEntryId = $this->constructCacheEntryId($item_ids);
}
/**
* {@inheritDoc}
*/
public function getCacheEntryId() {
return $this->cacheEntryId;
}
/**
* {@inheritDoc}
*/
public function isCached() {
$data_cached = $this->cache->get($this->cacheEntryId);
if ($data_cached) {
return TRUE;
}
return FALSE;
}
/**
* {@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;
}
/**
* {@inheritDoc}
*/
public function addCacheTags($tags) {
if (!$cache_item = $this->getCache()) {
return FALSE;
}
if (is_string($tags)) {
$tags = [$tags];
}
$new_tags = is_string($tags) ? [$tags] : $tags;
$existing_tags = $cache_item->tags;
$updated_tags = array_merge($existing_tags, $new_tags);
$this->setCache($cache_item->data, $cache_item->expire, $updated_tags);
return TRUE;
}
/**
* {@inheritDoc}
*/
public function getCache() {
return $this->cache->get($this->cacheEntryId);
}
/**
* {@inheritDoc}
*/
public function deleteCache() {
$this->cache->delete($this->cacheEntryId);
}
/**
* {@inheritDoc}
*/
public function invalidateCache() {
$this->cache->invalidate($this->cacheEntryId);
}
/**
* Constructs the item's cache ID.
*
* Uses the parent register name and the provided IDs.
*
* @param array|string $item_cache_ids
* One ID, or a set of IDs, used to identify the cache item.
*
* @return string
* Cache ID String
*/
private function constructCacheEntryId($item_cache_ids) {
$cache_id = is_array($item_cache_ids) ? implode('.', $item_cache_ids) : $item_cache_ids;
return "{$this->parentCacheRegister->getName()}:$cache_id";
}
}
<?php
namespace Drupal\cache_register;
use Drupal\Core\Cache\CacheBackendInterface;
interface CacheRegisterItemInterface {
/**
* Checks if the item has an entry in the cache.
*
* @return bool
* Whether or not the item with the given ids is cached.
*/
public function isCached();
/**
* Sets the item's cache entry.
*
* @param mixed $data
* The data to be saved to the given cache item.
*
* @param int $expire
* One of the following values:
* - CacheBackendInterface::CACHE_PERMANENT: Indicates that the item should
* not be removed unless it is deleted explicitly.
* - A Unix timestamp: Indicates that the item will be considered invalid
* after this time, i.e. it will not be returned by get() unless
* $allow_invalid has been set to TRUE. When the item has expired, it may
* be permanently deleted by the garbage collector at any time.
*
* @param array $tags
* An array of tags to be stored with the cache item. These should normally
* identify objects used to build the cache item, which should trigger
* cache invalidation when updated. For example if a cached item represents
* a node, both the node ID and the author's user ID might be passed in as
* tags. For example ['node:123', 'node:456', 'user:789'].
*
* @see CacheBackendInterface
*/
public function setCache($data, int $expire = CacheBackendInterface::CACHE_PERMANENT, array $tags = []);
/**
* Sets the given tags on the item'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 item's cache entry.
*
* @param array|string $tags
* One or multiple tags to append to the item.
*
* @return bool
* False if the item doesn't exist yet.
*/
public function addCacheTags($tags);
/**
* Gets the items cache entry.
*
* @return object|false
* The cache item or FALSE on failure.
*/
public function getCache();
/**
* Delete the item's cache entry.
*/
public function deleteCache();
/**
* Delete the item's cache entry.
*/
public function invalidateCache();
/**
* Get the ID of the item's cache entry.
*
* @return string
*/
public function getCacheEntryId();
}
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