Warning. Symfony's caching mechanism does not support private caching. Incorrect use will result in data leakage!
Do not use cache.yml
Do not use cache.yml at all for content that is session-dependent in any way, especially session-limited content. cache.yml unconditionally displays the first version that the user saw everyone else, registered or not.
Use Conditional Cache Filter
Instead, create a conditional cache filter . The following will cache each page and therefore display the first user version for all users who have myCredential .
// apps/myApp/lib/conditionalCacheFilter.php class conditionalCacheFilter extends sfFilter() { public function execute($filterChain) { $context = $this->getContext(); $user = $context->getUser(); if ($user->isAuthenticated() && $user->hasCredential('myCredential')) { foreach ($this->getParameter('pages') as $page) { $context->getViewCacheManager()->addCache($page['module'], $page['action'], array('lifeTime' => 300)); } } // Execute next filter $filterChain->execute(); } }
Use case
This is useful for a heavy data page that is displayed only to users with specific credentials, but all users get the same page. A good example is the collection of credentials with specific statistics.
Alternative use
You can also directly specify the pages that are added to the cache to the filter. This can be useful fail-safe to still explicitly activate the filter for specific pages only.
// apps/backend/lib/conditionalCacheFilter.php $context = $this->getContext(); $user = $context->getUser(); if ($user->isAuthenticated() && $user->hasPermission()) { $context->getViewCacheManager()->addCache('myModule', 'myAction', array( 'withLayout' => true, 'lifeTime' => 3600, )); }
No true private caching
Symfony has no provision for a userβs private cache. You should use the private-side client-side cache management headers for this use case. You can also use the nginx reverse proxy or similar setting.
source share