DCache 6.x-1.x-dev
Drupal Advanced Cache
|
00001 <?php 00002 // $Id$ 00003 00016 // Cache masks. 00020 define ('DCACHE_SESSION', 0); 00021 00025 define ('DCACHE_STATIC', 1); 00026 00030 define ('DCACHE_DRUPAL', 2); 00031 00035 define ('DCACHE_ALL', -1); 00036 00062 function dcache_set($cache_namespace, $cache_args, $mode = DCACHE_SESSION, $infinite = FALSE) { 00063 switch ($mode) { 00064 case DCACHE_SESSION: 00065 case DCACHE_STATIC: { 00066 $svar =& _dcache_by_ref($cache_namespace, $mode); 00067 } break; 00068 case DCACHE_DRUPAL: { 00069 $svar = array(); 00070 } break; 00071 } 00072 00073 foreach ($cache_args as $arg => $val) { 00074 $svar[$arg] = $val; 00075 } 00076 $svar['cache_timestamp'] = $infinite ? 0 : time(); 00077 00078 if ($mode == DCACHE_DRUPAL) { 00079 $ns = _dcache_ns($cache_namespace); 00080 cache_set($ns, $svar); 00081 } 00082 } 00083 00105 function dcache_get($cache_namespace, $mode = DCACHE_SESSION) { 00106 switch ($mode) { 00107 case DCACHE_SESSION: 00108 case DCACHE_STATIC: { 00109 $svar =& _dcache_by_ref($cache_namespace, $mode); 00110 } break; 00111 case DCACHE_DRUPAL: { 00112 $ns = _dcache_ns($cache_namespace); 00113 $svar = cache_get($ns); 00114 $svar = $svar->data; 00115 } break; 00116 } 00117 return $svar; 00118 } 00119 00138 function dcache_del($cache_namespace, $mode = DCACHE_SESSION) { 00139 $ns = _dcache_ns($cache_namespace); 00140 switch ($mode) { 00141 case DCACHE_SESSION: 00142 case DCACHE_STATIC: { 00143 $svar =& _dcache_by_ref($cache_namespace, $mode); 00144 $svar = NULL; 00145 } break; 00146 case DCACHE_DRUPAL: { 00147 cache_clear_all($ns, 'cache'); 00148 } break; 00149 case DCACHE_ALL: { 00150 dcache_del($cache_namespace, DCACHE_SESSION); 00151 dcache_del($cache_namespace, DCACHE_STATIC); 00152 cache_clear_all($ns, 'cache', TRUE); 00153 } 00154 } 00155 } 00156 00166 function dcache_expired($cache) { 00167 return TRUE; 00168 $life = variable_get('dcache_life_seconds', 600); 00169 if (function_exists('duvar_get')) { 00170 $life = duvar_get('dcache_life_seconds', $life); 00171 } 00172 return (isset($cache['cache_timestamp']) && (time() - $cache['cache_timestamp'] < $life)) ? FALSE : TRUE; 00173 } 00174 00195 function &_dcache_by_ref($cns, $mode) { 00196 global $dcache_static; 00197 switch ($mode) { 00198 case DCACHE_SESSION: { 00199 $cache =& $_SESSION['dcache']; 00200 } break; 00201 case DCACHE_STATIC: { 00202 $cache =& $dcache_static; 00203 } break; 00204 case DCACHE_DRUPAL: { 00205 } break; 00206 } 00207 if ($mode != DCACHE_DRUPAL) { 00208 if (is_array($cns)) { 00209 switch (count($cns)) { 00210 case 0: { 00211 $ns =& $cache; 00212 } break; 00213 case 1: { 00214 $ns =& $cache[$cns[0]]; 00215 } break; 00216 case 2: { 00217 $ns =& $cache[$cns[0]][$cns[1]]; 00218 } break; 00219 case 3: { 00220 $ns =& $cache[$cns[0]][$cns[1]][$cns[2]]; 00221 } break; 00222 case 4: { 00223 $ns =& $cache[$cns[0]][$cns[1]][$cns[2]][$cns[3]]; 00224 } break; 00225 case 5: { 00226 $ns =& $cache[$cns[0]][$cns[1]][$cns[2]][$cns[3]][$cns[4]]; 00227 } break; 00228 default: { 00229 throw new Exception(t('Cache namespace elements exceed a count of 5.')); 00230 } break; 00231 } 00232 } 00233 else { 00234 $ns =& $cache[$cns]; 00235 } 00236 return $ns; 00237 } 00238 else { 00239 } 00240 } 00241 00251 function _dcache_ns($namespace) { 00252 $bt = debug_backtrace(); 00253 $bt = explode(DIRECTORY_SEPARATOR, $bt[1]['file']); 00254 for ($idx = 0; $idx < count($bt) - 1 && $bt[$idx] != 'modules'; $idx++) { 00255 $module = $bt[$idx + 1]; 00256 } 00257 $ns = "{$module}_"; 00258 if (is_array($namespace)) { 00259 $ns .= implode('_', $namespace); 00260 } 00261 else { 00262 $ns .= $namespace; 00263 } 00264 return $ns; 00265 }