Skip to content

Commit

Permalink
Remove TranslationCacher
Browse files Browse the repository at this point in the history
  • Loading branch information
shioyama committed Nov 1, 2018
1 parent 38b6936 commit 047bb39
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 44 deletions.
19 changes: 18 additions & 1 deletion lib/mobility/backends/key_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,24 @@ def table_alias(attr, locale)
end

module Cache
include Plugins::Cache::TranslationCacher.new(:translation_for)
def translation_for(locale, **options)
return super(locale, options) if options.delete(:cache) == false
if cache.has_key?(locale)
cache[locale]
else
cache[locale] = super(locale, options)
end
end

private

def cache
@cache ||= {}
end

def clear_cache
@cache = {}
end
end
end
end
Expand Down
9 changes: 8 additions & 1 deletion lib/mobility/backends/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,14 @@ def table_alias(locale)
# Simple hash cache to memoize translations as a hash so they can be
# fetched quickly.
module Cache
include Plugins::Cache::TranslationCacher.new(:translation_for)
def translation_for(locale, **options)
return super(locale, options) if options.delete(:cache) == false
if cache.has_key?(locale)
cache[locale]
else
cache[locale] = super(locale, options)
end
end

private

Expand Down
20 changes: 18 additions & 2 deletions lib/mobility/plugins/cache.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# frozen_string_literal: true
require "mobility/plugins/cache/translation_cacher"

module Mobility
module Plugins
Expand Down Expand Up @@ -37,7 +36,14 @@ module BackendMethods
# @!macro backend_reader
# @!method read(locale, value, options = {})
# @option options [Boolean] cache *false* to disable cache.
include TranslationCacher.new(:read)
def read(locale, **options)
return super(locale, options) if options.delete(:cache) == false
if cache.has_key?(locale)
cache[locale]
else
cache[locale] = super(locale, options)
end
end

# @!macro backend_writer
# @option options [Boolean] cache
Expand All @@ -47,6 +53,16 @@ def write(locale, value, **options)
cache[locale] = super
end
# @!endgroup

private

def cache
@cache ||= {}
end

def clear_cache
@cache = {}
end
end
end
end
Expand Down
40 changes: 0 additions & 40 deletions lib/mobility/plugins/cache/translation_cacher.rb

This file was deleted.

0 comments on commit 047bb39

Please sign in to comment.