/[drupal]/contributions/modules/cacherouter/cacherouter.inc
ViewVC logotype

Contents of /contributions/modules/cacherouter/cacherouter.inc

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


Revision 1.6 - (show annotations) (download) (as text)
Fri Dec 26 09:52:36 2008 UTC (10 months, 4 weeks ago) by slantview
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +31 -11 lines
File MIME type: text/x-php
Merge with DRUPAL-7--1
1 <?php
2 /**
3 * $Id$
4 *
5 * @file cacherouter.inc
6 * Defines the cacherouter module
7 */
8 require dirname(__FILE__) .'/CacheRouter.php';
9
10 /**
11 * Return data from the persistent cache. Data may be stored as either plain text or as serialized data.
12 * cache_get will automatically return unserialized objects and arrays.
13 *
14 * @param $cid
15 * The cache ID of the data to retrieve.
16 * @param $table
17 * The table $table to store the data in. Valid core values are 'cache_filter',
18 * 'cache_menu', 'cache_page', or 'cache' for the default cache.
19 */
20 function cache_get($cid, $table = 'cache') {
21 global $cache, $user;
22 if (!isset($cache)) {
23 $cache = new CacheRouter();
24 }
25
26 $cache_object = $cache->get($cid, $table);
27 if ($cache_object && isset($user->cache) && $user->cache > $cache_object->created) {
28 return 0;
29 }
30
31 return $cache_object;
32 }
33
34 /**
35 * Store data in the persistent cache.
36 *
37 * The persistent cache is split up into four database
38 * tables. Contributed modules can add additional tables.
39 *
40 * 'cache_page': This table stores generated pages for anonymous
41 * users. This is the only table affected by the page cache setting on
42 * the administrator panel.
43 *
44 * 'cache_menu': Stores the cachable part of the users' menus.
45 *
46 * 'cache_filter': Stores filtered pieces of content. This table is
47 * periodically cleared of stale entries by cron.
48 *
49 * 'cache': Generic cache storage table.
50 *
51 * The reasons for having several tables are as follows:
52 *
53 * - smaller tables allow for faster selects and inserts
54 * - we try to put fast changing cache items and rather static
55 * ones into different tables. The effect is that only the fast
56 * changing tables will need a lot of writes to disk. The more
57 * static tables will also be better cachable with MySQL's query cache
58 *
59 * @param $cid
60 * The cache ID of the data to store.
61 * @param $data
62 * The data to store in the cache. Complex data types will be automatically serialized before insertion.
63 * Strings will be stored as plain text and not serialized.
64 * @param $table
65 * The table $table to store the data in. Valid core values are 'cache_filter',
66 * 'cache_menu', 'cache_page', or 'cache'.
67 * @param $expire
68 * One of the following values:
69 * - CACHE_PERMANENT: Indicates that the item should never be removed unless
70 * explicitly told to using cache_clear_all() with a cache ID.
71 * - CACHE_TEMPORARY: Indicates that the item should be removed at the next
72 * general cache wipe.
73 * - A Unix timestamp: Indicates that the item should be kept at least until
74 * the given time, after which it behaves like CACHE_TEMPORARY.
75 * @param $headers
76 * A string containing HTTP header information for cached pages.
77 */
78 function cache_set($cid, $value, $table = 'cache', $expire = CACHE_PERMANENT, $headers = NULL) {
79 global $cache;
80 if (!isset($cache)) {
81 $cache = new CacheRouter();
82 }
83 return $cache->set($cid, $value, $expire, $headers, $table);
84 }
85
86 /**
87 *
88 * Expire data from the cache. If called without arguments, expirable
89 * entries will be cleared from the cache_page and cache_block tables.
90 *
91 * @param $cid
92 * If set, the cache ID to delete. Otherwise, all cache entries that can
93 * expire are deleted.
94 *
95 * @param $table
96 * If set, the table $table to delete from. Mandatory
97 * argument if $cid is set.
98 *
99 * @param $wildcard
100 * If set to TRUE, the $cid is treated as a substring
101 * to match rather than a complete ID. The match is a right hand
102 * match. If '*' is given as $cid, the table $table will be emptied.
103 */
104 function cache_clear_all($cid = NULL, $table = NULL, $wildcard = FALSE) {
105 global $cache;
106 global $user;
107
108 if (!isset($cache)) {
109 $cache = new CacheRouter();
110 }
111
112 if (!isset($cid) && !isset($table)) {
113 // Clear the block cache first, so stale data will
114 // not end up in the page cache.
115 $cache->flush('cache_block');
116 $cache->flush('cache_page');
117 return;
118 }
119
120 if (empty($cid)) {
121 if (variable_get('cache_lifetime', 0)) {
122 // We store the time in the current user's $user->cache variable which
123 // will be saved into the sessions table by sess_write(). We then
124 // simulate that the cache was flushed for this user by not returning
125 // cached data that was cached before the timestamp.
126 $user->cache = time();
127
128 $cache_flush = variable_get('cache_flush', 0);
129 if ($cache_flush == 0) {
130 // This is the first request to clear the cache, start a timer.
131 variable_set('cache_flush', time());
132 }
133 else if (time() > ($cache_flush + variable_get('cache_lifetime', 0))) {
134 // Clear the cache for everyone, cache_flush_delay seconds have
135 // passed since the first request to clear the cache.
136 $cache->flush($table);
137 variable_set('cache_flush', 0);
138 }
139 }
140 else {
141 // No minimum cache lifetime, flush all temporary cache entries now.
142 $cache->flush($table);
143 }
144 }
145 else {
146 if ($wildcard) {
147
148 if ($cid == '*') {
149
150 $cache->delete('*', $table);
151 }
152 else {
153 $cache->delete($cid . '*', $table);
154 }
155 }
156 else {
157 //find a better way to enable/disable wildcard searches
158 $cid = str_replace("*", "", $cid);
159 if($cid != ""){
160 $cache->delete($cid, $table);
161 }
162 }
163 }
164
165 }
166
167 /**
168 * Main callback from DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE phase
169 */
170 function page_cache_fastpath() {
171 global $base_root;
172 global $cache;
173
174 if (empty($_POST) && !isset($_COOKIE['cacherouter'])) {
175 if (!isset($cache)) {
176 $cache = new CacheRouter();
177 }
178 if ($cache->page_fast_cache('cache_page')) {
179 $page = $cache->get($base_root . request_uri(), 'cache_page');
180 if (!empty($page)) {
181 drupal_page_header();
182
183 //checking if first chars are compressed (always the same pattern for every header)
184 if (substr($page->data, 0,3) == "\x1f\x8b\x08") {
185
186 // Determine if the browser accepts gzipped data.
187 if (@strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === FALSE && function_exists('gzencode')) {
188 // Strip the gzip header and run uncompress.
189 $page->data = gzinflate(substr(substr($page->data, 10), 0, -8));
190 }
191 elseif (function_exists('gzencode')) {
192 //send gzip header to the browser
193 header('Content-Encoding: gzip');
194 }
195 }
196
197 print $page->data;
198
199 return TRUE;
200 }
201 }
202 }
203 return FALSE;
204 }
205

  ViewVC Help
Powered by ViewVC 1.1.2