Class: CopyTunerClient::Cache
- Inherits:
-
Object
- Object
- CopyTunerClient::Cache
- Defined in:
- lib/copy_tuner_client/cache.rb
Overview
Manages synchronization of copy between I18nBackend and Client. Acts like a Hash. Applications using the client will not need to interact with this class directly.
Responsible for locking down access to data used by both threads.
Instance Attribute Summary collapse
-
#blurbs ⇒ Object
readonly
Returns the value of attribute blurbs.
-
#last_downloaded_at ⇒ Object
readonly
Returns the value of attribute last_downloaded_at.
-
#last_uploaded_at ⇒ Object
readonly
Returns the value of attribute last_uploaded_at.
-
#queued ⇒ Object
readonly
Returns the value of attribute queued.
Instance Method Summary collapse
-
#[](key) ⇒ String
Returns content for the given blurb.
-
#[]=(key, value) ⇒ Object
Sets content for the given blurb.
- #download ⇒ Object
-
#export ⇒ String
Yaml representation of all blurbs.
- #flush ⇒ Object
-
#initialize(client, options) ⇒ Cache
constructor
Usually instantiated when CopyTunerClient::Configuration#apply is invoked.
- #inspect ⇒ Object
-
#keys ⇒ Array<String>
Keys for all blurbs stored on the server.
- #pending? ⇒ Boolean
-
#sync ⇒ Object
Downloads and then flushes.
-
#wait_for_download ⇒ Object
Waits until the first download has finished.
Constructor Details
#initialize(client, options) ⇒ Cache
Usually instantiated when CopyTunerClient::Configuration#apply is invoked.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/copy_tuner_client/cache.rb', line 15 def initialize(client, ) @client = client @logger = [:logger] @mutex = Mutex.new @exclude_key_regexp = [:exclude_key_regexp] @upload_disabled = [:upload_disabled] @locales = Array([:locales]).map(&:to_s) # mutable states @blurbs = {} @blank_keys = Set.new @queued = {} @started = false @downloaded = false end |
Instance Attribute Details
#blurbs ⇒ Object (readonly)
Returns the value of attribute blurbs.
117 118 119 |
# File 'lib/copy_tuner_client/cache.rb', line 117 def blurbs @blurbs end |
#last_downloaded_at ⇒ Object (readonly)
Returns the value of attribute last_downloaded_at.
117 118 119 |
# File 'lib/copy_tuner_client/cache.rb', line 117 def last_downloaded_at @last_downloaded_at end |
#last_uploaded_at ⇒ Object (readonly)
Returns the value of attribute last_uploaded_at.
117 118 119 |
# File 'lib/copy_tuner_client/cache.rb', line 117 def last_uploaded_at @last_uploaded_at end |
#queued ⇒ Object (readonly)
Returns the value of attribute queued.
117 118 119 |
# File 'lib/copy_tuner_client/cache.rb', line 117 def queued @queued end |
Instance Method Details
#[](key) ⇒ String
Returns content for the given blurb.
33 34 35 |
# File 'lib/copy_tuner_client/cache.rb', line 33 def [](key) lock { @blurbs[key] } end |
#[]=(key, value) ⇒ Object
Sets content for the given blurb. The content will be pushed to the server on the next flush.
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/copy_tuner_client/cache.rb', line 41 def []=(key, value) return if @exclude_key_regexp && key.match?(@exclude_key_regexp) return unless key.include?('.') return if @locales.present? && !@locales.member?(key.split('.').first) return if @upload_disabled lock do return if @blank_keys.member?(key) || @blurbs.key?(key) @queued[key] = value end end |
#download ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/copy_tuner_client/cache.rb', line 91 def download @started = true res = client.download do |downloaded_blurbs| blank_blurbs, blurbs = downloaded_blurbs.partition { |_key, value| value == '' } lock do @blank_keys = Set.new(blank_blurbs.to_h.keys) @blurbs = blurbs.to_h end end @last_downloaded_at = Time.now.utc res rescue ConnectionError => e logger.error e. ensure @downloaded = true end |
#export ⇒ String
Yaml representation of all blurbs
62 63 64 |
# File 'lib/copy_tuner_client/cache.rb', line 62 def export lock { @blurbs.present? ? DottedHash.to_h(@blurbs).to_yaml : nil } end |
#flush ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/copy_tuner_client/cache.rb', line 79 def flush res = with_queued_changes do |queued| client.upload queued end @last_uploaded_at = Time.now.utc res rescue ConnectionError => e logger.error e. end |
#inspect ⇒ Object
119 120 121 |
# File 'lib/copy_tuner_client/cache.rb', line 119 def inspect "#<CopyTunerClient::Cache:#{object_id}>" end |
#keys ⇒ Array<String>
Keys for all blurbs stored on the server.
56 57 58 |
# File 'lib/copy_tuner_client/cache.rb', line 56 def keys lock { @blurbs.keys } end |
#pending? ⇒ Boolean
123 124 125 |
# File 'lib/copy_tuner_client/cache.rb', line 123 def pending? @started && !@downloaded end |
#sync ⇒ Object
Downloads and then flushes
112 113 114 115 |
# File 'lib/copy_tuner_client/cache.rb', line 112 def sync download flush end |
#wait_for_download ⇒ Object
Waits until the first download has finished.
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/copy_tuner_client/cache.rb', line 67 def wait_for_download return unless pending? logger.info 'Waiting for first download' if logger.respond_to? :flush logger.flush end sleep 0.1 while pending? end |