class PersistableCache in RESTful 7.2
Hierarchy
- class \Drupal\restful\Util\PersistableCache implements PersistableCacheInterface
Expanded class hierarchy of PersistableCache
1 file declares its use of PersistableCache
- RestfulManager.php in src/
RestfulManager.php - Contains \Drupal\restful\RestfulManager.
File
- src/
Util/ PersistableCache.php, line 10 - Contains \Drupal\restful\Util\PersistableCache.
Namespace
Drupal\restful\UtilView source
class PersistableCache implements PersistableCacheInterface {
/**
* The data array.
*
* @var array
*/
protected $data = array();
/**
* Tracks the loaded keys.
*
* @var string[]
*/
protected $loaded = array();
/**
* The cache bin where to store the caches.
*
* @var string
*/
protected $cacheBin = 'cache';
/**
* PersistableCache constructor.
*
* @param string $cache_bin
* The cache bin where to store the persisted cache.
*/
public function __construct($cache_bin = NULL) {
$this->cacheBin = $cache_bin ?: 'cache';
}
/**
* {@inheritdoc}
*/
public function contains($key) {
return isset($this->data[$key]);
}
/**
* {@inheritdoc}
*/
public function &get($key) {
if (!$this
->contains($key)) {
// Load from the real cache if it's not loaded yet.
$this
->load($key);
}
if (!$this
->contains($key)) {
$this->data[$key] = NULL;
}
return $this->data[$key];
}
/**
* {@inheritdoc}
*/
public function set($key, $value) {
$this->data[$key] = $value;
}
/**
* {@inheritdoc}
*/
public function delete($key) {
unset($this->data[$key]);
}
/**
* {@inheritdoc}
*/
public function persist() {
foreach ($this->data as $key => $value) {
cache_set($key, $value, $this->cacheBin);
}
}
/**
* Persist the data in the cache backend during shutdown.
*/
public function __destruct() {
$this
->persist();
}
/**
* Checks if a key was already loaded before.
*
* @param string $key
* The key to check.
*
* @return bool
* TRUE if it was loaded before. FALSE otherwise.
*/
protected function isLoaded($key) {
return isset($this->loaded[$key]);
}
/**
* Tries to load an item from the real cache.
*
* @param string $key
* The key of the item.
*/
protected function load($key) {
if ($this
->isLoaded($key)) {
return;
}
// Mark the key as loaded.
$this->loaded[$key] = TRUE;
if ($cache = cache_get($key, $this->cacheBin)) {
$this->data[$key] = $cache->data;
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PersistableCache:: |
protected | property | The cache bin where to store the caches. | |
PersistableCache:: |
protected | property | The data array. | |
PersistableCache:: |
protected | property | Tracks the loaded keys. | |
PersistableCache:: |
public | function |
Checks if the cache contains the key. Overrides PersistableCacheInterface:: |
|
PersistableCache:: |
public | function |
Delete a cached item. Overrides PersistableCacheInterface:: |
|
PersistableCache:: |
public | function |
Gets the memory reference of the cached item. Overrides PersistableCacheInterface:: |
|
PersistableCache:: |
protected | function | Checks if a key was already loaded before. | |
PersistableCache:: |
protected | function | Tries to load an item from the real cache. | |
PersistableCache:: |
public | function |
Persist the cache to the RESTful cache. Overrides PersistableCacheInterface:: |
|
PersistableCache:: |
public | function |
Gets the memory reference of the cached item. Overrides PersistableCacheInterface:: |
|
PersistableCache:: |
public | function | PersistableCache constructor. | |
PersistableCache:: |
public | function | Persist the data in the cache backend during shutdown. |