00001 <?php
00002
00003
00014 function cache_get($cid, $table = 'cache') {
00015 global $user;
00016
00017
00018 $cache_flush = variable_get('cache_flush', 0);
00019 if ($cache_flush && ($cache_flush + variable_get('cache_lifetime', 0) <= time())) {
00020
00021 variable_set('cache_flush', 0);
00022
00023 db_query("DELETE FROM {" . $table . "} WHERE expire != %d AND expire <= %d", CACHE_PERMANENT, $cache_flush);
00024 }
00025
00026 $cache = db_fetch_object(db_query("SELECT data, created, headers, expire, serialized FROM {" . $table . "} WHERE cid = '%s'", $cid));
00027 if (isset($cache->data)) {
00028
00029
00030 if ($cache->expire == CACHE_PERMANENT || !variable_get('cache_lifetime', 0)) {
00031 $cache->data = db_decode_blob($cache->data);
00032 if ($cache->serialized) {
00033 $cache->data = unserialize($cache->data);
00034 }
00035 }
00036
00037
00038
00039
00040
00041 else {
00042 if ($user->cache > $cache->created) {
00043
00044 return 0;
00045 }
00046 else {
00047 $cache->data = db_decode_blob($cache->data);
00048 if ($cache->serialized) {
00049 $cache->data = unserialize($cache->data);
00050 }
00051 }
00052 }
00053 return $cache;
00054 }
00055 return 0;
00056 }
00057
00102 function cache_set($cid, $data, $table = 'cache', $expire = CACHE_PERMANENT, $headers = NULL) {
00103 $serialized = 0;
00104 if (is_object($data) || is_array($data)) {
00105 $data = serialize($data);
00106 $serialized = 1;
00107 }
00108 $created = time();
00109 db_query("UPDATE {" . $table . "} SET data = %b, created = %d, expire = %d, headers = '%s', serialized = %d WHERE cid = '%s'", $data, $created, $expire, $headers, $serialized, $cid);
00110 if (!db_affected_rows()) {
00111 @db_query("INSERT INTO {" . $table . "} (cid, data, created, expire, headers, serialized) VALUES ('%s', %b, %d, %d, '%s', %d)", $cid, $data, $created, $expire, $headers, $serialized);
00112 }
00113 }
00114
00133 function cache_clear_all($cid = NULL, $table = NULL, $wildcard = FALSE) {
00134 global $user;
00135
00136 if (!isset($cid) && !isset($table)) {
00137
00138
00139 cache_clear_all(NULL, 'cache_block');
00140 cache_clear_all(NULL, 'cache_page');
00141 return;
00142 }
00143
00144 if (empty($cid)) {
00145 if (variable_get('cache_lifetime', 0)) {
00146
00147
00148
00149
00150 $user->cache = time();
00151
00152 $cache_flush = variable_get('cache_flush', 0);
00153 if ($cache_flush == 0) {
00154
00155 variable_set('cache_flush', time());
00156 }
00157 else if (time() > ($cache_flush + variable_get('cache_lifetime', 0))) {
00158
00159
00160 db_query("DELETE FROM {" . $table . "} WHERE expire != %d AND expire < %d", CACHE_PERMANENT, time());
00161 variable_set('cache_flush', 0);
00162 }
00163 }
00164 else {
00165
00166 db_query("DELETE FROM {" . $table . "} WHERE expire != %d AND expire < %d", CACHE_PERMANENT, time());
00167 }
00168 }
00169 else {
00170 if ($wildcard) {
00171 if ($cid == '*') {
00172 db_query("DELETE FROM {" . $table . "}");
00173 }
00174 else {
00175 db_query("DELETE FROM {" . $table . "} WHERE cid LIKE '%s%%'", $cid);
00176 }
00177 }
00178 else {
00179 db_query("DELETE FROM {" . $table . "} WHERE cid = '%s'", $cid);
00180 }
00181 }
00182 }
00183