我应该如何为具有大量内容更新的动态页面设置 dalli? [英] How should I set up dalli for a dynamic page with lots of content updates?

查看:37
本文介绍了我应该如何为具有大量内容更新的动态页面设置 dalli?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 dalli(缓存)对于提高静态网站的性能非常有用.
但是,经常更新的动态网站呢?
设置 dalli 的正确方法是什么?

我遇到的一个问题,例如:dalli 在使用分页时将不同的 params[:page] 识别为同一页面:(

当两者都使用 dalli 时,你们会如何设计系统

  • 经常更新的页面
  • 不会经常更新的页面

我现在的代码就是这样.这就是为什么我在使用缓存时面临分页问题...

控制器

caches_page :index, :show

config/environments/development.rb

config.think_all_requests_local = trueconfig.action_controller.perform_caching = trueconfig.cache_store = :dalli_store

解决方案

我遇到了同样的问题,并决定在视图中缓存片段是更好的选择.

片段的缓存键基于模型,并且会在每次模型更新时发生变化.

这是我在haml中的一个观点的摘录.

 - @content_pages.each 做 |content_page|- 缓存(content_page.get_cache_key('home_page_content_page'))做...渲染 ContentPage 模型

它基于此处 37Signals 推荐的方法基于键的缓存过期的工作原理

我在我的网站上写了完整的方法

使用基于键的方法的Rails缓存策略

希望能帮到你

I know dalli (caching) is pretty useful to enhance performance for static sites.
But what about dynamic sites, which are updated quite often?
What is the correct way to set up dalli?

One problem I've encountered for example: dalli recognizes different params[:page] as the same page when using pagination:(

How would you guys design the system when using dalli for both

  • the page that gets updated so often
  • the page that won't be updated so often

My current code is just like this. That's why I'm facing the pagination problem when using caching...

controller

caches_page :index, :show

config/environments/development.rb

config.consider_all_requests_local       = true
config.action_controller.perform_caching = true
config.cache_store = :dalli_store

解决方案

I had the same problem and decided that fragment caching in the views was a better option.

The cache keys for the fragments are based on the models and will change every time a model is updated.

Here is an extract from one of my views in haml.

 - @content_pages.each do |content_page|
          - cache(content_page.get_cache_key('home_page_content_page')) do
            ...Render ContentPage model

It is based on an approach recommended by 37Signals here How key-based cache expiration works

I have a full write up of the approach on my website here

Rails caching strategy using key-based approach

Hope this helps

这篇关于我应该如何为具有大量内容更新的动态页面设置 dalli?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