Add ShmBackend for when APCu is not available

Created on 19 October 2023, over 1 year ago

Problem/Motivation

APCu might not be available.

/dev/shm might be.

Steps to reproduce

- Don't have APCu, but /dev/shm is writable

Proposed resolution

I wrote some code, which I am sharing now (https://gist.github.com/LionsAd/31043834e54ac53d8af168d32a2a238f):

<?php

namespace Drupal\Core\Cache;

use Drupal\Component\Assertion\Inspector;

/**
 * Stores cache items in /dev/shm.
 */
class ShmBackend implements CacheBackendInterface {

  /**
   * The name of the cache bin to use.
   *
   * @var string
   */
  protected $bin;

  /**
   * Prefix for all keys in the storage that belong to this site.
   *
   * @var string
   */
  protected $sitePrefix;

  /**
   * Prefix for all keys in this cache bin.
   *
   * Includes the site-specific prefix in $sitePrefix.
   *
   * @var string
   */
  protected $binPrefix;

  /**
   * The cache tags checksum provider.
   *
   * @var \Drupal\Core\Cache\CacheTagsChecksumInterface
   */
  protected $checksumProvider;

  /**
   * Constructs a new ApcuBackend instance.
   *
   * @param string $bin
   *   The name of the cache bin.
   * @param string $site_prefix
   *   The prefix to use for all keys in the storage that belong to this site.
   * @param \Drupal\Core\Cache\CacheTagsChecksumInterface $checksum_provider
   *   The cache tags checksum provider.
   */
  public function __construct($bin, $site_prefix, CacheTagsChecksumInterface $checksum_provider) {
    $this->bin = $bin;
    $this->sitePrefix = $site_prefix;
    $this->checksumProvider = $checksum_provider;
    $this->binPrefix = '/dev/shm/' . $this->sitePrefix . '/' . $this->bin . '/';

    @mkdir('/dev/shm/' . $this->sitePrefix);
    @mkdir($this->binPrefix);
  }

  /**
   * Prepends the APCu user variable prefix for this bin to a cache item ID.
   *
   * @param string $cid
   *   The cache item ID to prefix.
   *
   * @return string
   *   The APCu key for the cache item ID.
   */
  public function getApcuKey($cid) {
    return $this->binPrefix . str_replace('%3A', ':', rawurlencode($cid));
  }

  /**
   * {@inheritdoc}
   */
  public function get($cid, $allow_invalid = FALSE) {
    $cids = [$cid];
    $data = $this->getMultiple($cids, $allow_invalid);
    return !empty($data[$cid]) ? $data[$cid] : FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getMultiple(&$cids, $allow_invalid = FALSE) {
    $start = microtime(TRUE);
    $cids_copy = $cids;
    $memory = memory_get_usage();

    // Translate the requested cache item IDs to APCu keys.
    $map = [];
    foreach ($cids as $cid) {
      $map[$this->getApcuKey($cid)] = $cid;
    }

    $result = $this->fetch(array_keys($map));
    $cache = [];
    if ($result) {
      foreach ($result as $key => $item) {
        $item = $this->prepareItem($item, $allow_invalid);
        if ($item) {
          $cache[$map[$key]] = $item;
        }
      }
    }
    unset($result);

    $cids = array_diff($cids, array_keys($cache));
    return $cache;
  }

  /**
   * Prepares a cached item.
   *
   * Checks that the item is either permanent or did not expire.
   *
   * @param \stdClass $cache
   *   An item loaded from cache_get() or cache_get_multiple().
   * @param bool $allow_invalid
   *   If TRUE, a cache item may be returned even if it is expired or has been
   *   invalidated. See ::get().
   *
   * @return mixed
   *   The cache item or FALSE if the item expired.
   */
  protected function prepareItem($cache, $allow_invalid) {
    if (!isset($cache->data)) {
      return FALSE;
    }

    $cache->tags = $cache->tags ? explode(' ', $cache->tags) : [];

    // Check expire time.
    $cache->valid = $cache->expire == Cache::PERMANENT || $cache->expire >= REQUEST_TIME;

    // Check if invalidateTags() has been called with any of the entry's tags.
    if (!$this->checksumProvider->isValid($cache->checksum, $cache->tags)) {
      $cache->valid = FALSE;
    }

    if (!$allow_invalid && !$cache->valid) {
      return FALSE;
    }

    return $cache;
  }

  /**
   * {@inheritdoc}
   */
  public function set($cid, $data, $expire = CacheBackendInterface::CACHE_PERMANENT, array $tags = []) {
    assert(Inspector::assertAllStrings($tags), 'Cache tags must be strings.');
    $tags = array_unique($tags);
    $cache = new \stdClass();
    $cache->cid = $cid;
    $cache->expire = $expire;
    $cache->tags = implode(' ', $tags);
    $cache->checksum = $this->checksumProvider->getCurrentChecksum($tags);
    // APCu serializes/unserializes any structure itself.
    $cache->serialized = 0;
    $cache->data = $data;

    $filename = $this->getApcuKey($cid);
    $mtime_filename = $filename . '_mtime';

    $old_data = file_get_contents($filename);
    $data = serialize($cache);

    if ($data !== $old_data) {
      file_put_contents($filename, serialize($cache));
    }

    file_put_contents($mtime_filename, round(microtime(TRUE), 3));
  }

  /**
   * {@inheritdoc}
   */
  public function setMultiple(array $items = []) {
    foreach ($items as $cid => $item) {
      $this->set($cid, $item['data'], isset($item['expire']) ? $item['expire'] : CacheBackendInterface::CACHE_PERMANENT, isset($item['tags']) ? $item['tags'] : []);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function delete($cid) {
    unlink($this->getApcuKey($cid));
  }

  /**
   * {@inheritdoc}
   */
  public function deleteMultiple(array $cids) {
    foreach ($cids as $cid) {
      $this->delete($cid);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function deleteAll() {
    throw new \Exception('Unsupported');
    // @todo Implement?
  }

  /**
   * {@inheritdoc}
   */
  public function garbageCollection() {
    // APCu performs garbage collection automatically.
    // @todo Add by removing files from directory.
  }

  /**
   * {@inheritdoc}
   */
  public function removeBin() {
    throw new \Exception('Unsupported');
  }

  /**
   * {@inheritdoc}
   */
  public function invalidate($cid) {
    $this->invalidateMultiple([$cid]);
  }

  /**
   * {@inheritdoc}
   */
  public function invalidateMultiple(array $cids) {
    foreach ($this->getMultiple($cids) as $cache) {
      $this->set($cache->cid, $cache, REQUEST_TIME - 1);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function invalidateAll() {
    // Clear whole bin.
    throw new \Exception('Unsupported');
  }

  protected function fetch($cids) {
    $data = [];

    foreach ($cids as $cid) {
      $mtime_file = $cid . '_mtime';
      if (file_exists($cid) && file_exists($mtime_file)) {
        $data[$cid] = unserialize(file_get_contents($cid));
        $data[$cid]->created = file_get_contents($mtime_file);
      }
    }

    return $data;
  }
}

Remaining tasks

- Make into a patch
- Discuss if we want to add this to core
- Add test coverage (?) - if GITLAB CI supports this
- Fix todos
- Remove all APCu related things

📌 Task
Status

Active

Version

11.0 🔥

Component
Cache 

Last updated about 12 hours ago

Created by

🇩🇪Germany Fabianx

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Fabianx
  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    Catching up on long-forgotten tabs … from last year’s European DrupalCon just before this year’s starts 😄

    I think Fabian’s patch above first must be converted to an MR, hence tagging “Needs reroll”.

Production build 0.71.5 2024