You are here

class CacheApcu in Drupal driver for SQL Server and SQL Azure 8.2

Apcu implementation for the in-memory fast cache. Use this for very frequently used cache items.

Hierarchy

Expanded class hierarchy of CacheApcu

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Component/CacheApcu.php, line 9

Namespace

Drupal\Driver\Database\sqlsrv\Component
View source
class CacheApcu implements CacheInterface {
  private $prefix = null;

  /**
   * This cache stores everything in-memory during the
   * lifetime of this request.
   *
   * @var array
   */
  private $data = [];

  /**
   * Serializer to use.
   *
   * @var SerializerInterface
   */
  private $serializer = null;
  public function __construct($prefix) {
    $this->prefix = $prefix;

    // Try to use a serializer...
    if (function_exists('igbinary_serialize')) {
      $this->serializer = new SerializerIgbinary();
    }
    else {
      $this->serializer = new SerializerPhp();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function Set($cid, $data) {
    $cache = new \stdClass();
    $cache->data = $data;
    $cache->serialized = false;
    $cache->timestamp = time();
    $this->data[$cid] = clone $cache;
    apcu_store($this->prefix . ':' . $cid, $cache);
  }

  /**
   * {@inheritdoc}
   */
  public function Get($cid) {
    if (isset($this->data[$cid])) {
      return $this->data[$cid];
    }
    $success = false;
    $result = apcu_fetch($this->prefix . ':' . $cid, $success);
    if (!$success) {
      return false;
    }
    if (isset($result->serialized) && $result->serialized) {
      $result->data = $this->serializer
        ->unserialize($result->data);
    }
    $this->data[$cid] = $result;
    return $result;
  }

  /**
   * {@inheritdoc}
   */
  public function Clear($cid) {
    apcu_delete($this->prefix . ':' . $cid);
    unset($this->data[$cid]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheApcu::$data private property This cache stores everything in-memory during the lifetime of this request.
CacheApcu::$prefix private property
CacheApcu::$serializer private property Serializer to use.
CacheApcu::Clear public function Clear a cache item. Overrides CacheInterface::Clear
CacheApcu::Get public function Get a cache item. Overrides CacheInterface::Get
CacheApcu::Set public function Set a cache item. Overrides CacheInterface::Set
CacheApcu::__construct public function