consistentBackend = $consistent_backend; $this->fastBackend = $fast_backend; $this->bin = 'cache_' . $bin; $this->lastWriteTimestamp = NULL; } /** * {@inheritdoc} */ public function get($cid, $allow_invalid = FALSE) { $cids = [$cid]; $cache = $this->getMultiple($cids, $allow_invalid); return reset($cache); } /** * {@inheritdoc} */ public function getMultiple(&$cids, $allow_invalid = FALSE) { $cids_copy = $cids; $cache = []; // If we can determine the time at which the last write to the consistent // backend occurred (we might not be able to if it has been recently // flushed/restarted), then we can use that to validate items from the fast // backend, so try to get those first. Otherwise, we can't assume that // anything in the fast backend is valid, so don't even bother fetching // from there. $last_write_timestamp = $this->getLastWriteTimestamp(); if ($last_write_timestamp) { // Items in the fast backend might be invalid based on their timestamp, // but we can't check the timestamp prior to getting the item, which // includes unserializing it. However, unserializing an invalid item can // throw an exception. For example, a __wakeup() implementation that // receives object properties containing references to code or data that // no longer exists in the application's current state. // // Unserializing invalid data, whether it throws an exception or not, is // a waste of time, but we only incur it while a cache invalidation has // not yet finished propagating to all the fast backend instances. // // Most cache backend implementations should not wrap their internal // get() implementations with a try/catch, because they have no reason to // assume that their data is invalid, and doing so would mask // unserialization errors of valid data. We do so here, only because the // fast backend is non-authoritative, and after discarding its // exceptions, we proceed to check the consistent (authoritative) backend // and allow exceptions from that to bubble up. try { $items = $this->fastBackend->getMultiple($cids, $allow_invalid); } catch (\Exception $e) { $cids = $cids_copy; $items = []; } // Even if items were successfully fetched from the fast backend, they // are potentially invalid if older than the last time the bin was // written to in the consistent backend, so only keep ones that aren't. foreach ($items as $item) { if ($item->created < $last_write_timestamp) { $cids[array_search($item->cid, $cids_copy)] = $item->cid; } else { $cache[$item->cid] = $item; } } } // If there were any cache entries that were not available in the fast // backend, retrieve them from the consistent backend and store them in the // fast one. if ($cids) { foreach ($this->consistentBackend->getMultiple($cids, $allow_invalid) as $item) { $cache[$item->cid] = $item; $this->fastBackend->set($item->cid, $item->data, $item->expire, $item->tags); } } return $cache; } /** * {@inheritdoc} */ public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = []) { $this->consistentBackend->set($cid, $data, $expire, $tags); $this->markAsOutdated(); $this->fastBackend->set($cid, $data, $expire, $tags); } /** * {@inheritdoc} */ public function setMultiple(array $items) { $this->consistentBackend->setMultiple($items); $this->markAsOutdated(); $this->fastBackend->setMultiple($items); } /** * {@inheritdoc} */ public function delete($cid) { $this->consistentBackend->deleteMultiple([$cid]); $this->markAsOutdated(); } /** * {@inheritdoc} */ public function deleteMultiple(array $cids) { $this->consistentBackend->deleteMultiple($cids); $this->markAsOutdated(); } /** * {@inheritdoc} */ public function deleteAll() { $this->consistentBackend->deleteAll(); $this->markAsOutdated(); } /** * {@inheritdoc} */ public function invalidate($cid) { $this->invalidateMultiple([$cid]); } /** * {@inheritdoc} */ public function invalidateMultiple(array $cids) { $this->consistentBackend->invalidateMultiple($cids); $this->markAsOutdated(); } /** * {@inheritdoc} */ public function invalidateTags(array $tags) { if ($this->consistentBackend instanceof CacheTagsInvalidatorInterface) { $this->consistentBackend->invalidateTags($tags); } if ($this->fastBackend instanceof CacheTagsInvalidatorInterface) { $this->fastBackend->invalidateTags($tags); } } /** * {@inheritdoc} */ public function invalidateAll() { $this->consistentBackend->invalidateAll(); $this->markAsOutdated(); } /** * {@inheritdoc} */ public function garbageCollection() { $this->consistentBackend->garbageCollection(); $this->fastBackend->garbageCollection(); } /** * {@inheritdoc} */ public function removeBin() { $this->consistentBackend->removeBin(); $this->fastBackend->removeBin(); } /** * @todo Document in https://www.drupal.org/node/2311945. */ public function reset() { $this->lastWriteTimestamp = NULL; } /** * Gets the last write timestamp. */ protected function getLastWriteTimestamp() { if ($this->lastWriteTimestamp === NULL) { $cache = $this->consistentBackend->get(self::LAST_WRITE_TIMESTAMP_PREFIX . $this->bin); $this->lastWriteTimestamp = $cache ? $cache->data : 0; } return $this->lastWriteTimestamp; } /** * Marks the fast cache bin as outdated because of a write. */ protected function markAsOutdated() { // Clocks on a single server can drift. Multiple servers may have slightly // differing opinions about the current time. Given that, do not assume // 'now' on this server is always later than our stored timestamp. // Also add 1 millisecond, to ensure that caches written earlier in the same // millisecond are invalidated. It is possible that caches will be later in // the same millisecond and are then incorrectly invalidated, but that only // costs one additional roundtrip to the persistent cache. $now = round(microtime(TRUE) + .001, 3); if ($now > $this->getLastWriteTimestamp()) { $this->lastWriteTimestamp = $now; $this->consistentBackend->set(self::LAST_WRITE_TIMESTAMP_PREFIX . $this->bin, $this->lastWriteTimestamp); } } }