English 中文(简体)
请解释在这种情况下铁路海滩的行为。
原标题:Please explain the behaviour of Rails s cache in this case

Imagine I有5个标签:tag1,tag2...tag5。 如果是:

Rails.cache.fetch("all.tags") { Tag.all }

之后 页: 1 如果我增加另一个标签,而且我试图再次从海滩上挑出来,新标签也装上了。 为什么如此?

EDIT:我的实际法典如下:

Rails.cache.fetch("autocomplete.#{term}") { puts "Cache miss #{term}"; Tag.starting_with(term) }

当<条码>起步时,请从某些信件中找到标记。 这里,我的行为是:

1.9.3p125 :046 > Rails.cache.read("autocomplete.ta")
  Tag Load (1.0ms)  SELECT "tags".* FROM "tags" WHERE (name like  ta% )
 => [#<Tag id: 10, name: "tag1">, #<Tag id: 11, name: "tag2">, #<Tag id: 12, name: "tag3">, #<Tag id: 13, name: "tag4">]
1.9.3p125 :048 > Tag.create(name:"tag5")
   (0.2ms)  begin transaction
  SQL (1.0ms)  INSERT INTO "tags" ("name") VALUES (?)  [["name", "tag5"]]
   (150.9ms)  commit transaction
 => #<Tag id: 14, name: "tag5"> 
1.9.3p125 :049 > Rails.cache.read("autocomplete.ta")
  Tag Load (0.8ms)  SELECT "tags".* FROM "tags" WHERE (name like  ta% )
 => [#<Tag id: 10, name: "tag1">, #<Tag id: 11, name: "tag2">, #<Tag id: 12, name: "tag3">, #<Tag id: 13, name: "tag4">, #<Tag id: 14, name: "tag5">] 
最佳回答

您没有显示<代码>Tag.starting_with_term的代码,但我没有好钱说它具有范围,或回到像这样的范围,就象这样的东西。

Tag.where(...)

This is fundamentally different from the Tag.all i your initial question: Tag.all is an array but the above is a scope. Scopes are evaluated lazily, the rows are only requested from the database when you call a method on the scope that requires the scope be an actual array.

这里发生的情况是,你正在缩小实际范围,而不是范围所选择的行文。 审视这个问题的另一个途径是说,目前你正在抓住构成问题的条件,而不是问答结果。

当你从海滩上读到时,从海滩上取回你的范围,而试图显示它的行动则迫使铁路进行评价。 这一评价是在从海滩上检索范围之后进行的,因此,你总是取得新的成果。

The simplest thing is to force evaluation of the scope, ie something like

Rails.cache.fetch( cache_key ) { Tag.starting_with(term).all }
问题回答

这是不可能的。

Cache在钥匙/价值上工作。

Rails.cache.fetch("all.tags") { Tag.all }

它将永远收回储存的关键“所有标签”的价值。

您可以读到存放在藏匿处的价值。 然后添加新的标签

Rails.cache.read("all.tags")

我不了解你的回答,但我也很想知道为什么会发生。

Are you doing any thing else with cache setting ?





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签