Package base :: Package includes :: Module cache
[hide private]

Source Code for Module base.includes.cache

  1  #!/usr/bin/env python 
  2  # $Id: cache.inc,v 1.20 2008/07/02 20:42:25 dries Exp $ 
  3   
  4  """ 
  5    Cache functions 
  6   
  7    @package includes 
  8    @see <a href='http://drupy.net'>Drupy Homepage</a> 
  9    @see <a href='http://drupal.org'>Drupal Homepage</a> 
 10    @note Drupy is a port of the Drupal project. 
 11    @note This file was ported from Drupal's includes/cache.inc 
 12    @author Brendon Crawford 
 13    @copyright 2008 Brendon Crawford 
 14    @contact message144 at users dot sourceforge dot net 
 15    @created 2008-01-10 
 16    @version 0.1 
 17    @note License: 
 18   
 19      This program is free software; you can redistribute it and/or 
 20      modify it under the terms of the GNU General Public License 
 21      as published by the Free Software Foundation; either version 2 
 22      of the License, or (at your option) any later version. 
 23   
 24      This program is distributed in the hope that it will be useful, 
 25      but WITHOUT ANY WARRANTY; without even the implied warranty of 
 26      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 27      GNU General Public License for more details. 
 28   
 29      You should have received a copy of the GNU General Public License 
 30      along with this program; if not, write to: 
 31       
 32      The Free Software Foundation, Inc., 
 33      51 Franklin Street, Fifth Floor, 
 34      Boston, MA  02110-1301, 
 35      USA 
 36  """ 
 37   
 38  __version__ = "$Revision: 1 $" 
 39   
 40  from lib.drupy import DrupyPHP as php 
 41  import bootstrap as lib_bootstrap 
 42  import database as lib_database 
 43   
