source: sipes/cord/includes/cache.inc @ 4b26eb0

stableversion-3.0
Last change on this file since 4b26eb0 was d7a822e, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se agrego el directorio del cord

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