Class: CacheToolbox::FixedPrefix
- Inherits:
-
ActiveSupport::Cache::Store
- Object
- ActiveSupport::Cache::Store
- CacheToolbox::FixedPrefix
- 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
-
#initialize(options = {}) ⇒ FixedPrefix
constructor
A new instance of FixedPrefix.
Constructor Details
#initialize(options = {}) ⇒ FixedPrefix
Returns a new instance of FixedPrefix.
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/cache_toolbox/fixed_prefix.rb', line 22 def initialize( = {}) super() raise ArgumentError, 'No cache store option given.' if [:store].nil? @store = ::ActiveSupport::Cache.lookup_store([:store]) raise ArgumentError, 'No key prefix option given.' if [:prefix].nil? || [:prefix].to_s.empty? @prefix = [:prefix].to_s + '-' end |