Module: Mobility::Plugins::Cache

Defined in:
lib/mobility/plugins/cache.rb,
lib/mobility/plugins/cache/translation_cacher.rb

Overview

Caches values fetched from the backend so subsequent fetches can be performed more quickly. The cache stores cached values in a simple hash, which is not optimal for some storage strategies, so some backends (KeyValue, Table) use a custom module through the Backend::Setup#apply_plugin hook. For details see the documentation for these backends.

The cache is reset when one of a set of events happens (saving, reloading, etc.). See BackendResetter for details.

Values are added to the cache in two ways:

  1. first read from backend

  2. any write to backend

Defined Under Namespace

Classes: TranslationCacher

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.apply(attributes, option) ⇒ Object

Applies cache plugin to attributes.

Parameters:


27
28
29
30
31
32
33
34
35
# File 'lib/mobility/plugins/cache.rb', line 27

def self.apply(attributes, option)
  if option
    backend_class = attributes.backend_class
    backend_class.include(self) unless backend_class.apply_plugin(:cache)

    model_class = attributes.model_class
    model_class.include BackendResetter.for(model_class).new(attributes.names) { clear_cache }
  end
end

Instance Method Details

#write(locale, value, **options) ⇒ Object

Updates translation for provided locale without calling backend's methods to persist the changes.

Parameters:

  • locale (Symbol)

    Locale to write

  • value (Object)

    Value to write

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • cache (Boolean)

    false to disable cache.

Returns:

  • (Object)

    Updated value


47
48
49
50
# File 'lib/mobility/plugins/cache.rb', line 47

def write(locale, value, **options)
  return super if options.delete(:cache) == false
  cache[locale] = super
end