1

我的应用程序使用 Rails 4.0.0。我有patcial模型users_posts

class UsersPost < ActiveRecord::Base
  attr_accessible :post_id, :user_id, :example
  belongs_to :post, touch: true

posts

class Post < ActiveRecord::Base
  has_many :users_posts

这是 users_post 视图:

-cache users_post do
  .box.col2
    p.center
      =status_post_vk(users_post)
      =status_post_fb(users_post) 
    p = check_box_tag "posts_ids[]", users_post.id
    p = best_in_place users_post.post, :text, type: :textarea
    p = raw users_post.post_url_small.map{|url| image_tag 'http://bla.com/'+url}.join()
    p 
      = link_to 'Show', users_post
      = link_to 'Edit', edit_users_post_path(users_post)
      = link_to 'Destroy', users_post, data: { confirm: 'Text' }, :method => :delete

当我更改帖子的文本(更新数据库中的属性文本)时,如何使缓存密钥过期best_in_place users_post.post, :text, type: :textarea

4

1 回答 1

1

您可以通过添加gem 'dalli'. 供查看:

-cache ["#{current_user.id}",users_post], skip_digest: true do
  .box.col2
    p.center
      =status_post_vk(users_post)
      =status_post_fb(users_post) 
    p = check_box_tag "posts_ids[]", users_post.id
    p = best_in_place users_post.post, :text, type: :textarea
    p = raw users_post.post_url_small.map{|url| image_tag 'http://bla_bla.com/'+url}.join()
    p 
      = link_to 'Show', users_post
      = link_to 'Edit', edit_users_post_path(users_post)
      = link_to 'Destroy', users_post, data: { confirm: 'Text' }, :method => :delete

在控制器中添加 expire_fragment,如下所示:

class PostsController < ApplicationController
  after_filter  :cache_clear, :only => [:create, :update, :destroy]

  def cache_clear
    expire_fragment /.*#{current_user.id}.*/
  end

更新后,创建或销毁缓存过期的帖子。

于 2013-11-08T10:32:20.543 回答