Home > OS >  Use ActiveSupport::Cache::FileStore directly in a model class
Use ActiveSupport::Cache::FileStore directly in a model class

Time:12-03

I tried to use ActiveSupport::Cache::FileStore dirctly in a model class:

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

The program reports a NameError

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

I added

require "active_support"
require "active_support/core_ext"

It still shows the error. I know that I can use Rails.Cache for the purpose, but the object I cached must be on the disk, which is different from other parts of the application.

The application is written in Rails 6.

CodePudding user response:

I tried this inside a rails 6 console and it works just fine.

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

It's just typo I mentioned in the comment. It's ::FileStore.new instead of ::FileCache.new

  • Related