Class: McCache

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

Class Method Summary collapse

Class Method Details

.cache_exists?Boolean

initialize

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
# File 'lib/caches/mc_cache.rb', line 18

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.message}"
  @@client = nil
rescue Net::HTTPBadResponse => error
  # do nothing
end

.clientObject



5
6
7
# File 'lib/caches/mc_cache.rb', line 5

def client
  @@client
end

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



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

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

.initializeObject



9
10
11
12
13
14
15
16
# File 'lib/caches/mc_cache.rb', line 9

def initialize
  @@config ||= ConfigService.load_config('memcached.yml')[ConfigService.environment]
  @@client ||= Dalli::Client.new(@@config['host'], @@config)
  cache_exists?
rescue Exception => error
  puts("McCache.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



32
33
34
35
# File 'lib/caches/mc_cache.rb', line 32

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

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



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

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