cache_dir = $newCacheDir; // Generate full-qualified path name $this->cache_dir = sprintf("%s%s", ABSPATH, $this->cache_dir); // Is our working directory up? if (!is_dir($this->cache_dir)) { // Try to create it @mkdir($this->cache_dir); @chmod($this->cache_dir, 0777); // Re-check it if (!is_dir($this->cache_dir)) { // FATAL ERROR! die("ELEMENT-CACHE: Cannot create my directory ".$this->cache_dir.". Please do so and CHMOD it to 0777."); } else { // Create .htaccess file $fp = @fopen($this->cache_dir . ".htaccess", 'w') or die("Cannot create .htaccess file!"); fwrite($fp, "Deny from all\n"); fclose($fp); } } } // Check if element is in the array // private function isHashInArray ($elgroup, $hash) { if (isset($this->elementCache[$elgroup][$hash])) { // Element is already cached! $this->currentElement = $this->elementCache[$elgroup][$hash]; $this->currentGroup = $elgroup; $this->currentHash = $hash; return true; } return false; } // Generate a hash useable for all elements // private function hash($element) { // Generate hash return md5(WP_SECRET.$element); } // Check if element is cached or not and gets the requested element // public function isElementCached ($elgroup, $element) { // Generate hash $this->currentHash = $this->hash($element); // Do we have it in cache? $status = $this->isHashInArray($elgroup, $this->currentHash); if (!$status) { // Generate filename $cacheFile = sprintf("%s%s.cache", $this->cache_dir, $elgroup); if (file_exists($cacheFile) && is_readable($cacheFile)) { // Load the cache file if (function_exists('gzopen')) { $content = ""; $fp = @gzopen($cacheFile, "r") or die("Cannot read $elgroup!"); while ( !gzeof($fp) ) { $content .= gzread($fp, 1024); } gzclose($fp); } else { $content = file_get_contents($cacheFile); } $this->elementCache[$elgroup] = unserialize($content); // Check again... $status = $this->isHashInArray($elgroup, $this->currentHash); } } // If file exists or not makes no difference the element must be inside return $status; } // Add an element to the cache (does not write it to disc!) // public function addElement($elgroup, $element, $data) { // Is the element already added? if (isset($this->elementCache[$elgroup][$this->currentHash])) return; // Do we need to flush this group to disc? if (!isset($this->flushThis[$elgroup])) $this->flushThis[$elgroup] = 1; // Add it to the memory cache $this->elementCache[$elgroup][$this->currentHash] = $data; } // Get the current loaded element // public function getCurrentElement() { // Do we have it? if (!$this->currentElement) return false; // Not set! // Okay, look it up in our array... $element = false; if (isset($this->elementCache[$this->currentGroup][$this->currentHash])) { // Load the element $element = $this->elementCache[$this->currentGroup][$this->currentHash]; // And remove the current element $this->currentElement = false; $this->currentHash = false; } // Return fetched element return $element; } // Flush all changed/new caches to disc function flushCache() { foreach ($this->flushThis as $cache=>$dummy) { // Serialize the cache $output = serialize($this->elementCache[$cache]); // Generate file name $cacheFile = sprintf("%s%s.cache", $this->cache_dir, $cache); // And put it out to the file if (function_exists('gzopen')) { // Write compressed files $fp = @gzopen($cacheFile, 'w4') or die ("Cannot flush cache ".basename($cacheFile)."!"); gzwrite($fp, $output); gzclose($fp); } elseif (function_exists('file_put_contents')) { file_put_contents($cacheFile, $output); } else { $fp = @fopen($cacheFile, 'w') or die ("Cannot flush cache ".basename($cacheFile)."!"); fwrite($fp, $output); fclose($fp); } } } } // The initializer function function element_cache_init() { global $elCache; $elCache = new Element_Cache(); // Shutdown function register_shutdown_function('element_cache_shutdown'); } // Shutdown function function element_cache_shutdown() { global $elCache; // Flush (maybe) added/changed caches $elCache->flushCache(); // Do something more... } ?>