public function MemcacheBackend::set in Memcache API and Integration 8.2
Stores data in the persistent cache.
Core cache implementations set the created time on cache item with microtime(TRUE) rather than REQUEST_TIME_FLOAT, because the created time of cache items should match when they are created, not when the request started. Apart from being more accurate, this increases the chance an item will legitimately be considered valid.
Parameters
string $cid: The cache ID of the data to store.
mixed $data: The data to store in the cache. Some storage engines only allow objects up to a maximum of 1MB in size to be stored by default. When caching large arrays or similar, take care to ensure $data does not exceed this size.
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.
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'].
Overrides CacheBackendInterface::set
See also
\Drupal\Core\Cache\CacheBackendInterface::get()
\Drupal\Core\Cache\CacheBackendInterface::getMultiple()
1 call to MemcacheBackend::set()
- MemcacheBackend::setMultiple in src/
MemcacheBackend.php - Store multiple items in the persistent cache.
File
- src/
MemcacheBackend.php, line 198
Class
- MemcacheBackend
- Defines a Memcache cache backend.
Namespace
Drupal\memcacheCode
public function set($cid, $data, $expire = CacheBackendInterface::CACHE_PERMANENT, array $tags = []) {
assert(Inspector::assertAllStrings($tags));
$tags[] = "memcache:{$this->bin}";
$tags = array_unique($tags);
// Sort the cache tags so that they are stored consistently.
sort($tags);
// Create new cache object.
$cache = new \stdClass();
$cache->cid = $cid;
$cache->data = $data;
$cache->created = round(microtime(TRUE), 3);
$cache->expire = $expire;
$cache->tags = $tags;
$cache->checksum = $this->checksumProvider
->getCurrentChecksum($tags);
// Cache all items permanently. We handle expiration in our own logic.
if ($this->memcache
->set($cid, $cache)) {
return TRUE;
}
// Assume that the item is too large. We need to split it into multiple
// chunks with a parent entry referencing all the chunks.
$childKeys = [];
foreach ($this
->splitItem($cache) as $part) {
// If a single chunk fails to be set, stop trying - we can't reconstitute
// a value with a missing chunk.
if (!$this->memcache
->set($part->cid, $part)) {
return FALSE;
}
$childKeys[] = $part->cid;
}
// Create and write the parent entry referencing all chunks.
$cache->data = new MultipartItem($childKeys);
return $this->memcache
->set($cid, $cache);
}