[ Index ]
 

Code source de Drupal 5.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/includes/ -> cache.inc (source)

   1  <?php
   2  // $Id: cache.inc,v 1.5.2.4 2007/06/27 03:35:48 drumm Exp $
   3  
   4  /**
   5   * Return data from the persistent cache.
   6   *
   7   * @param $key
   8   *   The cache ID of the data to retrieve.
   9   * @param $table
  10   *   The table $table to store the data in. Valid core values are 'cache_filter',
  11   *   'cache_menu', 'cache_page', or 'cache' for the default cache.
  12   */
  13  function cache_get($key, $table = 'cache') {
  14    global $user;
  15  
  16    // Garbage collection necessary when enforcing a minimum cache lifetime
  17    $cache_flush = variable_get('cache_flush', 0);
  18    if ($cache_flush && ($cache_flush + variable_get('cache_lifetime', 0) <= time())) {
  19      // Time to flush old cache data
  20      db_query("DELETE FROM {". $table ."} WHERE expire != %d AND expire <= %d", CACHE_PERMANENT, $cache_flush);
  21      variable_set('cache_flush', 0);
  22    }
  23  
  24    $cache = db_fetch_object(db_query("SELECT data, created, headers, expire FROM {". $table ."} WHERE cid = '%s'", $key));
  25    if (isset($cache->data)) {
  26      // If the data is permanent or we're not enforcing a minimum cache lifetime
  27      // always return the cached data.
  28      if ($cache->expire == CACHE_PERMANENT || !variable_get('cache_lifetime', 0)) {
  29        $cache->data = db_decode_blob($cache->data);
  30      }
  31      // If enforcing a minimum cache lifetime, validate that the data is
  32      // currently valid for this user before we return it by making sure the
  33      // cache entry was created before the timestamp in the current session's
  34      // cache timer. The cache variable is loaded into the $user object by
  35      // sess_read() in session.inc.
  36      else {
  37        if ($user->cache > $cache->created) {
  38          // This cache data is too old and thus not valid for us, ignore it.
  39          return 0;
  40        }
  41        else {
  42          $cache->data = db_decode_blob($cache->data);
  43        }
  44      }
  45      return $cache;
  46    }
  47    return 0;
  48  }
  49  
  50  /**
  51   * Store data in the persistent cache.
  52   *
  53   * The persistent cache is split up into four database
  54   * tables. Contributed modules can add additional tables.
  55   *
  56   * 'cache_page': This table stores generated pages for anonymous
  57   * users. This is the only table affected by the page cache setting on
  58   * the administrator panel.
  59   *
  60   * 'cache_menu': Stores the cachable part of the users' menus.
  61   *
  62   * 'cache_filter': Stores filtered pieces of content. This table is
  63   * periodically cleared of stale entries by cron.
  64   *
  65   * 'cache': Generic cache storage table.
  66   *
  67   * The reasons for having several tables are as follows:
  68   *
  69   * - smaller tables allow for faster selects and inserts
  70   * - we try to put fast changing cache items and rather static
  71   *   ones into different tables. The effect is that only the fast
  72   *   changing tables will need a lot of writes to disk. The more
  73   *   static tables will also be better cachable with MySQL's query cache
  74   *
  75   * @param $cid
  76   *   The cache ID of the data to store.
  77   * @param $table
  78   *   The table $table to store the data in. Valid core values are 'cache_filter',
  79   *   'cache_menu', 'cache_page', or 'cache'.
  80   * @param $data
  81   *   The data to store in the cache. Complex data types must be serialized first.
  82   * @param $expire
  83   *   One of the following values:
  84   *   - CACHE_PERMANENT: Indicates that the item should never be removed unless
  85   *     explicitly told to using cache_clear_all() with a cache ID.
  86   *   - CACHE_TEMPORARY: Indicates that the item should be removed at the next
  87   *     general cache wipe.
  88   *   - A Unix timestamp: Indicates that the item should be kept at least until
  89   *     the given time, after which it behaves like CACHE_TEMPORARY.
  90   * @param $headers
  91   *   A string containing HTTP header information for cached pages.
  92   */
  93  function cache_set($cid, $table = 'cache', $data, $expire = CACHE_PERMANENT, $headers = NULL) {
  94    db_lock_table($table);
  95    db_query("UPDATE {". $table. "} SET data = %b, created = %d, expire = %d, headers = '%s' WHERE cid = '%s'", $data, time(), $expire, $headers, $cid);
  96    if (!db_affected_rows()) {
  97      @db_query("INSERT INTO {". $table. "} (cid, data, created, expire, headers) VALUES ('%s', %b, %d, %d, '%s')", $cid, $data, time(), $expire, $headers);
  98    }
  99    db_unlock_tables();
 100  }
 101  
 102  /**
 103   *
 104   * Expire data from the cache. If called without arguments, expirable
 105   * entries will be cleared from the cache_page table.
 106   *
 107   * @param $cid
 108   *   If set, the cache ID to delete. Otherwise, all cache entries that can
 109   *   expire are deleted.
 110   *
 111   * @param $table
 112   *   If set, the table $table to delete from. Mandatory
 113   *   argument if $cid is set.
 114   *
 115   * @param $wildcard
 116   *   If set to TRUE, the $cid is treated as a substring
 117   *   to match rather than a complete ID. The match is a right hand
 118   *   match. If '*' is given as $cid, the table $table will be emptied.
 119   */
 120  function cache_clear_all($cid = NULL, $table = NULL, $wildcard = FALSE) {
 121    global $user;
 122  
 123    if (!isset($cid) && !isset($table)) {
 124      cache_clear_all(NULL, 'cache_page');
 125      return;
 126    }
 127  
 128    if (empty($cid)) {
 129      if (variable_get('cache_lifetime', 0)) {
 130        // We store the time in the current user's $user->cache variable which
 131        // will be saved into the sessions table by sess_write(). We then
 132        // simulate that the cache was flushed for this user by not returning
 133        // cached data that was cached before the timestamp.
 134        $user->cache = time();
 135  
 136        $cache_flush = variable_get('cache_flush', 0);
 137        if ($cache_flush == 0) {
 138          // This is the first request to clear the cache, start a timer.
 139          variable_set('cache_flush', time());
 140        }
 141        else if (time() > ($cache_flush + variable_get('cache_lifetime', 0))) {
 142          // Clear the cache for everyone, cache_flush_delay seconds have
 143          // passed since the first request to clear the cache.
 144          db_query("DELETE FROM {". $table. "} WHERE expire != %d AND expire < %d", CACHE_PERMANENT, time());
 145          variable_set('cache_flush', 0);
 146        }
 147      }
 148      else {
 149        // No minimum cache lifetime, flush all temporary cache entries now.
 150        db_query("DELETE FROM {". $table. "} WHERE expire != %d AND expire < %d", CACHE_PERMANENT, time());
 151      }
 152    }
 153    else {
 154      if ($wildcard) {
 155        if ($cid == '*') {
 156          db_query("DELETE FROM {". $table. "}");
 157        }
 158        else {
 159          db_query("DELETE FROM {". $table. "} WHERE cid LIKE '%s%%'", $cid);
 160        }
 161      }
 162      else {
 163        db_query("DELETE FROM {". $table. "} WHERE cid = '%s'", $cid);
 164      }
 165    }
 166  }
 167  


Généré le : Fri Nov 30 16:20:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics