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).

Examples:

Rails.cache.fetch("users") { User.all }          # default Rails cache
Rails.cache(:memory).fetch("countries") { ... }  # named store

Defined Under Namespace

Modules: Generators, RailsExt Classes: Configuration, ConfigurationError, Error, Railtie, Registry, UnknownStoreError

Constant Summary collapse

REGISTRY =

The process-wide registry.

Returns:

Registry.new
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.fetch(name) ⇒ ActiveSupport::Cache::Store

Parameters:

  • name (Symbol, String)

Returns:

  • (ActiveSupport::Cache::Store)

Raises:



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.

Parameters:

  • stores (Hash{Symbol=>Object}, nil)

    raw configuration value

Returns:

  • (Array<Symbol>)

    the names that were registered



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

.namesArray<Symbol>

Returns every registered name.

Returns:

  • (Array<Symbol>)

    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.

Parameters:

  • name (Symbol, String)

Returns:

  • (Symbol)

Raises:



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.

Parameters:

  • name (Symbol, String)
  • store (ActiveSupport::Cache::Store)

Returns:

  • (ActiveSupport::Cache::Store)


69
70
71
# File 'lib/rails_named_cache.rb', line 69

def register(name, store)
  registry.register(name, store)
end

.registered?(name) ⇒ Boolean

Parameters:

  • name (Symbol, String)

Returns:

  • (Boolean)


82
83
84
# File 'lib/rails_named_cache.rb', line 82

def registered?(name)
  registry.registered?(name)
end

.registryRegistry

Returns the process-wide registry.

Returns:

  • (Registry)

    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