Skip to content

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

License

Notifications You must be signed in to change notification settings

Jay-Schneider/activesupport-cache-database

 
 

Repository files navigation

ActiveSupport::Cache::DatabaseStore

Test 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, simply use a new instance. Please keep in mind that, for performance reasons, your database may not be the most suitable general purpose cache backend.

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

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%