美文网首页
rails 模板中使用缓存

rails 模板中使用缓存

作者: 风暴英雄 | 来源:发表于2014-03-31 20:08 被阅读0次

这个辅助函数主要用于高速缓存视图片段而不是整个动作或者页面的方法。这种技术是非常有用,如菜单,newstopics的列表,静态HTML片段,等等。此方法需要包含您希望缓存的内容的块。

使用此方法时,你列出的缓存依赖作为高速缓存的名称,如下所示:

<% cache project do %>
  <b>All the topics on this project</b>
  <%= render project.topics %>
<% end %>

这种方法假定当有新主题添加时,会创建project。从这个调用生成的缓存键会是这样的:

views/projects/123-20120806214154/7a1156131a6928cb0026877f8b749ac9
^class ^id ^updated_at ^template tree digest
缓存会自动处理当projectupdated_at改变时

当你的模板依赖多种来源(尽可能保持事情简单),你可以命名这些依赖作为数组的一部分

<% cache [ project, current_user ] do %>
  <b>All the topics on this project</b>
  <%= render project.topics %>
<% end %>

This will include both records as part of the cache key and updating either of them will expire the cache.

Template digest

The template digest that's added to the cache key is computed by taking an md5 of the contents of the entire template file. This ensures that your caches will automatically expire when you change the template file.

Note that the md5 is taken of the entire template file, not just what's within the cache do/end call. So it's possible that changing something outside of that call will still expire the cache.

Additionally, the digestor will automatically look through your template file for explicit and implicit dependencies, and include those as part of the digest.

The digestor can be bypassed by passing skip_digest: true as an option to the cache call:

<% cache project, skip_digest: true do %>
<b>All the topics on this project</b>
<%= render project.topics %>
<% end %>
Implicit dependencies

Most template dependencies can be derived from calls to render in the template itself. Here are some examples of render calls that Cache Digests knows how to decode:

render partial: "comments/comment", collection: commentable.comments
render "comments/comments"
render 'comments/comments'
render('comments/comments')

render "header" => render("comments/header")

render(@topic) => render("topics/topic")
render(topics) => render("topics/topic")
render(message.topics) => render("topics/topic")
It's not possible to derive all render calls like that, though. Here are a few examples of things that can't be derived:

render group_of_attachments
render @project.documents.where(published: true).order('created_at')
You will have to rewrite those to the explicit form:

render partial: 'attachments/attachment', collection: group_of_attachments
render partial: 'documents/document', collection: @project.documents.where(published: true).order('created_at')
Explicit dependencies

Some times you'll have template dependencies that can't be derived at all. This is typically the case when you have template rendering that happens in helpers. Here's an example:

<%= render_sortable_todolists @project.todolists %>
You'll need to use a special comment format to call those out:

<%# Template Dependency: todolists/todolist %>
<%= render_sortable_todolists @project.todolists %>
The pattern used to match these is /# Template Dependency: ([^ ]+)/, so it's important that you type it out just so. You can only declare one template dependency per line.

External dependencies

If you use a helper method, for example, inside of a cached block and you then update that helper, you'll have to bump the cache as well. It doesn't really matter how you do it, but the md5 of the template file must change. One recommendation is to simply be explicit in a comment, like:

<%# Helper Dependency Updated: May 6, 2012 at 6pm %>
<%= some_helper_method(person) %>
Now all you'll have to do is change that timestamp when the helper method changes.

相关文章

  • rails 模板中使用缓存

    这个辅助函数主要用于高速缓存视图片段而不是整个动作或者页面的方法。这种技术是非常有用,如菜单,newstopics...

  • AngularJs $templateCache 和 $temp

    $templateCache 第一次使用模板,它被加载到模板缓存中,以便快速检索。你可以直接将模板标签加载到缓存中...

  • $templateCache

    简介 第一次使用模板,它被加载到模板缓存中,以便快速检索。你可以直接将模板标签加载到缓存中,或者通过$templa...

  • rails缓存技术

    三种缓存技术:页面,动作和片段。 Rails 默认支持片段缓存。如果想使用页面缓存和动作缓存,要在 Gemfile...

  • SpringBoot笔记2:热部署(自动重启)

    使用spring-boot-devtools 的用处: 默认禁用缓存选项。比如模板引擎将缓存编译完的模板,以避免重...

  • Flask-cache小贴士

    原文请点击这里 设置失效时间,单位秒 代码中添加缓存 模板中添加缓存 模板缓存时,缓存的部分会生成html字符串并...

  • rails slim模板使用入门

    1.创建并启动一个新的项目 2.修改默认模板 修改blog\config\application.rb 3.新建控...

  • DX中绘制物体

    DX绘制 使用顶点缓存绘制直线这次我使用了之前写好的模板来快速开始这样可以免去每次都重复编写一样的代码模板中实现了...

  • houyi平台-开发指南-持久层开发

    缓存API使用指南 导入Redis依赖工程 声明Redis读写模板 基本操作 往Redis中存放数据 redisR...

  • [原创]安装rails_admin时admin错误?

    使用gem:rails_admin 出错,无法把admin写进users中gem 'rails_admin' bu...

网友评论

      本文标题:rails 模板中使用缓存

      本文链接:https://www.haomeiwen.com/subject/sjzktttx.html