Warning: The symfony cache mechanism does not support private caching. Incorrectly applied it will result in leaked data!
Do not use cache.yml
Do not use cache.yml
at all for content that depends on the session in any way, especially content that is restricted by the session. cache.yml unconditionally displays the first version a user has seen to all others, logged in or not.
Use a conditional cache filter
Instead, create a conditional cache filter. The following will cache every page, and hence display the version of the first user, for all users that have the credential 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();
}
}
# filters.yml
conditionalCache:
class: conditionalCacheFilter
param:
pages:
- { module: myModule, action: myAction }
cache: ~
Use case
This is useful for a data heavy page shown to only users with a certain credential, but all users get the same page. A collection of credential specific statistics pages is a good example.
Alternative use
You may also directly specify pages to be added in the cache to the filter. It could be a useful failsafe to still explicitly activate the filter for certain 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,
));
}
#filters.yml
conditionalCache:
class: conditionalCacheFilter
pages:
- { module: myModule, action: myAction }
No true private caching
Symfony does not have provisions for a by-user private cache. You should use client side cache control headers with the private setting for this use case. You may also use an nginx reverse proxy or similar setup.