public function forTemplate() { $key = $this->getKey(); $cache = $this->getCache(); $data = $cache->get($key); if ($data && !$this->forceRegen) { if (defined('PROXY_CACHE_GENERATING')) { return '<!--SimpleCache::' . $key . ',FragmentCache-->'; } return $data; } $data = $this->context->renderWith($this->templateName); $data = $data->raw(); if (Versioned::current_stage() != 'Stage' && strlen($data) && !isset($_GET['flush'])) { // $this->cache->store($key, $menu, self::CACHE_LENGTH); // when storing for the frontend cache replacement stuff, use whatever we have // configured // do NOT store if we're on stage or we have _flush=1 $this->cache->store($key, $data); } // clear the cache key for next request if (isset($_GET['flush'])) { $this->cache->delete($key); } if (defined('PROXY_CACHE_GENERATING')) { return '<!--SimpleCache::' . $key . ',FragmentCache-->'; } return $data; }
public function testClearThousands() { Filesystem::removeFolder(TEMP_FOLDER . '/my_test_cache'); $cache = new SimpleCache(new SimpleFileBasedCacheStore(TEMP_FOLDER . '/my_test_cache')); for ($i = 0; $i < 1000; $i++) { $object = new stdClass(); $object->Title = "Object {$i}"; $other = $i % 10; $cache->store('key_' . $i, $object, -1, array('mytag', "mod{$other}")); } $start = microtime(true); $cache->deleteByTag('mytag'); $end = microtime(true) - $start; $elems = $cache->getByTag('mytag'); $this->assertEquals(0, count($elems)); }
public function generateCache($host, $url) { $url = $this->urlForCaching($url); $key = "{$host}/{$url}"; define('PROXY_CACHE_GENERATING', true); $config = $this->configForUrl($url, $host); $expiry = isset($config['expiry']) ? $config['expiry'] : -1; if ($expiry < 0) { return; } ob_start(); include BASE_PATH . '/framework/main.php'; $toCache = new stdClass(); $toCache->Content = ob_get_clean(); $toCache->LastModified = date('Y-m-d H:i:s'); // check headers to see if we have an expires header $headers = headers_list(); if ($headerExpiry = $this->ageFromHeaders($headers, $expiry)) { if ($headerExpiry < $expiry && $headerExpiry != 0) { $expiry = $headerExpiry; } } $storedHeaders = $this->headersToStore($headers); if (count($storedHeaders)) { $toCache->Headers = $storedHeaders; } $toCache->Age = $expiry; if (function_exists('http_response_code')) { $response = http_response_code(); $this->enabled = $response >= 200 && $response < 300; } // see if we've got a no cache header if (isset($storedHeaders['x-silverstripe-nocache'])) { $this->enabled = false; } // store the content for this if ($this->enabled && $this->dynamicCache && strlen($toCache->Content)) { $tags = isset($config['tags']) ? $config['tags'] : null; $this->dynamicCache->store($key, $toCache, $expiry, $tags); if ($this->includeCacheHeaders) { $this->headers[] = 'X-SilverStripe-Cache: miss-gen at ' . date('r'); } } $this->currentItem = $toCache; }
public function generateCache($host, $url) { $key = "{$host}/{$url}"; define('PROXY_CACHE_GENERATING', true); $config = $this->configForUrl($url); $expiry = isset($config['expiry']) ? $config['expiry'] : -1; if ($expiry < 0) { return; } ob_start(); include BASE_PATH . '/framework/main.php'; $toCache = new stdClass(); $toCache->Content = ob_get_clean(); $toCache->LastModified = date('Y-m-d H:i:s'); $toCache->Age = $expiry; // store the content for this if ($this->dynamicCache && strlen($toCache->Content)) { $tags = isset($config['tags']) ? $config['tags'] : null; $this->dynamicCache->store($key, $toCache, $expiry, $tags); $this->headers[] = 'X-SilverStripe-Cache: miss-gen at ' . @date('r') . ' on ' . $key; } $this->currentItem = $toCache; }