44 -def get(cid, table = 'cache'):
45 """ 46 Return data from the persistent cache. Data may be stored as 47 either plain text or as serialized data. 48 cache_get will automatically return unserialized objects and arrays. 49 50 @param cid 51 The cache ID of the data to retrieve. 52 @param table 53 The table table to store the data in. 54 Valid core values are 'cache_filter', 55 'cache_menu', 'cache_page', or 'cache' for the default cache. 56 @return The cache or FALSE on failure. 57 """ 58 # Garbage collection necessary when enforcing a minimum cache lifetime 59 cache_flush = lib_bootstrap.variable_get('cache_flush', 0); 60 if (cache_flush and (cache_flush + \ 61 variable_get('cache_lifetime', 0) <= php.time_())): 62 # Reset the variable immediately to prevent a meltdown in heavy 63 # load situations. 64 variable_set('cache_flush', 0); 65 # Time to php.flush old cache data 66 lib_database.query(\ 67 "DELETE FROM {" + table + "} WHERE expire != %d AND expire <= %d", \ 68 CACHE_PERMANENT, cache_flush); 69 cache = lib_database.fetch_object(lib_database.query(\ 70 "SELECT data, created, headers, expire, serialized " + \ 71 "FROM {" + table + "} WHERE cid = '%s'", cid)); 72 if (php.isset(cache, 'data')): 73 # If the data is permanent or we're not enforcing a minimum cache lifetime 74 # always return the cached data. 75 if (cache.expire == CACHE_PERMANENT or not \ 76 variable_get('cache_lifetime', 0)): 77 cache.data = db_decode_blob(cache.data); 78 if (cache.serialized): 79 cache.data = php.unserialize(cache.data); 80 # If enforcing a minimum cache lifetime, validate that the data is 81 # currently valid for this user before we return it by making sure the 82 # cache entry was created before the timestamp in the current session's 83 # cache timer. The cache variable is loaded into the user object by 84 # sess_read() in session.inc. 85 else: 86 if (lib_appglobals.user.cache > cache.created): 87 # This cache data is too old and thus not valid for us, ignore it. 88 return False; 89 else: 90 cache.data = db_decode_blob(cache.data); 91 if (cache.serialized): 92 cache.data = php.unserialize(cache.data); 93 return cache; 94 return False;
95 96 97
98 -def set(cid, data, table = 'cache', expire = None, headers = None):
99 """ 100 Store data in the persistent cache. 101 102 The persistent cache is split up into four database 103 tables. Contributed plugins can add additional tables. 104 105 'cache_page': This table stores generated pages for anonymous 106 users. This is the only table affected by the page cache setting on 107 the administrator panel. 108 109 'cache_menu': Stores the cachable part of the users' menus. 110 111 'cache_filter': Stores filtered pieces of content. This table is 112 periodically cleared of stale entries by cron. 113 114 'cache': Generic cache storage table. 115 116 The reasons for having several tables are as follows: 117 118 - smaller tables allow for faster selects and inserts 119 - we try to put fast changing cache items and rather static 120 ones into different tables. The effect is that only the fast 121 changing tables will need a lot of writes to disk. The more 122 static tables will also be better cachable with MySQL's query cache 123 124 @param cid 125 The cache ID of the data to store. 126 @param data 127 The data to store in the cache. Complex data types will be 128 automatically serialized before insertion. 129 Strings will be stored as plain text and not serialized. 130 @param table 131 The table table to store the data in. Valid core values are 'cache_filter', 132 'cache_menu', 'cache_page', or 'cache'. 133 @param expire 134 One of the following values: 135 - CACHE_PERMANENT: Indicates that the item should never be removed unless 136 explicitly told to using cache_clear_all() with a cache ID. 137 - CACHE_TEMPORARY: Indicates that the item should be removed at the next 138 general cache wipe. 139 - A Unix timestamp: Indicates that the item should be kept at least until 140 the given time, after which it behaves like CACHE_TEMPORARY. 141 @param headers 142 A string containing HTTP php.header information for cached pages. 143 """ 144 if expire is None: 145 expire = lib_bootstrap.CACHE_PERMANENT 146 serialized = 0; 147 if (not php.is_string(data)): 148 data = php.serialize(data); 149 serialized = 1; 150 created = php.time_(); 151 lib_database.query( 152 "UPDATE {" + table + "} SET data = %b, created = %d, expire = %d, " + \ 153 "headers = '%s', serialized = %d WHERE cid = '%s'", \ 154 data, created, expire, headers, serialized, cid);
155 #if (not db_affected_rows()): 156 #db_query("INSERT INTO {" + table + "} (cid, data, created, 157 #expire, headers, serialized) VALUES ('%s', %b, %d, %d, '%s', %d)", 158 #cid, data, created, expire, headers, serialized); 159 160 161 162
163 -def clear_all(cid = None, table = None, wildcard = False):
164 """ 165 Expire data from the cache. If called without arguments, expirable 166 entries will be cleared from the cache_page and cache_block tables. 167 168 @param cid 169 If set, the cache ID to delete. Otherwise, all cache entries that can 170 expire are deleted. 171 172 @param table 173 If set, the table table to delete from. Mandatory 174 argument if cid is set. 175 176 @param wildcard 177 If set to TRUE, the cid is treated as a substring 178 to match rather than a complete ID. The match is a right hand 179 match. If '*' is given as cid, the table table will be emptied. 180 """ 181 thisTime = php.time_(); 182 if (cid == None and table == None): 183 # Clear the block cache first, so stale data will 184 # not end up in the page cache. 185 clear_all(None, 'cache_block'); 186 clear_all(None, 'cache_page'); 187 return; 188 if (cid == None): 189 if (variable_get('cache_lifetime', 0)): 190 # We store the time in the current user's user.cache variable which 191 # will be saved into the sessions table by sess_write(). We then 192 # simulate that the cache was flushed for this user by not returning 193 # cached data that was cached before the timestamp. 194 lib_appglobals.user.cache = thisTime; 195 cache_flush = variable_get('cache_flush', 0); 196 if (cache_flush == 0): 197 # This is the first request to clear the cache, start a timer. 198 variable_set('cache_flush', thisTime); 199 elif (thisTime > (cache_flush + variable_get('cache_lifetime', 0))): 200 # Clear the cache for everyone, cache_flush_delay seconds have 201 # passed since the first request to clear the cache. 202 db_query(\ 203 "DELETE FROM {" + table + "} WHERE expire != %d AND expire < %d", \ 204 CACHE_PERMANENT, thisTime); 205 variable_set('cache_flush', 0); 206 else: 207 # No minimum cache lifetime, php.flush all temporary cache entries now. 208 db_query(\ 209 "DELETE FROM {" + table + "} WHERE expire != %d AND expire < %d", \ 210 CACHE_PERMANENT, thisTime); 211 else: 212 if (wildcard): 213 if (cid == '*'): 214 db_query("DELETE FROM {" + table + "}"); 215 else: 216 db_query("DELETE FROM {" + table + "} WHERE cid LIKE '%s%%'", cid); 217 else: 218 db_query("DELETE FROM {" + table + "} WHERE cid = '%s'", cid);
219