$cache
$cache
This is the default implementation for in-memory cell collection.
Alternatives implementation should leverage off-memory, non-volatile storage to reduce overall memory usage.
get(mixed $key, mixed $default = null) : mixed
Fetches a value from the cache.
mixed | $key | The unique key of this item in the cache. |
mixed | $default | Default value to return if the key does not exist. |
The value of the item from the cache, or $default in case of cache miss.
getMultiple(mixed $keys, mixed $default = null) : iterable
Obtains multiple cache items by their unique keys.
mixed | $keys | A list of keys that can obtained in a single operation. |
mixed | $default | Default value to return for keys that do not exist. |
A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value.
has(mixed $key) : bool
Determines whether an item is present in the cache.
NOTE: It is recommended that has() is only to be used for cache warming type purposes and not to be used within your live applications operations for get/set, as this method is subject to a race condition where your has() will return true and immediately after, another script can remove it making the state of your app out of date.
mixed | $key | The cache item key. |
set(mixed $key, mixed $value, mixed $ttl = null) : bool
Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
mixed | $key | The key of the item to store. |
mixed | $value | The value of the item to store, must be serializable. |
mixed | $ttl | Optional. The TTL value of this item. If no value is sent and the driver supports TTL then the library may set a default value for it or let the driver take care of that. |
True on success and false on failure.
setMultiple(mixed $values, mixed $ttl = null) : bool
Persists a set of key => value pairs in the cache, with an optional TTL.
mixed | $values | A list of key => value pairs for a multiple-set operation. |
mixed | $ttl | Optional. The TTL value of this item. If no value is sent and the driver supports TTL then the library may set a default value for it or let the driver take care of that. |
True on success and false on failure.
<?php
namespace PhpOffice\PhpSpreadsheet\Collection;
use Psr\SimpleCache\CacheInterface;
/**
* This is the default implementation for in-memory cell collection.
*
* Alternatives implementation should leverage off-memory, non-volatile storage
* to reduce overall memory usage.
*/
class Memory implements CacheInterface
{
private $cache = [];
public function clear()
{
$this->cache = [];
return true;
}
public function delete($key)
{
unset($this->cache[$key]);
return true;
}
public function deleteMultiple($keys)
{
foreach ($keys as $key) {
$this->delete($key);
}
return true;
}
public function get($key, $default = null)
{
if ($this->has($key)) {
return $this->cache[$key];
}
return $default;
}
public function getMultiple($keys, $default = null)
{
$results = [];
foreach ($keys as $key) {
$results[$key] = $this->get($key, $default);
}
return $results;
}
public function has($key)
{
return array_key_exists($key, $this->cache);
}
public function set($key, $value, $ttl = null)
{
$this->cache[$key] = $value;
return true;
}
public function setMultiple($values, $ttl = null)
{
foreach ($values as $key => $value) {
$this->set($key, $value);
}
return true;
}
}