clear($cid, $wildcard); } } /** * Ensure cache binary is statically loaded. */ private static function cache_load_ensure($bin, $skiploadifempty = FALSE) { if (!isset(static::$fastcacheitems[$bin])) { // If storage is enabled, try to load from cache. if (static::Enabled()) { if ($cache = cache_get($bin, 'fastcache')) { static::$fastcacheitems[$bin] = new fastcacheitem($bin, $cache); } // Don't bother initializing this. elseif ($skiploadifempty) { return; } } // If still not set, initialize. if (!isset(static::$fastcacheitems[$bin])) { static::$fastcacheitems[$bin] = new fastcacheitem($bin); } } } /** * cache_get wrapper. */ public static function cache_get($cid, $bin = NULL) { static::FixKeyAndBin($cid, $bin); static::cache_load_ensure($bin); return static::$fastcacheitems[$bin]->data_get($cid); } /** * cache_set wrapper. */ public static function cache_set($cid, $data, $bin = NULL) { static::FixKeyAndBin($cid, $bin); static::cache_load_ensure($bin); if (static::$fastcacheitems[$bin]->changed == FALSE) { static::$fastcacheitems[$bin]->changed = TRUE; // Do not lock if this is an atomic binary ($cid = $bin). if ($cid === $bin) { static::$fastcacheitems[$bin]->persist = TRUE; static::$fastcacheitems[$bin]->locked = FALSE; } else { // Do persist or lock if it is not enabled! if (static::Enabled()) { // Hold this locks longer than usual because // they run after the request has finished. if (function_exists('lock_acquire') && lock_acquire('fastcache_' . $bin, 120)) { static::$fastcacheitems[$bin]->persist = TRUE; static::$fastcacheitems[$bin]->locked = TRUE; } } } // Register shutdown persistence once, only if enabled! if (static::$shutdown_registered == FALSE && static::Enabled()) { register_shutdown_function(array('fastcache','fastcache_persist')); static::$shutdown_registered = TRUE; } } static::$fastcacheitems[$bin]->data_set($cid, $data); } /** * Called on shutdown, persists the cache * if necessary. */ public static function fastcache_persist () { // Try to be the last of the last... register_shutdown_function(array('fastcache','_fastcache_persist')); } /** * Persist the binaries. */ public static function _fastcache_persist () { foreach (static::$fastcacheitems as &$cache) { if ($cache->persist == TRUE) { cache_set($cache->bin, $cache->rawdata(), 'fastcache', CACHE_TEMPORARY); if ($cache->locked) { lock_release('fastcache_' . $cache->bin); } } // The binaries might be utilized on other shutdown functions... $cache->changed = FALSE; $cache->persist = FALSE; $cache->locked = FALSE; } // Fastcache may be reutilized during shutdown, make sure // persistence triggers run again. static::$shutdown_registered = FALSE; } }