How to cache session-specific data in symfony 1.4?

After reading the caching behavior of symfony 1.4 .., I found out that symfony 1.4 does not consider a user session to cache templates.

Now the situation is that I have several templates that have a piece of code that depends on the user session. Say, if the user is authenticated, he has the opportunity to ask a question in the list of products ... now, how should I cache this template file ... I know that I am creating a separate template for all these session code codes and setting a cache frame for it . but I want to know other smart ways, if you have ...

Thanks Hardik

+4
source share
5 answers

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(); } } # filters.yml conditionalCache: class: conditionalCacheFilter param: pages: - { module: myModule, action: myAction } cache: ~ 

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, )); } #filters.yml conditionalCache: class: conditionalCacheFilter pages: - { module: myModule, action: myAction } 

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.

+3
source

you can cache your template this way

 <?php $cache_str='something-that-is-uniq-'.$sf_user->isAuthenticated()?$sf_user->getGuardUser()->getId():''; <?php if (!cache($cache_str,36000)): ?> <div> something that you want to cache </div> <?php cache_save(); ?> <?php endif; ?> 
+1
source

Hardik as your application becomes more complex, the best way to have dynamic and cached content on a page is to use partial and components with their own cache settings. You just need to separate the main sections, creating partial and components, to replace all your content. Then you add these smaller parts to YourTemplateSuccess.php that should not be cached, otherwise all partial / components will be cached.

Read more about caching parts and components here .

0
source

I am the second partial caching of @dlondero with parameters that I still did not know about.

In a previous project, I implemented a modified caching filter and a manager that will cache at the action level based on session-based values. This may (?) Be more efficient as it caches the entire result of the action (i.e. not just partial / components)

https://github.com/yitznewton/freerms/blob/master/apps/resolver/lib/view/freermsResolverCacheManager.class.php

https://github.com/yitznewton/freerms/blob/master/apps/resolver/lib/filter/freermsResolverCacheFilter.class.php

0
source

Source: https://habr.com/ru/post/1393475/


All Articles