Module: RailsNamedCache
- Defined in:
- lib/rails_named_cache.rb,
lib/rails_named_cache/logging.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, Logging, 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
77 78 79 |
# File 'lib/rails_named_cache.rb', line 77 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.
106 107 108 109 110 111 |
# File 'lib/rails_named_cache.rb', line 106 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.
96 97 98 99 100 |
# File 'lib/rails_named_cache.rb', line 96 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.
88 89 90 |
# File 'lib/rails_named_cache.rb', line 88 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.
129 130 131 132 133 134 135 136 |
# File 'lib/rails_named_cache.rb', line 129 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.
70 71 72 |
# File 'lib/rails_named_cache.rb', line 70 def register(name, store) registry.register(name, store) end |
.registered?(name) ⇒ Boolean
83 84 85 |
# File 'lib/rails_named_cache.rb', line 83 def registered?(name) registry.registered?(name) end |
.registry ⇒ Registry
Returns the process-wide registry.
61 62 63 |
# File 'lib/rails_named_cache.rb', line 61 def registry REGISTRY end |
.reset! ⇒ void
This method returns an undefined value.
Empties the registry. Intended for tests.
116 117 118 |
# File 'lib/rails_named_cache.rb', line 116 def reset! registry.clear end |