我正在使用 Dalli 和 Memcachier 在 Heroku 上实现缓存,当我获得缓存命中时,它会将 Symbol 添加到返回的数组(或哈希,在另一个示例中)。这个符号 :@new_record 与我返回的结果没有任何关系,它被插入到不同类型的缓存提取中,我无法在 SO/Google 上找到其他人解决这个问题. 代码库范围的查找不返回“new_record”的结果。
这种添加发生在我尝试添加缓存的两个地方。我不知道如何首先防止它发生,但我通过检查返回集中每条记录的类来躲避它在这个特定用途中的影响,如果它不是供应商,则不呈现供应商的个人资料。这是一个丑陋的解决方案,我想从源头解决问题。
如果您有任何其他问题/需要我发布更多信息,请告诉我,感谢您的帮助。
我正在使用缓存的方法:
#this will be slow, need to store it somewhere
def self.set_for_index(index_name)
guide = INDEX_HOLDER[index_name]
return false if guide.nil?
haves, have_nots, countries = guide[0], guide[1], guide[2]
holder = []
supplier_set = Rails.cache.fetch("set_for_index_#{index_name}", :expires_in => 24.hours) {
Supplier.find_each do |s|
if (
s.profile_visible and
(countries == [] or (s.address and countries.include?(s.address.country))) and
!(haves.map{ |h| s.has_tag?(Tag.find_by_name(h).id) }.include?(false)) and
!(have_nots.map{ |h| s.has_tag?(Tag.find_by_name(h).id) }.include?(true))
)
holder << s
end
end
holder
}
return supplier_set
end
我现在正在使用此代码的最后一行筛选出 :@new_record:
def self.visible_profiles_sorted(index_name)
profiles = Supplier.set_for_index(index_name)
answer = {}
if !(profiles.nil? or profiles == [])
profiles.each do |s|
#block odd error where cache appends a :@new_record after the last result
next if !s.is_a?(Supplier) or s.address.nil? or s.address.country.nil?
...
错误,如果我不筛选出@new_record [这不是从上面的代码中删除行,而是从另一个地方我尝试使用基于类的测试不可行的缓存]:
2013-08-28T20:50:00.446550+00:00 heroku[web.1]: State changed from starting to up
2013-08-28T20:50:06.774001+00:00 app[web.1]: Dalli/SASL authenticating as af5c2c
2013-08-28T20:50:06.777925+00:00 app[web.1]: Dalli/SASL: af5c2c
2013-08-28T20:50:06.807129+00:00 app[web.1]: Started GET "/suppliers" for 70.36.146.74 at 2013-08-28 20:50:06 +0000
2013-08-28T20:50:10.671553+00:00 app[web.1]:
2013-08-28T20:50:10.671553+00:00 app[web.1]: 37: <td>
2013-08-28T20:50:10.671553+00:00 app[web.1]: ActionView::Template::Error (undefined method `name' for :@new_record:Symbol):
2013-08-28T20:50:10.671553+00:00 app[web.1]: 39: <%= link_to s.name, supplier_profile_path(s.name_for_link) %>
2013-08-28T20:50:10.671553+00:00 app[web.1]: 40: <%= image_tag
application.rb 的相关位:
module Partreach
class Application < Rails::Application
config.cache_store = :dalli_store
production.rb 的相关位:
Partreach::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
Gemfile 的相关位:
gem 'dalli', '2.6.4' #https://devcenter.heroku.com/articles/building-a-rails-3-application-with-memcache
gem 'memcachier', '0.0.2' #https://devcenter.heroku.com/articles/building-a-rails-3-application-with-memcache