Sha256: d7e7bad5c5da414dc8a1f304ff03bd9765c668a9c691d4cd9c1a55eb9bcefd2f

Contents?: true

Size: 1.67 KB

Versions: 20

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true
require "mobility/plugins/cache/translation_cacher"

module Mobility
  module Plugins
=begin

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 {Mobility::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

=end
    module Cache
      # Applies cache plugin to attributes.
      # @param [Attributes] attributes
      # @param [Boolean] option
      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

      # @group Backend Accessors
      #
      # @!macro backend_reader
      # @!method read(locale, value, options = {})
      #   @option options [Boolean] cache *false* to disable cache.
      include TranslationCacher.new(:read)

      # @!macro backend_writer
      # @option options [Boolean] cache
      #   *false* to disable cache.
      def write(locale, value, **options)
        return super if options.delete(:cache) == false
        cache[locale] = super
      end
      # @!endgroup
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
mobility-0.8.13 lib/mobility/plugins/cache.rb
mobility-0.8.11 lib/mobility/plugins/cache.rb
mobility-0.8.10 lib/mobility/plugins/cache.rb
mobility-0.8.9 lib/mobility/plugins/cache.rb
mobility-0.8.8 lib/mobility/plugins/cache.rb
mobility-0.8.7 lib/mobility/plugins/cache.rb
mobility-0.8.6 lib/mobility/plugins/cache.rb
mobility-0.8.5 lib/mobility/plugins/cache.rb
mobility-0.8.4 lib/mobility/plugins/cache.rb
mobility-0.8.3 lib/mobility/plugins/cache.rb
mobility-0.8.2 lib/mobility/plugins/cache.rb
mobility-0.8.1 lib/mobility/plugins/cache.rb
mobility-0.8.0 lib/mobility/plugins/cache.rb
mobility-0.7.6 lib/mobility/plugins/cache.rb
mobility-0.7.5 lib/mobility/plugins/cache.rb
mobility-0.7.4 lib/mobility/plugins/cache.rb
mobility-0.7.3 lib/mobility/plugins/cache.rb
mobility-0.7.2 lib/mobility/plugins/cache.rb
mobility-0.7.1 lib/mobility/plugins/cache.rb
mobility-0.7.0 lib/mobility/plugins/cache.rb