Class: ZkCache

Inherits:
Object
  • Object
show all
Defined in:
lib/caches/zk_cache.rb

Class Method Summary collapse

Class Method Details

.cache_exists?Boolean

initialize



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/caches/zk_cache.rb', line 23

def cache_exists?
#   @@client.read('test')
# rescue Exception => error
#   @@client = nil
  Net::HTTP.get(URI("http://#{@@config['host']}"))
rescue Errno::ECONNREFUSED => error
  puts "Could not connect to Zookeeper: #{error.message}"
  @@client = nil
rescue EOFError => error
  # do nothing
end

.clientObject



10
11
12
# File 'lib/caches/zk_cache.rb', line 10

def client
  @@client
end

.decode_data(data) ⇒ Object



82
83
84
85
86
# File 'lib/caches/zk_cache.rb', line 82

def decode_data(data)
  ActiveSupport::JSON.decode(data)
rescue => error
  data
end

.delete(key, options = {}) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/caches/zk_cache.rb', line 73

def delete(key, options = {})
  return unless client

  key = process_key(key)
  deleted = read(key)
  client.delete(path: key)
  deleted
end

.expires?(data) ⇒ Boolean



88
89
90
91
92
# File 'lib/caches/zk_cache.rb', line 88

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

.initializeObject



14
15
16
17
18
19
20
21
# File 'lib/caches/zk_cache.rb', line 14

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.message}")
  @@client = nil
end

.read(key, options = {}) ⇒ Object

Cache API, mimics ActiveSupport::Cache::Store api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/caches/zk_cache.rb', line 37

def read(key, options = {})
  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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/caches/zk_cache.rb', line 55

def write(key, value, options = {})
  return unless client

  key = process_key(key)
  init_key(key, nil)
  written_value = value
  if options[:expires_in].to_i > 0
    written_value = { zk_cache_data: { 
                                        value: value, 
                                        expires_in: (options[:expires_in].to_i),
                                        last_read: Time.now
                                      } 
                    }.to_json
  end

  client.set(path: key, data: written_value)
end