/[drupal]/contributions/modules/memcache/memcache.db.inc
ViewVC logotype

Contents of /contributions/modules/memcache/memcache.db.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.8 - (show annotations) (download) (as text)
Thu Oct 30 19:15:37 2008 UTC (12 months, 3 weeks ago) by jeremy
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +13 -13 lines
File MIME type: text/x-php
Bug #292346: properly invalidate cache_page when cache_clear_all() called.
1 <?php
2 // $Id: memcache.db.inc,v 1.7 2008/01/29 23:43:34 slantview Exp $
3
4 require_once 'dmemcache.inc';
5
6 /** Implementation of cache.inc with memcache logic included **/
7
8 /**
9 * Return data from the persistent cache.
10 *
11 * @param $key
12 * The cache ID of the data to retrieve.
13 * @param $table
14 * The table $table to store the data in. Valid core values are 'cache_filter',
15 * 'cache_menu', 'cache_page', or 'cache' for the default cache.
16 */
17 function cache_get($key, $table = 'cache') {
18 global $user;
19
20 // Garbage collection necessary when enforcing a minimum cache lifetime
21 $cache_flush = variable_get('cache_flush', 0);
22 if ($cache_flush && ($cache_flush + variable_get('cache_lifetime', 0) <= time())) {
23 // Time to flush old cache data
24 db_query("DELETE FROM {%s} WHERE expire != %d AND expire <= %d", $table, CACHE_PERMANENT, $cache_flush);
25 variable_set('cache_flush', 0);
26 }
27
28 // If we have a memcache hit for this, return it.
29 if ($cache = dmemcache_get($key, $table)) {
30 return $cache;
31 }
32
33 // Look for a database cache hit.
34 if ($cache = db_fetch_object(db_query("SELECT data, created, headers, expire, serialized FROM {%s} WHERE cid = '%s'", $table, $key))) {
35 if (isset($cache->data)) {
36 // If the data is permanent or we're not enforcing a minimum cache lifetime
37 // always return the cached data.
38 if ($cache->expire == CACHE_PERMANENT || !variable_get('cache_lifetime', 0)) {
39 $cache->data = db_decode_blob($cache->data);
40 if ($cache->serialized) {
41 $cache->data = unserialize($cache->data);
42 }
43 }
44 // If enforcing a minimum cache lifetime, validate that the data is
45 // currently valid for this user before we return it by making sure the
46 // cache entry was created before the timestamp in the current session's
47 // cache timer. The cache variable is loaded into the $user object by
48 // sess_read() in session.inc.
49 else {
50 if ($user->cache > $cache->created) {
51 // This cache data is too old and thus not valid for us, ignore it.
52 return 0;
53 }
54 else {
55 $cache->data = db_decode_blob($cache->data);
56 if ($cache->serialized) {
57 $cache->data = unserialize($cache->data);
58 }
59 }
60 }
61 }
62
63 // By calling cache_set with an extra paramater to signify no db storage,
64 // we can lazy instantiate memcache that just comes online.
65 cache_set($key, $table, $cache->data, $cache->expire, $cache->headers, FALSE);
66 return $cache;
67 }
68 return 0;
69 }
70
71 /**
72 * Store data in the persistent cache.
73 *
74 * The persistent cache is split up into four database
75 * tables. Contributed modules can add additional tables.
76 *
77 * 'cache_page': This table stores generated pages for anonymous
78 * users. This is the only table affected by the page cache setting on
79 * the administrator panel.
80 *
81 * 'cache_menu': Stores the cachable part of the users' menus.
82 *
83 * 'cache_filter': Stores filtered pieces of content. This table is
84 * periodically cleared of stale entries by cron.
85 *
86 * 'cache': Generic cache storage table.
87 *
88 * The reasons for having several tables are as follows:
89 *
90 * - smaller tables allow for faster selects and inserts
91 * - we try to put fast changing cache items and rather static
92 * ones into different tables. The effect is that only the fast
93 * changing tables will need a lot of writes to disk. The more
94 * static tables will also be better cachable with MySQL's query cache
95 *
96 * @param $cid
97 * The cache ID of the data to store.
98 * @param $table
99 * The table $table to store the data in. Valid core values are 'cache_filter',
100 * 'cache_menu', 'cache_page', or 'cache'.
101 * @param $data
102 * The data to store in the cache. Complex data types must be serialized first.
103 * @param $expire
104 * One of the following values:
105 * - CACHE_PERMANENT: Indicates that the item should never be removed unless
106 * explicitly told to using cache_clear_all() with a cache ID.
107 * - CACHE_TEMPORARY: Indicates that the item should be removed at the next
108 * general cache wipe.
109 * - A Unix timestamp: Indicates that the item should be kept at least until
110 * the given time, after which it behaves like CACHE_TEMPORARY.
111 * @param $headers
112 * A string containing HTTP header information for cached pages.
113 * @param $db_storage
114 * This boolean is unique to the memcache.inc implementation of cache set.
115 * It allows us to do a cache_set and not write to the database, but only
116 * to memcache.
117 */
118 function cache_set($cid, $table = 'cache', $data, $expire = CACHE_PERMANENT, $headers = NULL, $db_storage = TRUE) {
119 $time = time();
120
121 // Create new cache object.
122 $cache = new stdClass;
123 $cache->cid = $cid;
124 $cache->data = is_object($data) ? memcache_clone($data) : $data;
125 $cache->created = $time;
126 $cache->expire = $expire;
127 $cache->headers = $headers;
128
129 if ($db_storage) {
130 $serialized = 0;
131 if (is_object($data) || is_array($data)) {
132 $data = serialize($data);
133 $serialized = 1;
134 }
135 // Save to the database
136 db_query("
137 INSERT INTO {$table} (data, created, expire, headers, serialized, cid) VALUES (%b, %d, %d, '%s', %d, '%s') ON DUPLICATE KEY
138 UPDATE data = %b, created = %d, expire = %d, headers = '%s', serialized = %d",
139 $data, $time, $expire, $headers, $serialized, $cid,
140 $data, $time, $expire, $headers, $serialized, $cid);
141 }
142
143 // Save to memcache
144 if ($expire == CACHE_TEMPORARY) {
145 $expire = variable_get('cache_lifetime', 2591999);
146 }
147 dmemcache_set($cid, $cache, $expire, $table);
148 }
149
150 /**
151 *
152 * Expire data from the cache. If called without arguments, expirable
153 * entries will be cleared from the cache_page table.
154 *
155 * Memcache logic is simpler than the core cache because memcache doesn't have
156 * a minimum cache lifetime consideration (it handles it internally), and
157 * doesn't support wildcards. Wildcard flushes result in the entire table
158 * being flushed.
159 *
160 * @param $cid
161 * If set, the cache ID to delete. Otherwise, all cache entries that can
162 * expire are deleted from the specified table.
163 *
164 * @param $table
165 * If set, the table delete from.
166 *
167 * @param $wildcard
168 * If set to TRUE, the $cid is treated as a substring
169 * to match rather than a complete ID. The match is a right hand
170 * match. If '*' is given as $cid, the table $table will be emptied.
171 */
172 function cache_clear_all($cid = NULL, $table = NULL, $wildcard = FALSE) {
173 global $user;
174
175 if (!isset($cid) && !isset($table)) {
176 $cid = '*';
177 $wildcard = TRUE;
178 $table = 'cache_page';
179 }
180 if (empty($cid) || ($cid == '*' && $wildcard !== TRUE)) {
181 # don't do anything if cid is unset. this matches the default drupal behavior...
182 if ($wildcard && $cid != '*') {
183 if (variable_get('memcache_debug', FALSE)) {
184 // call watchdog, since you probably didn't want to flush the entire bin.
185 watchdog('memcache', "illegal wildcard in cache_clear_all - not flushing entire bin. table: $table. cid: $cid", WATCHDOG_WARNING);
186 }
187 }
188 }
189 else if ($cid == '*' || $wildcard === TRUE) {
190 dmemcache_flush($table);
191 }
192 else {
193 dmemcache_delete($cid, $table);
194 }
195
196 if (empty($cid)) {
197 if (variable_get('cache_lifetime', 0)) {
198 // We store the time in the current user's $user->cache variable which
199 // will be saved into the sessions table by sess_write(). We then
200 // simulate that the cache was flushed for this user by not returning
201 // cached data that was cached before the timestamp.
202 $user->cache = time();
203
204 $cache_flush = variable_get('cache_flush', 0);
205 if ($cache_flush == 0) {
206 // This is the first request to clear the cache, start a timer.
207 variable_set('cache_flush', time());
208 }
209 else if (time() > ($cache_flush + variable_get('cache_lifetime', 0))) {
210 // Clear the cache for everyone, cache_flush_delay seconds have
211 // passed since the first request to clear the cache.
212 db_query("DELETE FROM {%s} WHERE expire != %d AND expire < %d", $table, CACHE_PERMANENT, time());
213 variable_set('cache_flush', 0);
214 }
215 }
216 else {
217 // No minimum cache lifetime, flush all temporary cache entries now.
218 db_query("DELETE FROM {%s} WHERE expire != %d AND expire < %d", $table, CACHE_PERMANENT, time());
219 }
220 }
221 else {
222 if ($wildcard) {
223 if ($cid == '*') {
224 db_query("DELETE FROM {%s}", $table);
225 }
226 else {
227 db_query("DELETE FROM {%s} WHERE cid LIKE '%s%%'", $table, $cid);
228 }
229 }
230 else {
231 db_query("DELETE FROM {%s} WHERE cid = '%s'", $table, $cid);
232 }
233 }
234 }
235
236 /**
237 * Provide a substitute clone() function for PHP4. This is a copy of drupal_clone
238 * because common.inc isn't included early enough in the bootstrap process to
239 * be able to depend on drupal_clone.
240 */
241 function memcache_clone($object) {
242 return version_compare(phpversion(), '5.0') < 0 ? $object : clone($object);
243 }

  ViewVC Help
Powered by ViewVC 1.1.2