You are here

class AuthcacheP13nDatabaseKeyValueStore in Authenticated User Page Caching (Authcache) 7.2

Database based implementation of key-value store.

Hierarchy

Expanded class hierarchy of AuthcacheP13nDatabaseKeyValueStore

File

modules/authcache_p13n/includes/AuthcacheP13nDatabaseKeyValueStore.inc, line 10
Defines database based implementation of a key-value store.

View source
class AuthcacheP13nDatabaseKeyValueStore implements AuthcacheP13nKeyValueStoreInterface {
  protected static $tableName = 'authcache_p13n_key_value';
  protected $collectionName;

  /**
   * Construct new key-value store operating on the given table-name.
   */
  public function __construct($collection_name) {
    $this->collectionName = $collection_name;
  }

  /**
   * {@inheritdoc}
   */
  public function set($key, $value) {
    db_merge(static::$tableName)
      ->key(array(
      'name' => $key,
      'collection' => $this->collectionName,
    ))
      ->fields(array(
      'value' => serialize($value),
    ))
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function get($keys = NULL) {
    if ($keys === array()) {
      return array();
    }
    $query = db_select(static::$tableName, 'kv')
      ->fields('kv', array(
      'name',
      'value',
    ))
      ->condition('collection', $this->collectionName);
    if ($keys !== NULL) {
      $query
        ->condition('name', $keys);
    }
    return array_map('unserialize', $query
      ->execute()
      ->fetchAllKeyed());
  }

  /**
   * {@inheritdoc}
   */
  public function getOne($key) {
    $result = $this
      ->get(array(
      $key,
    ));
    if (count($result) > 0) {
      return reset($result);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getKeys($keys = NULL) {
    if ($keys === array()) {
      return array();
    }
    $query = db_select(static::$tableName, 'kv')
      ->fields('kv', array(
      'name',
    ))
      ->condition('collection', $this->collectionName);
    if ($keys !== NULL) {
      $query
        ->condition('name', $keys);
    }
    return $query
      ->execute()
      ->fetchCol();
  }

  /**
   * {@inheritdoc}
   */
  public function delete($keys = NULL) {
    if ($keys === array()) {
      return 0;
    }
    $query = db_delete(static::$tableName)
      ->condition('collection', $this->collectionName);
    if ($keys !== NULL) {
      $query
        ->condition('name', $keys);
    }
    return $query
      ->execute();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AuthcacheP13nDatabaseKeyValueStore::$collectionName protected property
AuthcacheP13nDatabaseKeyValueStore::$tableName protected static property
AuthcacheP13nDatabaseKeyValueStore::delete public function Remove entries with the given key. If keys is omitted, delete all entries. Overrides AuthcacheP13nKeyValueStoreInterface::delete
AuthcacheP13nDatabaseKeyValueStore::get public function Return an associative array of entries for the given keys. Overrides AuthcacheP13nKeyValueStoreInterface::get
AuthcacheP13nDatabaseKeyValueStore::getKeys public function Return a list of existing keys. Overrides AuthcacheP13nKeyValueStoreInterface::getKeys
AuthcacheP13nDatabaseKeyValueStore::getOne public function Return the value from the collection for the given key. Overrides AuthcacheP13nKeyValueStoreInterface::getOne
AuthcacheP13nDatabaseKeyValueStore::set public function Set a value in the collection with the given key. Overrides AuthcacheP13nKeyValueStoreInterface::set
AuthcacheP13nDatabaseKeyValueStore::__construct public function Construct new key-value store operating on the given table-name.