class MemoryCounterBackend in Drupal 9
Same name and namespace in other branches
- 8 core/lib/Drupal/Core/Cache/MemoryCounterBackend.php \Drupal\Core\Cache\MemoryCounterBackend
Defines a memory cache implementation that counts set and get calls.
This can be used to mock a cache backend where one needs to know how many times a cache entry was set or requested.
@todo On the longrun this backend should be replaced by phpunit mock objects.
Hierarchy
- class \Drupal\Core\Cache\MemoryBackend implements CacheBackendInterface, CacheTagsInvalidatorInterface
- class \Drupal\Core\Cache\MemoryCounterBackend
Expanded class hierarchy of MemoryCounterBackend
1 file declares its use of MemoryCounterBackend
- AliasTest.php in core/
modules/ path_alias/ tests/ src/ Kernel/ AliasTest.php
File
- core/
lib/ Drupal/ Core/ Cache/ MemoryCounterBackend.php, line 13
Namespace
Drupal\Core\CacheView source
class MemoryCounterBackend extends MemoryBackend {
/**
* Stores a list of cache cid calls keyed by function name.
*
* @var array
*/
protected $counter = [];
/**
* {@inheritdoc}
*/
public function get($cid, $allow_invalid = FALSE) {
$this
->increaseCounter(__FUNCTION__, $cid);
return parent::get($cid, $allow_invalid);
}
/**
* {@inheritdoc}
*/
public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = []) {
$this
->increaseCounter(__FUNCTION__, $cid);
parent::set($cid, $data, $expire, $tags);
}
/**
* {@inheritdoc}
*/
public function delete($cid) {
$this
->increaseCounter(__FUNCTION__, $cid);
parent::delete($cid);
}
/**
* Increase the counter for a function with a certain cid.
*
* @param string $function
* The called function.
* @param string $cid
* The cache ID of the cache entry to increase the counter.
*/
protected function increaseCounter($function, $cid) {
if (!isset($this->counter[$function][$cid])) {
$this->counter[$function][$cid] = 1;
}
else {
$this->counter[$function][$cid]++;
}
}
/**
* Returns the call counter for the get, set and delete methods.
*
* @param string $method
* (optional) The name of the method to return the call counter for.
* @param string $cid
* (optional) The name of the cache id to return the call counter for.
*
* @return int|array
* An integer if both method and cid is given, an array otherwise.
*/
public function getCounter($method = NULL, $cid = NULL) {
if ($method && $cid) {
return isset($this->counter[$method][$cid]) ? $this->counter[$method][$cid] : 0;
}
elseif ($method) {
return isset($this->counter[$method]) ? $this->counter[$method] : [];
}
else {
return $this->counter;
}
}
/**
* Resets the call counter.
*/
public function resetCounter() {
$this->counter = [];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheBackendInterface:: |
constant | Indicates that the item should never be removed unless explicitly deleted. | ||
MemoryBackend:: |
protected | property | Array to store cache objects. | |
MemoryBackend:: |
public | function |
Deletes all cache items in a bin. Overrides CacheBackendInterface:: |
|
MemoryBackend:: |
public | function |
Deletes multiple items from the cache. Overrides CacheBackendInterface:: |
|
MemoryBackend:: |
public | function |
Performs garbage collection on a cache bin. Overrides CacheBackendInterface:: |
|
MemoryBackend:: |
public | function |
Returns data from the persistent cache when given an array of cache IDs. Overrides CacheBackendInterface:: |
|
MemoryBackend:: |
protected | function | Wrapper method for REQUEST_TIME constant. | |
MemoryBackend:: |
public | function |
Marks a cache item as invalid. Overrides CacheBackendInterface:: |
|
MemoryBackend:: |
public | function |
Marks all cache items as invalid. Overrides CacheBackendInterface:: |
|
MemoryBackend:: |
public | function |
Marks cache items as invalid. Overrides CacheBackendInterface:: |
|
MemoryBackend:: |
public | function |
Marks cache items with any of the specified tags as invalid. Overrides CacheTagsInvalidatorInterface:: |
|
MemoryBackend:: |
protected | function | Prepares a cached item. | 1 |
MemoryBackend:: |
public | function |
Remove a cache bin. Overrides CacheBackendInterface:: |
|
MemoryBackend:: |
public | function | Reset statically cached variables. | |
MemoryBackend:: |
public | function |
Store multiple items in the persistent cache. Overrides CacheBackendInterface:: |
|
MemoryBackend:: |
public | function | Prevents data stored in memory backends from being serialized. | |
MemoryCounterBackend:: |
protected | property | Stores a list of cache cid calls keyed by function name. | |
MemoryCounterBackend:: |
public | function |
Deletes an item from the cache. Overrides MemoryBackend:: |
|
MemoryCounterBackend:: |
public | function |
Returns data from the persistent cache. Overrides MemoryBackend:: |
|
MemoryCounterBackend:: |
public | function | Returns the call counter for the get, set and delete methods. | |
MemoryCounterBackend:: |
protected | function | Increase the counter for a function with a certain cid. | |
MemoryCounterBackend:: |
public | function | Resets the call counter. | |
MemoryCounterBackend:: |
public | function |
Stores data in the persistent cache. Overrides MemoryBackend:: |