key($key)); if (!empty($cache)) { $cache = unserialize($cache); } parent::set($key, $cache); return $cache; } /** * set() * Add item into cache. * * @param string $key * The key to set. * @param string $value * The data to store in the cache. * @param string $expire * The time to expire in seconds. * @param string $headers * The page headers. * @return bool * Returns TRUE on success or FALSE on failure */ function set($key, $value, $expire = CACHE_PERMENANT, $headers = NULL) { if ($expire == CACHE_TEMPORARY) { $expire = 180; } // Create new cache object. $cache = new stdClass; $cache->cid = $key; $cache->created = time(); $cache->expire = $expire; $cache->headers = $headers; if (is_array($value) || is_object($value)) { $cache->serialized = TRUE; $cache->data = serialize($value); } else { $cache->serialized = FALSE; $cache->data = $value; } if (!empty($key) && !empty($value)) { $this->lock(); // Get lookup table to be able to keep track of bins $lookup = eaccelerator_get($this->lookup); // If the lookup table is empty, initialize table if (empty($lookup)) { $lookup = array(); } // Get full key for storage and set it to 1 so we can keep track of the bin $full_key = $this->key($key); $lookup[$full_key] = 1; // Attempt to store full key and value if (!eaccelerator_put($full_key, $cache, $expire)) { unset($lookup[$full_key]); $return = FALSE; } else { $return = TRUE; } } // Resave the lookup table (even on failure) eaccelerator_put($this->lookup, $lookup, 0); // Remove lock. $this->unlock(); return $return; /** * delete() * Remove item from cache. * * @param string $key * The key to delete. * @return mixed object|bool * Returns either the cache object or FALSE on failure */ function delete($key) { // Remove from static array cache. parent::flush(); if (substr($key, -1, 1) == '*') { $this->lock(); $lookup = eaccelerator_get($this->lookup); if (!is_null($lookup)) { $lookup = unserialize($lookup); } if (is_array($lookup)) { if ($key == '*') { //Fast clean of lookup eaccelerator_put($this->lookup, array()); $this->unlock(); foreach ($lookup as $k => $v) { eaccelerator_rm($k); } return TRUE; } else { $key = substr($key, 0, strlen($key) - 1); foreach ($lookup as $k => $v) { if (strpos($k, $key) === 0) { //if (substr($k, 0, strlen($key) - 1)) { eaccelerator_rm($k); unset($lookup[$k]); } } } } else { $lookup = array(); } eaccelerator_put($this->lookup, serialize($lookup)); $this->unlock(); } else { //Remove only key - clean $lookup on flush return eaccelerator_rm($this->key($key)); } } /** * flush() * Flush the entire cache. * * @param none * @return mixed bool * Returns TRUE */ function flush() { parent::flush(); $this->lock(); // Get lookup table to be able to keep track of bins $lookup = eaccelerator_get($this->lookup); // If the lookup table is empty, remove lock and return if (empty($lookup)) { $this->unlock(); return TRUE; } $lookup = unserialize($lookup); // Cycle through keys and remove each entry from the cache if (is_array($lookup)) { foreach ($lookup as $k => $v) { if ($v == CACHE_TEMPORARY || is_null(eaccelerator_get($k))) { if (eaccelerator_rm($k)) { unset($lookup[$k]); } } } } else { $lookup = array(); } // Resave the lookup table (even on failure) eaccelerator_put($this->lookup, serialize($lookup)); // Remove lock $this->unlock(); eaccelerator_gc(); return TRUE; } /** * lock() * lock the cache from other writes. * * @param none * @return string * Returns TRUE on success, FALSE on failure */ function lock() { return eaccelerator_lock($this->lock); /* // Lock once by trying to add lock file, if we can't get the lock, we will loop // for 3 seconds attempting to get lock. If we still can't get it at that point, // then we give up and return FALSE. $time = time(); while (eaccelerator_get($this->lock)) { if (time() - $time >= 3) { return FALSE; } } eaccelerator_put($this->lock, TRUE); return TRUE; */ } /** * unlock() * lock the cache from other writes. * * @param none * @return bool * Returns TRUE on success, FALSE on failure */ function unlock() { return eaccelerator_unlock($this->lock); //return eaccelerator_rm($this->lock); } }