ActiveSupport::Cache::DatabaseStore

Build Status License

ActiveSupport::Cache::Store implementation backed by a database via ActiveRecord.

Tested with:

  • PostgreSQL
  • SQlite3
  • MySQL/MariaDB

Usage

Include the data migration:

# db/migrate/20190908102030_create_activesupport_cache_entries.rb
require 'active_support/cache/database_store/migration'

class CreateActivesupportCacheEntries < ActiveRecord::Migration[5.2]
  def up
    ActiveSupport::Cache::DatabaseStore::Migration.migrate(:up)
  end

  def down
    ActiveSupport::Cache::DatabaseStore::Migration.migrate(:down)
  end
end

Open and use the new cache instance:

cache = ActiveSupport::Cache::DatabaseStore.new namespace: 'my-scope'
value = cache.fetch('some-key') { 'default' }

To use as a Rails cache store (not recommended!), simply use a new instance.

config.cache_store = ActiveSupport::Cache::DatabaseStore.new