【问题标题】:Use ActiveSupport::Cache::FileStore directly in a model class直接在模型类中使用 ActiveSupport::Cache::FileStore
【发布时间】:2021-12-02 23:08:25
【问题描述】:

我尝试在模型类中直接使用 ActiveSupport::Cache::FileStore:

@backend_cache = ActiveSupport::Cache::FileCache.new Rails.root.join("tmp", "cache")

程序报告一个 NameError

NameError: uninitialized constant ActiveSupport::Cache::FileCache
Did you mean?  FileTest

我加了

require "active_support"
require "active_support/core_ext"

它仍然显示错误。我知道我可以使用 Rails.Cache 达到目的,但是我缓存的对象必须在磁盘上,这与应用程序的其他部分不同。

应用程序是用 Rails 6 编写的。

【问题讨论】:

  • 我认为你有一个错字:::FileStore.new 而不是 FileCache

标签: ruby-on-rails


【解决方案1】:

我在 Rails 6 控制台中尝试过,效果很好。

@backend_cache = ActiveSupport::Cache::FileStore.new(Rails.root.join("tmp", "cache"))
@backend_cache.write('x', 1) # => true
@backend_cache.read('x') # => 1

这只是我在评论中提到的错字。是::FileStore.new 而不是::FileCache.new

【讨论】:

    猜你喜欢
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 2023-01-20
    相关资源
    最近更新 更多