ruby-on-rails – Rails缓存:使用清扫器进行需要参数的操作
发布时间:2020-12-17 01:58:31 所属栏目:百科 来源:网络整理
导读:我正在尝试使用扫地机来处理我的页面刷新.为了刷新索引操作等等一切正常……但我似乎无法清理器解释页面参数.如果有人能告诉我下面的代码有什么问题,我会非常感激: 控制器: class PostsController ApplicationController load_and_authorize_resource cach
|
我正在尝试使用扫地机来处理我的页面刷新.为了刷新索引操作等等一切正常……但我似乎无法清理器解释页面参数.如果有人能告诉我下面的代码有什么问题,我会非常感激:
控制器: class PostsController < ApplicationController
load_and_authorize_resource
cache_sweeper :post_sweeper,:only => [:create,:update,:destroy]
caches_page :index
caches_page :show
caches_action :edit,:new
# This refreshes cache correctly
def index
@posts = Post.all
end
#这会创建缓存,但不会刷新(永远).如果我将expire_page命令直接放入操作(而不是清理程序),它可以正常工作 def update
@post = Post.find(params[:id])
respond_to do |format|
if @post.update_attributes(params[:post])
flash[:notice] = t(:post_updated)
format.html { redirect_to(@post) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @post.errors,:status => :unprocessable_entity }
end
end
end
扫地机: class PostSweeper < ActionController::Caching::Sweeper
observe Post
def after_create(record)
expire_cache_for_index(record)
end
def after_update(record)
expire_cache_for_index(record)
expire_cache_for_post(record)
expire_cache_for_edit(record)
end
def after_destroy(record)
expire_cache_for_index(record)
expire_cache_for_post(record)
expire_cache_for_edit(record)
end
private
def expire_cache_for_index(record)
expire_page :controller => 'posts',:action => 'index'
end
def expire_cache_for_post(record)
expire_page :controller => 'posts',:action => 'show',:id => record.id
end
def expire_case_for_edit(record)
expire_action :controller => 'posts',:action => 'edit',:id => record.id
end
end
解决方法
如果我们假设你复制并粘贴了代码,那么拼写错误也在你的代码中.由于您没有被Rails标记为错误,我们可以假设没有调用扫描程序. (即没有调用after_update).我会添加一些记录器消息来验证确实是这种情况.
问题是关于邮政: >它是ActiveRecord :: Base的死者吗? 网上的清扫器示例始终将cache_sweeper行放在caches_xxx行之后.如果这有所作为,但值得一试,我会感到惊讶. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
