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
-
#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.
-
#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 |
# 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] @locales = Array([:locales]).map(&:to_s) # mutable states @blurbs = {} @queued = {} @started = false @downloaded = false end |
Instance Attribute Details
#last_downloaded_at ⇒ Object (readonly)
Returns the value of attribute last_downloaded_at.
130 131 132 |
# File 'lib/copy_tuner_client/cache.rb', line 130 def last_downloaded_at @last_downloaded_at end |
#last_uploaded_at ⇒ Object (readonly)
Returns the value of attribute last_uploaded_at.
130 131 132 |
# File 'lib/copy_tuner_client/cache.rb', line 130 def last_uploaded_at @last_uploaded_at end |
#queued ⇒ Object (readonly)
Returns the value of attribute queued.
130 131 132 |
# File 'lib/copy_tuner_client/cache.rb', line 130 def queued @queued end |
Instance Method Details
#[](key) ⇒ String
Returns content for the given blurb.
31 32 33 |
# File 'lib/copy_tuner_client/cache.rb', line 31 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.
39 40 41 42 43 |
# File 'lib/copy_tuner_client/cache.rb', line 39 def []=(key, value) return if key =~ @exclude_key_regexp return if @locales.present? && !@locales.member?(key.split('.').first) lock { @queued[key] = value } end |
#download ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/copy_tuner_client/cache.rb', line 107 def download @started = true res = client.download do |downloaded_blurbs| downloaded_blurbs.reject! { |key, value| value == '' } lock { @blurbs = downloaded_blurbs } end @last_downloaded_at = Time.now.utc res rescue ConnectionError => error logger.error error. ensure @downloaded = true end |
#export ⇒ String
Yaml representation of all blurbs
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/copy_tuner_client/cache.rb', line 53 def export keys = {} lock do @blurbs.sort.each do |(blurb_key, value)| current = keys yaml_keys = blurb_key.split('.') 0.upto(yaml_keys.size - 2) do |i| key = yaml_keys[i] # Overwrite en.key with en.sub.key unless current[key].class == Hash current[key] = {} end current = current[key] end current[yaml_keys.last] = value end end unless keys.size < 1 keys.to_yaml end end |
#flush ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/copy_tuner_client/cache.rb', line 95 def flush res = with_queued_changes do |queued| client.upload queued end @last_uploaded_at = Time.now.utc res rescue ConnectionError => error logger.error error. end |
#inspect ⇒ Object
132 133 134 |
# File 'lib/copy_tuner_client/cache.rb', line 132 def inspect "#<CopyTunerClient::Cache:#{object_id}>" end |
#keys ⇒ Array<String>
Keys for all blurbs stored on the server.
47 48 49 |
# File 'lib/copy_tuner_client/cache.rb', line 47 def keys lock { @blurbs.keys } end |
#sync ⇒ Object
Downloads and then flushes
125 126 127 128 |
# File 'lib/copy_tuner_client/cache.rb', line 125 def sync download flush end |
#wait_for_download ⇒ Object
Waits until the first download has finished.
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/copy_tuner_client/cache.rb', line 81 def wait_for_download if pending? logger.info 'Waiting for first download' if logger.respond_to? :flush logger.flush end while pending? sleep 0.1 end end end |