Module: RailsNamedCache::RailsExt

Defined in:
lib/rails_named_cache/rails_ext.rb

Overview

Prepended onto the Rails singleton class so Rails.cache takes an optional store name.

This is the only monkey patch in the gem.

Rails.cache          # => the default Rails cache, untouched
Rails.cache(:memory) # => the store registered as :memory

Instance Method Summary collapse

Instance Method Details

#cache(*name) ⇒ ActiveSupport::Cache::Store

Splat rather than a nil default, so Rails.cache(name) with a nil name raises instead of quietly reading the default store.

Parameters:

  • name (Array<Symbol, String>)

    zero arguments, or one registered store name

Returns:

  • (ActiveSupport::Cache::Store)

Raises:



19
20
21
22
23
# File 'lib/rails_named_cache/rails_ext.rb', line 19

def cache(*name)
  return super() if name.empty?

  RailsNamedCache.fetch(*name)
end