的ActiveResource +缓存 [英] ActiveResource + Caching

查看:155
本文介绍了的ActiveResource +缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建设,通过与的ActiveResource一个API消耗模型的应用程序。我注意到

I'm building an application that consumes models through an api with ActiveResource. I noticed that the

@resource ||= @resource.do a query 

不工作,也就是说,如果我把类似的东西在我的控制,我的申请将仍然查询API。因此,有没有内置的缓存,我已经习惯了用ActiveRecord。时间扩大我的知识和技能基础,ok了。

doesn't work, i.e. If I put something like that in my controller, my application will still query the api. So there is no built in caching that I'm used to with ActiveRecord. Time to expand my knowledge and skill base, ok.

我发现这一点: http://injectisforwizards.com/blog/read - 通过缓存 - -的的ActiveResource / ,虽然我不明白这100%然而,对于做.find基于控制器的查询,这似乎工作。但没有任何自定义查询我有例如:

I found this: http://injectisforwizards.com/blog/read-through-caching-of-activeresource/, and while I don't understand this 100% yet, for controller based queries that do .find, this appears to work. But not for any custom queries I have e.g.:

@current_resource ||= Resource.get(:resource_all, :by_account=>@current_account.account_key)

(其中打一个定制的控制器,并运行一个范围,返回集合)

(which hits a custom controller and runs a scope, returning a collection)

我通过这个工作,我会发现什么是怎么回事,但我很好奇,如果有人能告诉我简单来说是怎么回事,我能做些什么来抚平缓存中的ActiveResource更像ActiveRecord的,我怎么可以根据这个来缓存所有查询等什么真正的将是有益的。

I'm working through this and I'll find out what is going, but I'm curious if someone could tell me simpler terms what is going on, what I can do to smooth over caching in ActiveResource to be more like ActiveRecord, how I can tailor this to cache all queries, etc. Anything really would be helpful.

编辑:

我发现这一点: https://github.com/Ahsizara/cached_resource 这看起来很有希望,但它是新的(并且内置关闭上面的链接)......值得注意的是,它似乎并没有处理任何类型的集合,而是一个资源发现/缓存好。

I found this: https://github.com/Ahsizara/cached_resource which looks promising but it is new (and built off that link above)....notable is that it does not seem to handle any sort of collections, but for one resource finds/caches well.

推荐答案

既然你收到一个控制器为每个请求的新实例,缓存一样,是行不通的。我是presuming你有这样的事情:

Since you receive a new instance of a controller for each request, caching like that is not going to work. I'm presuming you have something like this:

def show
  @resource ||= expensive_request
end

什么是你期待 @resource 是该方法执行时?从A previous调用的东西显示的结果呢?不会发生。

What are you expecting @resource to be when that method executes? The result of something from a previous call to show? Not going to happen.

您需要做的是把东西放到 Rails.cache 如果你想让他们请求之间持续存在。如果使用的memcached作为一个后端,这可能是非常有效的。请参阅缓存使用Rails 各地的2.5节指南,了解更多信息。

What you need to do is put things into Rails.cache if you want them to persist between requests. If you use memcached as a back end, this can be very efficient. See the Caching with Rails guide for more information around section 2.5.

在一般情况下,比缓存的 || = 方法,更好的方法是定义被包裹着的 memoize的保存结果。例如:

In general, a better approach than the ||= method of caching is to define protected methods that are wrapped with memoize to save the results. For example:

def load_resource
  expensive_request
end
memoize :load_resource

请记住,这将仅适用于后续调用该方法同样要求的范围内,是不一样的使用 Rails.cache

这篇关于的ActiveResource +缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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