Class: ZkCache
- Inherits:
-
Object
- Object
- ZkCache
- Defined in:
- lib/caches/zk_cache.rb
Class Method Summary collapse
-
.cache_exists? ⇒ Boolean
initialize.
- .client ⇒ Object
- .decode_data(data) ⇒ Object
- .delete(key, options = {}) ⇒ Object
- .expires?(data) ⇒ Boolean
- .initialize ⇒ Object
-
.read(key, options = {}) ⇒ Object
Cache API, mimics ActiveSupport::Cache::Store api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html.
- .write(key, value, options = {}) ⇒ Object
Class Method Details
.cache_exists? ⇒ Boolean
initialize
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/caches/zk_cache.rb', line 21 def cache_exists? # @@client.read('test') # rescue Exception => error # @@client = nil Net::HTTP.get(URI("http://#{@@config['host']}")) rescue Errno::ECONNREFUSED => error puts "**** Error: #{error.}" @@client = nil rescue EOFError => error # do nothing end |
.client ⇒ Object
8 9 10 |
# File 'lib/caches/zk_cache.rb', line 8 def client @@client end |
.decode_data(data) ⇒ Object
80 81 82 83 84 |
# File 'lib/caches/zk_cache.rb', line 80 def decode_data(data) ActiveSupport::JSON.decode(data) rescue => error data end |
.delete(key, options = {}) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/caches/zk_cache.rb', line 71 def delete(key, = {}) return unless client key = process_key(key) deleted = read(key) client.delete(path: key) deleted end |
.expires?(data) ⇒ Boolean
86 87 88 89 90 |
# File 'lib/caches/zk_cache.rb', line 86 def expires?(data) return true if (data.nil? || !data.is_a?(Hash)) data = data.with_indifferent_access return DateTime.parse(data[:last_read]).to_i + data[:expires_in].to_i < Time.now.to_i end |
.initialize ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/caches/zk_cache.rb', line 12 def initialize @@config ||= ConfigService.load_config('zookeeper.yml')[ConfigService.environment] @@client ||= Zookeeper.new(@@config['host']) cache_exists? rescue Exception => error puts("ZkCache.initialize error: #{error.}") @@client = nil end |
.read(key, options = {}) ⇒ Object
Cache API, mimics ActiveSupport::Cache::Store api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/caches/zk_cache.rb', line 35 def read(key, = {}) return unless client key = process_key(key) result = client.get(path: key)[:data] result = decode_data(result) return result unless result.is_a?(Hash) sym_data = result.with_indifferent_access return sym_data unless sym_data[:zk_cache_data].present? sym_data = sym_data[:zk_cache_data] return nil if expires?(sym_data) sym_data[:value] end |
.write(key, value, options = {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/caches/zk_cache.rb', line 53 def write(key, value, = {}) return unless client key = process_key(key) init_key(key, nil) written_value = value if [:expires_in].to_i > 0 written_value = { zk_cache_data: { value: value, expires_in: ([:expires_in].to_i), last_read: Time.now } }.to_json end client.set(path: key, data: written_value) end |