Module: RailsNamedCache
- Defined in:
- lib/rails_named_cache.rb,
lib/rails_named_cache/railtie.rb,
lib/rails_named_cache/version.rb,
lib/rails_named_cache/registry.rb,
lib/rails_named_cache/rails_ext.rb,
lib/rails_named_cache/configuration.rb,
lib/generators/rails_named_cache/install/install_generator.rb
Overview
Named ActiveSupport::Cache::Store instances for Rails applications.
Register stores through config.named_cache_stores and read them back with
Rails.cache(:name).
Defined Under Namespace
Modules: Generators, RailsExt Classes: Configuration, ConfigurationError, Error, Railtie, Registry, UnknownStoreError
Constant Summary collapse
Class Method Summary collapse
- .fetch(name) ⇒ ActiveSupport::Cache::Store
-
.install! ⇒ void
Teaches
Rails.cacheto accept an optional name. -
.load_configuration(stores) ⇒ Array<Symbol>
Registers every store described by
config.named_cache_stores. -
.names ⇒ Array<Symbol>
Every registered name.
-
.normalize_name(name) ⇒ Symbol
private
Coerces a store name to a Symbol.
-
.register(name, store) ⇒ ActiveSupport::Cache::Store
Registers a cache store under
name. - .registered?(name) ⇒ Boolean
-
.registry ⇒ Registry
The process-wide registry.
-
.reset! ⇒ void
Empties the registry.
Class Method Details
.fetch(name) ⇒ ActiveSupport::Cache::Store
76 77 78 |
# File 'lib/rails_named_cache.rb', line 76 def fetch(name) registry.fetch(name) end |
.install! ⇒ void
This method returns an undefined value.
Teaches Rails.cache to accept an optional name. Idempotent, and a no-op
when the gem is used outside Rails.
105 106 107 108 109 110 |
# File 'lib/rails_named_cache.rb', line 105 def install! return if !defined?(::Rails) || ::Rails.singleton_class.include?(RailsExt) ::Rails.singleton_class.prepend(RailsExt) nil end |
.load_configuration(stores) ⇒ Array<Symbol>
Registers every store described by config.named_cache_stores.
95 96 97 98 99 |
# File 'lib/rails_named_cache.rb', line 95 def load_configuration(stores) Configuration.new(stores).resolved_stores.each do |name, store| register(name, store) end.keys end |
.names ⇒ Array<Symbol>
Returns every registered name.
87 88 89 |
# File 'lib/rails_named_cache.rb', line 87 def names registry.names end |
.normalize_name(name) ⇒ Symbol
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Coerces a store name to a Symbol.
Shared by Registry and Configuration so a bad name fails the same way whether it came from configuration or from a direct call.
128 129 130 131 132 133 134 135 |
# File 'lib/rails_named_cache.rb', line 128 def normalize_name(name) unless (name.is_a?(Symbol) || name.is_a?(String)) && !name.to_s.empty? raise ConfigurationError, "Cache store name must be a non-empty Symbol or String, got #{name.inspect}" end name.to_sym end |
.register(name, store) ⇒ ActiveSupport::Cache::Store
Registers a cache store under name.
69 70 71 |
# File 'lib/rails_named_cache.rb', line 69 def register(name, store) registry.register(name, store) end |
.registered?(name) ⇒ Boolean
82 83 84 |
# File 'lib/rails_named_cache.rb', line 82 def registered?(name) registry.registered?(name) end |
.registry ⇒ Registry
Returns the process-wide registry.
60 61 62 |
# File 'lib/rails_named_cache.rb', line 60 def registry REGISTRY end |
.reset! ⇒ void
This method returns an undefined value.
Empties the registry. Intended for tests.
115 116 117 |
# File 'lib/rails_named_cache.rb', line 115 def reset! registry.clear end |