Class: CacheToolbox::FixedPrefix

Inherits:
ActiveSupport::Cache::Store
  • Object
show all
Defined in:
lib/cache_toolbox/fixed_prefix.rb

Overview

A utility cache store which writes everything into a parent cache with a fixed key prefix. Aimed to help implementing key-based expiration.

Example use case: flushable http client cache

client = Faraday.new do |builder|
  cache = CacheToolbox::FixedPrefix.new(store: Rails.cache, prefix: ENV['CACHE_PR'])
  builder.use :http_cache, store: cache
  builder.adapter Faraday.default_adapter
end

If the app runs on Heroku then cache could be instantly flushed by running:

heroku config:set CACHE_PR=<..random prefix..>

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ FixedPrefix

Returns a new instance of FixedPrefix.

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cache_toolbox/fixed_prefix.rb', line 22

def initialize(options = {})
  super(options)

  raise ArgumentError, 'No cache store option given.' if
    options[:store].nil?

  @store = ::ActiveSupport::Cache.lookup_store(options[:store])

  raise ArgumentError, 'No key prefix option given.' if
    options[:prefix].nil? || options[:prefix].to_s.empty?

  @prefix = options[:prefix].to_s + '-'
end