Module: Mobility::Backend::Cache

Defined in:
lib/mobility/backend/cache.rb

Overview

Caches values fetched from the backend so subsequent fetches can be performed more quickly.

By default, the cache stores cached values in a simple hash, returned from the #new_cache method added to the including backend instance class. To use a different cache class, simply define a new_cache method in the backend and return a new instance of the cache class (many backends do this, see KeyValue for one example.)

The cache is reset by the #clear_cache method, which by default simply assigns the result of #new_cache to the @cache instance variable. This behaviour can also be customized by defining a new_cache method on the backend class (see ActiveRecord::Table#new_cache for an example of a backend that does this).

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

Values are added to the cache in two ways:

  1. first read from backend

  2. any write to backend

The latter can be customized by defining the #write_to_cache? method, which by default returns false. If set to true, then writes will only update the cache and not hit the backend. This is a sensible setting in case the cache is actually an object which directly stores the translation (see one of the ORM-specific implementations of KeyValue for examples of this).

Defined Under Namespace

Modules: Setup

Backend Accessors collapse

Cache Methods collapse

Class Method Summary collapse

Class Method Details

.included(backend_class) ⇒ Object

Includes cache methods to backend (unless they are already defined) and extends backend class with Cache::Setup for backend resetting.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mobility/backend/cache.rb', line 85

def self.included(backend_class)
  backend_class.class_eval do
    extend Setup

    def new_cache
      {}
    end unless method_defined?(:new_cache)

    def write_to_cache?
      false
    end unless method_defined?(:write_to_cache?)

    def clear_cache
      @cache = new_cache
    end unless method_defined?(:clear_cache)
  end
end

Instance Method Details

#clear_cacheObject



1
2
3
# File 'lib/mobility/backend/cache.rb', line 1

def clear_cache
  @cache = new_cache
end

#new_cacheObject



1
2
3
# File 'lib/mobility/backend/cache.rb', line 1

def new_cache
  {}
end

#read(locale, **options) ⇒ Object

Returns Value of translation.

Parameters:

  • locale (Symbol)

    Locale to read

  • options (Hash)

Returns:

  • (Object)

    Value of translation



38
39
40
41
42
43
44
# File 'lib/mobility/backend/cache.rb', line 38

def read(locale, **options)
  if write_to_cache? || cache.has_key?(locale)
    cache[locale]
  else
    cache[locale] = super
  end
end

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

Returns Updated value.

Parameters:

  • locale (Symbol)

    Locale to write

  • value (Object)

    Value to write

  • options (Hash)

Returns:



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

def write(locale, value, **options)
  cache[locale] = write_to_cache? ? value : super
end

#write_to_cache?Boolean

Returns:

  • (Boolean)


1
2
3
# File 'lib/mobility/backend/cache.rb', line 1

def write_to_cache?
  false
end