You are here

class QPCache in QueryPath 7.3

Same name and namespace in other branches
  1. 6 qpcache/qpcache.module \QPCache
  2. 7.2 qpcache/qpcache.module \QPCache

This is a special-purpose XML cache.

It performs a different role than the built-in Drupal cache. It caches XML documents for any given period. It is not cleared with the Drupal cache. It uses a hashkey for optimal search speed across multiple database types.

The qpcache_* functions in this module are not mere wrappers around the methods here. They add substantial logic on top of the bare caching layer. Part of the logic is the key encoding logic. The QPCache class assumes that keys are strings. The qpcache_* functions provide facilities for use objects and arrays as keys, too.

Hierarchy

Expanded class hierarchy of QPCache

File

qpcache/qpcache.module, line 199
The main file for qpcache.

View source
class QPCache {

  /**
   * Return true if the cache has this key.
   */
  public static function has($key) {
    list($crc, $hash) = self::genMultiKey($key);
    $now = time();
    $sql = 'SELECT 1 FROM {qpcache_xmlcache} WHERE crckey=:crc AND hashkey=\':hash\' AND (expire = 0 OR expire > :now)';
    return (bool) db_query_range($sql, 0, 1, array(
      ':crc' => $crc,
      ':hash' => $hash,
      ':now' => $now,
    ));
  }

  /**
   * Return the value of the given key, if it exists in the database.
   *
   * If no value is found, this will return NULL.
   */
  public static function get($key) {
    list($crc, $hash) = self::genMultiKey($key);
    $now = time();

    // if (is_array($key)) {
    //       drupal_set_message('Retrieval key is array.', 'status');
    //     }
    $sql = 'SELECT clearkey, expire, body FROM {qpcache_xmlcache} WHERE crckey=\':crc\' AND hashkey=\':hash\' AND (expire = 0 OR expire > \':now\')';
    $result = db_query($sql, array(
      ':crc' => $crc,
      ':hash' => $hash,
      ':now' => $now,
    ));
    if (empty($result)) {
      return;
    }
    foreach ($result as $object) {
      $object->xml = $object->body;
      return $object;
    }
  }

  /**
   * Put a value in the cache.
   *
   * This will overwrite any existing entry with the same key.
   *
   * @param $key
   *   A string value.
   * @param $body
   *   The text value to store.
   * @param $expire
   *   An expiration date in UNIX timestamp format.
   */
  public static function set($key, $body, $expire = 0) {
    list($crckey, $hashkey) = self::genMultiKey($key);
    db_delete('qpcache_xmlcache')
      ->condition('hashkey', $hashkey);
    db_insert('qpcache_xmlcache')
      ->fields(array(
      'crckey' => $crckey,
      'hashkey' => $hashkey,
      'clearkey' => $key,
      'body' => $body,
      'expire' => $expire,
    ));
  }

  /**
   * Remove a single entry from the cache.
   *
   * This will remove the item whether it has expired or note.
   *
   * @param $key
   *   The key of the item to be removed.
   */
  public static function remove($key) {
    list($crckey, $hashkey) = self::genMultiKey($key);
    db_delete('qpcache_xmlcache')
      ->condition('hashkey', $hashkey);
  }

  /**
   * Remove expired keys.
   */
  public static function prune() {
    db_delete('qpcache_xmlcache')
      ->condition('expire', array(
      1,
      time(),
    ), 'BETWEEN');
  }

  /**
   * Empty the cache.
   */
  public static function clear() {
    db_query('TRUNCATE TABLE {qpcache_xmlcache}');
  }

  /**
   * Given the original key, generate a multi-key.
   *
   * For performance reasons, we use a composite key for retrieving entries.
   * This key uses a CRC-32 checksum against the original key plus an MD5 of
   * the original key. A CRC can be looked up in the database about 30 times faster
   * than an MD5. However, it has a much broader collision space.
   * So if indexing is done correctly, we can use the CRC to very quickly narrow,
   * and then use the MD5 to select the appropriate result from a very small set.
   *.
   * @return
   *   List where position 0 is CRC and 1 is MD5
   */
  public static function genMultiKey($key) {
    return array(
      crc32($key),
      md5($key),
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
QPCache::clear public static function Empty the cache.
QPCache::genMultiKey public static function Given the original key, generate a multi-key.
QPCache::get public static function Return the value of the given key, if it exists in the database.
QPCache::has public static function Return true if the cache has this key.
QPCache::prune public static function Remove expired keys.
QPCache::remove public static function Remove a single entry from the cache.
QPCache::set public static function Put a value in the cache.