如何在 symfony 1.4 中缓存会话相关数据?

发布于 2024-12-29 08:26:47 字数 229 浏览 1 评论 0 原文

在阅读了 symfony 1.4 的缓存行为后..我开始知道 symfony 1.4 不考虑用户会话来缓存模板..

现在的情况是,我有几个模板,它们的某些部分代码依赖于用户会话..让我们假设用户经过身份验证,他可以在产品列表中提出问题...现在我应该如何缓存此模板文件...我知道的一件事是为所有会话代码块创建单独的模板并将缓存设置为 false。但我想知道其他聪明的方法,如果你有......

谢谢你, 哈迪克

After reading caching behaviour of symfony 1.4.. i came to know that symfony 1.4 is not considering user session for caching templates..

now, the situation is, i have several templates that have some part of code that depends on user session.. let's say if user is authenticated he has a facility to ask question in product listing... now how should i cache this template file... one thing i know that creating separate template for all that session code blocks and setting cache false to it. but i want to know other smart ways if you have...

Thank you,
Hardik

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

瑕疵 2025-01-05 08:26:47

警告:symfony 缓存机制不支持私有缓存。错误应用会导致数据泄露!

不要使用cache.yml

对于依赖于任何会话中的会话的内容,根本不要使用cache.yml方式,特别是受会话限制的内容。 cache.yml 无条件地向所有其他人显示用户看到的第一个版本,无论是否登录。

使用条件缓存过滤器

相反,创建一个 条件缓存过滤器。对于所有拥有凭证 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: ~

用例

这对于仅向具有特定凭据的用户显示的数据量大的页面非常有用,但所有用户都会获得相同的页面。凭证特定统计页面的集合就是一个很好的例子。

替代用途

您还可以直接指定要添加到过滤器的缓存中的页面。仍显式激活仅针对某些页面的过滤器可能是一种有用的故障保护。

// 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 }

没有真正的私有缓存

Symfony 没有针对用户私有缓存的规定。您应该使用具有私有设置的客户端缓存控制标头 对于此用例。您还可以使用 nginx 反向代理或类似设置。

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.

樱花落人离去 2025-01-05 08:26:47

你可以用这种方式缓存你的模板

<?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; ?>

you can cache your template in 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; ?>
余厌 2025-01-05 08:26:47

Hardik 随着您的应用程序变得越来越复杂,在页面上拥有动态和缓存内容的最佳方法是使用具有自己的缓存设置的部分和组件。您只需要解耦主要部分,创建部分和组件即可替换所有内容。然后,您将这个较小的部分添加到不应该被缓存的 nameYourTemplateSuccess.php 中,否则所有部分/组件都将被缓存。

有关缓存部分和组件的更多信息请此处

Hardik as your application becomes more and more complex the best way to have dynamic and cached contents on a page is to use partial and components with their own cache settings. You just need to decouple the main sections creating partial and components to replace all your contents. Then you add this smaller parts to the nameYourTemplateSuccess.php that shouldn't be cached, otherwise all partial/components will be cached.

More on caching partials and components here.

腻橙味 2025-01-05 08:26:47

我赞同 @dlondero 的带有参数的部分缓存,直到现在我才意识到这一点。

在过去的项目中,我实现了修改后的缓存过滤器和管理器,它将根据基于会话的值在操作级别进行缓存。这可能(?)更有效,因为它缓存操作的整个结果(即不仅仅是部分/组件)

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

I second @dlondero's partial caching with parameters, of which I was unaware until now.

In a past project, I implemented a modified caching filter and manager, which 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 partials/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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文