我正在尝试在DigitalOcean上的Ubuntu 14.04计算机上实现缓存,我不确定我是否正确设置了这个。我在Rails控制台中,我看到了这个:
[2] pry(#<DigitizationsController>)> Rails.cache.write("hi", 1)
Cache write: hi
=> 288230376151711744
[3] pry(#<DigitizationsController>)> Rails.cache.read("hi")
Cache read: hi
=> 1
[4] pry(#<DigitizationsController>)> Rails.cache.class
=> ActiveSupport::Cache::DalliStore
那似乎没问题?
但在我的实际代码中,数据库查询似乎仍然存在。我的代码:
@search_tag_suggestions = Rails.cache.fetch('digitizations_index', expires_in: 1.hour) do
[Submission.tag_counts.where('taggings_count > 5').limit(5).order('RAND()')
end
和我的终端输出似乎是从缓存中获取的,但为什么查询会在之后触发呢?
Rails.cache.read('digitizations_index')
Cache read: digitizations_index
ActsAsTaggableOn::Tag Load (0.6ms) SELECT tags.*, taggings.tags_count AS count FROM `tags` JOIN (SELECT taggings.tag_id, COUNT(taggings.tag_id) AS tags_count FROM `taggings` INNER JOIN submissions ON submissions.id = taggings.taggable_id WHERE (taggings.taggable_type = 'Submission' AND taggings.context = 'tags') AND (taggings.taggable_id IN (NULL)) GROUP BY taggings.tag_id HAVING COUNT(taggings.tag_id) > 0) AS taggings ON taggings.tag_id = tags.id WHERE (taggings_count > 5) ORDER BY RAND() LIMIT 5
为什么数据库会在触及缓存时触发?是因为我只是缓存查询计划,当我实际调用变量或者在调用to_s
的控制台中调用查询计划时它会触发吗?
我的环境有这条线:
config.cache_store = :dalli_store
在开发中,我也看到了这个错误。谁知道为什么?我的猜测是,在开发中我不急于加载?
Rails.cache.read('digitizations_index')
Cache read: digitizations_index
Unexpected exception during Dalli request: NameError: uninitialized constant ActsAsTaggableOn::Taggable::Collection