Rails 5.2.8使用dalli_store缓存适配器报错且修改代码无效
解决Dalli缓存适配器找不到的问题
使用的技术版本
- rails 5.2.8
- ruby 2.3.1
- dalli 3.0.4
出现的错误
cache.rb:110:in `rescue in retrieve_store_class': Could not find cache store adapter for dalli_store (cannot load such file -- active_support/cache/dalli_store) (RuntimeError)
相关原代码
def retrieve_store_class(store) # require_relative cannot be used here because the class might be # provided by another gem, like redis-activesupport for example. require "active_support/cache/#{store}" rescue LoadError => e raise "Could not find cache store adapter for #{store} (#{e})" else ActiveSupport::Cache.const_get(store.to_s.camelize) end
尝试过的修改(无效)
def retrieve_store_class(store) # require_relative cannot be used here because the class might be # provided by another gem, like redis-activesupport for example. begin #added begin statement after getting error require "active_support/cache/#{store}" rescue LoadError => e raise "Could not find cache store adapter for #{store} (#{e})" else ActiveSupport::Cache.const_get(store.to_s.camelize) end end
解决方法
方案1:降级Dalli版本
Dalli 3.x系列移除了对Rails 5.2及以下版本的适配支持,不再自带active_support/cache/dalli_store文件。修改Gemfile指定兼容的2.x版本:
gem 'dalli', '~> 2.7.11'
执行命令更新依赖:
bundle install
该版本可与Rails 5.2.8完美配合,无需额外配置即可找到缓存适配器。
方案2:安装独立适配gem
若不想降级Dalli,可安装专门为低版本Rails提供适配的active_support-cache-dalli gem。修改Gemfile:
gem 'dalli', '3.0.4' gem 'active_support-cache-dalli'
执行bundle install后,Rails就能正常加载Dalli缓存适配器。
关键提醒
你修改的begin语句无法解决问题——错误根源是缺少适配器文件,而非代码结构问题,不要改动Rails内部的retrieve_store_class方法。
内容的提问来源于stack exchange,提问作者vikasrb
相关产品推荐
相关产品推荐

