问题描述
我的应用程序将命名空间用于管理目的.我最近尝试开始使用动作缓存,但是在尝试使用 expire_action 使缓存过期时遇到了一些问题.基本上,我的默认命名空间 newsposts 控制器中有一个索引操作,它使用如下操作缓存进行缓存:
My application is using a namespace for administrative purposes. I recently tried to start using action caching however I ran into some problems trying to expire the cache using expire_action. Basically I have a index action in my default namespace newsposts controller that is cached using action caching like this:
class NewspostsController < ApplicationController
caches_action :index, :layout => false
def index
@posts = Newspost.includes(:author).order("created_at DESC").limit(5)
end
end
这会缓存views/host/newsposts下的视图.
This caches the view under views/host/newsposts.
默认命名空间没有修改数据的操作,它们都在我的管理命名空间中.在我的 Admin::NewspostsController 中,我试图在创建操作中使此缓存过期,如下所示:
The default namespace has no actions for modifying data, they are all in my admin namespace. In my Admin::NewspostsController I am trying to expire this cache in the create action like this:
expire_action(:controller => 'newsposts', :action => 'index')
但是,这将使位于views/host/admin/newsposts 下的缓存文件过期.显然它不能工作,因为我在 admin 命名空间中,并且 rails 正在(正确地)寻找这个命名空间的过期缓存.遗憾的是我无法将命名空间参数传递给 axpire_action 方法,那么如何使另一个命名空间中的操作缓存过期?
however this will expire a cache file located under views/host/admin/newsposts. Obviously it can not work since im in the admin namespace and rails is (rightfully) looking to expire cache for this namespace. Sadly I can not pass a namespace parameter to the axpire_action method, so how can i expire the action cache in another namespace?
推荐答案
经过一番挖掘,我终于找到了解决方案.在 url_for 方法中有点暗示:
after some more digging I finally found the solution. It's a bit hinted in the url_for method:
特别是,前导斜杠确保不假定命名空间.因此,如果当前控制器位于该模块下,则 url_for :controller => 'users' 可能会解析为 Admin::UsersController,但 url_for :controller => '/users' 确保无论如何链接到 ::UsersController.
In particular, a leading slash ensures no namespace is assumed. Thus, while url_for :controller => 'users' may resolve to Admin::UsersController if the current controller lives under that module, url_for :controller => '/users' ensures you link to ::UsersController no matter what.
基本上,
expire_action(:controller => '/newsposts', :action => 'index')
将在默认命名空间中过期,并且
Will expire in the default namespace, and
expire_action(:controller => 'admin/newsposts', :action => 'index')
在管理命名空间中(默认时).
in the admin namespace (when in default).
这篇关于rails缓存:另一个命名空间中的expire_action的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!