/** * Make the cache key depend on the user culture */ public function generatePageCacheKey($internalUri, $hostName, $vary, $contextualPrefix, sfViewCacheManager $viewCacheManager) { sfConfig::set('sf_cache_namespace_callable', null); $cacheKey = $viewCacheManager->generateCacheKey($internalUri, $hostName, $vary, $contextualPrefix); sfConfig::set('sf_cache_namespace_callable', array($this, 'generatePageCacheKey')); if (strpos($internalUri, '@sf_cache_partial') === 0) { return $cacheKey; } $cacheKey = $this->user->getCulture() . ':' . $cacheKey; sfConfig::set('dm_internal_page_cached', $viewCacheManager->getCache()->has($cacheKey)); return $cacheKey; }
} } class myRouting extends sfPatternRouting { public function getCurrentInternalUri($with_route_name = false) { return 'currentModule/currentAction?currentKey=currentValue'; } } $context = sfContext::getInstance(array('controller' => 'myController', 'routing' => 'myRouting', 'request' => 'myRequest')); $r = $context->routing; $r->connect('default', new sfRoute('/:module/:action/*')); // ->initialize() $t->diag('->initialize()'); $m = new sfViewCacheManager($context, $cache = new myCache()); $t->is($m->getCache(), $cache, '->initialize() takes a sfCache object as its second argument'); // ->generateCacheKey() $t->diag('->generateCacheKey'); $t->is($m->generateCacheKey('mymodule/myaction'), '/localhost/all/mymodule/myaction', '->generateCacheKey() creates a simple cache key from an internal URI'); $t->is($m->generateCacheKey('mymodule/myaction', 'foo'), '/foo/all/mymodule/myaction', '->generateCacheKey() can take a hostName as second parameter'); $t->is($m->generateCacheKey('mymodule/myaction', null, 'bar'), '/localhost/bar/mymodule/myaction', '->generateCacheKey() can take a serialized set of vary headers as third parameter'); $t->is($m->generateCacheKey('mymodule/myaction?key1=value1&key2=value2'), '/localhost/all/mymodule/myaction/key1/value1/key2/value2', '->generateCacheKey() includes request parameters as key/value pairs'); $t->is($m->generateCacheKey('mymodule/myaction?akey=value1&ckey=value2&bkey=value3'), '/localhost/all/mymodule/myaction/akey/value1/bkey/value3/ckey/value2', '->generateCacheKey() reorders request parameters alphabetically'); try { $m->generateCacheKey('@rule?key=value'); $t->fail('->generateCacheKey() throws an sfException when passed an internal URI with a rule'); } catch (sfException $e) { $t->pass('->generateCacheKey() throws an sfException when passed an internal URI with a rule'); } try { $m->generateCacheKey('@sf_cache_partial?module=mymodule&action=myaction');