Class: RdCache

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

Class Method Summary collapse

Class Method Details

.cache_exists?Boolean

initialize

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
# File 'lib/caches/rd_cache.rb', line 20

def cache_exists?
  Net::HTTP.get(URI("http://#{@@config['host']}"))
rescue Errno::ECONNREFUSED => error
  puts "**** Error: #{error.message}"
  @@client = nil
rescue Net::HTTPBadResponse => error
  # do nothing
end

.clientObject



7
8
9
# File 'lib/caches/rd_cache.rb', line 7

def client
  @@client
end

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



42
43
44
45
46
47
# File 'lib/caches/rd_cache.rb', line 42

def delete(key, options = {})
  return unless client
  deleted = read(key)
  client.del(key)
  deleted
end

.initializeObject



11
12
13
14
15
16
17
18
# File 'lib/caches/rd_cache.rb', line 11

def initialize
  @@config ||= ConfigService.load_config('redis.yml')[ConfigService.environment]
  @@client ||= Redis.new(url: "redis://#{@@config['host']}")
  cache_exists?
rescue Exception => error
  puts("RdCache.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



31
32
33
34
# File 'lib/caches/rd_cache.rb', line 31

def read(key, options = {})
  return unless client
  client.get(key)
end

.write(key, value, options = {}) ⇒ Object



36
37
38
39
40
# File 'lib/caches/rd_cache.rb', line 36

def write(key, value, options = {})
  return unless client
  client.set(key, value)
  client.expire(key, options[:expires_in]) if  options[:expires_in]
end