[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/ctools/includes/ -> object-cache.inc (source)

   1  <?php
   2  // $Id: object-cache.inc,v 1.8.2.3 2010/07/22 22:33:34 merlinofchaos Exp $
   3  
   4  /**
   5   * @file
   6   * The non-volatile object cache is used to store an object while it is
   7   * being edited, so that we don't have to save until we're completely
   8   * done. The cache should be 'cleaned' on a regular basis, meaning to
   9   * remove old objects from the cache, but otherwise the data in this
  10   * cache must remain stable, as it includes unsaved changes.
  11   */
  12  
  13  /**
  14   * Get an object from the non-volatile ctools cache.
  15   *
  16   * This function caches in memory as well, so that multiple calls to this
  17   * will not result in multiple database reads.
  18   *
  19   * @param $obj
  20   *   A 32 character or less string to define what kind of object is being
  21   *   stored; primarily this is used to prevent collisions.
  22   * @param $name
  23   *   The name of the object being stored.
  24   * @param $skip_cache
  25   *   Skip the memory cache, meaning this must be read from the db again.
  26   *
  27   * @return
  28   *   The data that was cached.
  29   */
  30  function ctools_object_cache_get($obj, $name, $skip_cache = FALSE) {
  31    $cache = &ctools_static(__FUNCTION__, array());
  32    $key = "$obj:$name";
  33    if ($skip_cache) {
  34      unset($cache[$key]);
  35    }
  36  
  37    if (!array_key_exists($key, $cache)) {
  38      $data = db_fetch_object(db_query("SELECT * FROM {ctools_object_cache} WHERE sid = '%s' AND obj = '%s' AND name = '%s'", session_id(), $obj, $name));
  39      if ($data) {
  40        $cache[$key] = unserialize(db_decode_blob($data->data));
  41      }
  42    }
  43    return isset($cache[$key]) ? $cache[$key] : NULL;
  44  }
  45  
  46  /**
  47   * Store an object in the non-volatile ctools cache.
  48   *
  49   * @param $obj
  50   *   A 32 character or less string to define what kind of object is being
  51   *   stored; primarily this is used to prevent collisions.
  52   * @param $name
  53   *   The name of the object being stored.
  54   * @param $cache
  55   *   The object to be cached. This will be serialized prior to writing.
  56   */
  57  function ctools_object_cache_set($obj, $name, $cache) {
  58    // Store the CTools session id in the user session to force a
  59    // session for anonymous users in Drupal 7 and Drupal 6 Pressflow.
  60    // see http://drupal.org/node/562374, http://drupal.org/node/861778
  61    if (empty($GLOBALS['user']->uid) && empty($_SESSION['ctools_session_id'])) {
  62      $_SESSION['ctools_hold_session'] = TRUE;
  63    }
  64  
  65    ctools_object_cache_clear($obj, $name);
  66    db_query("INSERT INTO {ctools_object_cache} (sid, obj, name, data, updated) VALUES ('%s', '%s', '%s', %b, %d)", session_id(), $obj, $name, serialize($cache), time());
  67  }
  68  
  69  /**
  70   * Remove an object from the non-volatile ctools cache
  71   *
  72   * @param $obj
  73   *   A 32 character or less string to define what kind of object is being
  74   *   stored; primarily this is used to prevent collisions.
  75   * @param $name
  76   *   The name of the object being removed.
  77   */
  78  function ctools_object_cache_clear($obj, $name) {
  79    db_query("DELETE FROM {ctools_object_cache} WHERE sid = '%s' AND obj = '%s' AND name = '%s'", session_id(), $obj, $name);
  80    // Ensure the static cache is emptied of this obj:name set.
  81    $cache = &ctools_static('ctools_object_cache_get', array());
  82    unset($cache["$obj:$name"]);
  83  }
  84  
  85  
  86  /**
  87   * Determine if another user has a given object cached.
  88   *
  89   * This is very useful for 'locking' objects so that only one user can
  90   * modify them.
  91   *
  92   * @param $obj
  93   *   A 32 character or less string to define what kind of object is being
  94   *   stored; primarily this is used to prevent collisions.
  95   * @param $name
  96   *   The name of the object being removed.
  97   *
  98   * @return
  99   *   An object containing the UID and updated date if found; NULL if not.
 100   */
 101  function ctools_object_cache_test($obj, $name) {
 102    return db_fetch_object(db_query("SELECT s.uid, c.updated FROM {ctools_object_cache} c INNER JOIN {sessions}  s ON c.sid = s.sid WHERE s.sid != '%s' AND c.obj = '%s' AND c.name = '%s' ORDER BY c.updated ASC", session_id(), $obj, $name));
 103  }
 104  
 105  /**
 106   * Get the cache status of a group of objects.
 107   *
 108   * This is useful for displaying lock status when listing a number of objects
 109   * an an administration UI.
 110   *
 111   * @param $obj
 112   *   A 32 character or less string to define what kind of object is being
 113   *   stored; primarily this is used to prevent collisions.
 114   * @param $names
 115   *   An array of names of objects
 116   *
 117   * @return
 118   *   An array of objects containing the UID and updated date for each name found.
 119   */
 120  function ctools_object_cache_test_objects($obj, $names) {
 121    $placeholders = db_placeholders($names, 'varchar');
 122    $args = array_merge(array($obj), $names);
 123    $result = db_query("SELECT c.name, s.uid, c.updated FROM {ctools_object_cache} c INNER JOIN {sessions}  s ON c.sid = s.sid WHERE c.obj = '%s' AND c.name IN ($placeholders) ORDER BY c.updated ASC", $args);
 124  
 125    $return = array();
 126    while ($test = db_fetch_object($result)) {
 127      $return[$test->name] = $test;
 128    }
 129  
 130    return $return;
 131  }
 132  
 133  /**
 134   * Remove an object from the non-volatile ctools cache for all session IDs.
 135   *
 136   * This is useful for clearing a lock.
 137   *
 138   * @param $obj
 139   *   A 32 character or less string to define what kind of object is being
 140   *   stored; primarily this is used to prevent collisions.
 141   * @param $name
 142   *   The name of the object being removed.
 143   */
 144  function ctools_object_cache_clear_all($obj, $name) {
 145    db_query("DELETE FROM {ctools_object_cache} WHERE obj = '%s' AND name = '%s'", $obj, $name);
 146    // Ensure the static cache is emptied of this obj:name set.
 147    $cache = &ctools_static('ctools_object_cache_get', array());
 148    unset($cache["$obj:$name"]);
 149  }
 150  
 151  /**
 152   * Remove all objects in the object cache that are older than the
 153   * specified age.
 154   *
 155   * @param $age
 156   *   The minimum age of objects to remove, in seconds. For example, 86400 is
 157   *   one day. Defaults to 7 days.
 158   */
 159  function ctools_object_cache_clean($age = NULL) {
 160    if (empty($age)) {
 161      $age = 86400 * 7; // 7 days
 162    }
 163    db_query("DELETE FROM {ctools_object_cache} WHERE updated < %d", time() - $age);
 164  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7