Class: Memory

Inherits:
Object show all
Defined in:
lib/handset_detection/cache/memory.rb

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Memory

Construct a new Memory cache object.

param string dir



37
38
39
40
# File 'lib/handset_detection/cache/memory.rb', line 37

def initialize(config={})
  @thread_safe = (config.include?('cache') and config['cache'].include?('memory') and config['cache']['memory']['thread_safe']) ? true : false
  make_cache unless defined?(@@c)
end

Instance Method Details

#del(key) ⇒ Object

Delete key



59
60
61
62
# File 'lib/handset_detection/cache/memory.rb', line 59

def del(key)
  @@c.delete key
  true
end

#flushObject

Flush cache



65
66
67
68
# File 'lib/handset_detection/cache/memory.rb', line 65

def flush
  make_cache
  true
end

#get(key) ⇒ Object

Get key



44
45
46
47
48
# File 'lib/handset_detection/cache/memory.rb', line 44

def get(key)
  data, exp = @@c[key]
  return nil if exp.nil? or Time.now.to_i > exp
  data 
end

#make_cacheObject

Initialize the cache data structure



72
73
74
75
76
77
78
79
80
# File 'lib/handset_detection/cache/memory.rb', line 72

def make_cache
  if !@thread_safe 
    @@c = {}
  elsif defined?(ThreadSafe)
    @@c = ThreadSafe::Hash.new
  else
    raise 'ThreadSafe is required to use the Memory cache.'
  end    
end

#set(key, data, ttl) ⇒ Object

Set key



52
53
54
55
# File 'lib/handset_detection/cache/memory.rb', line 52

def set(key, data, ttl)
  @@c[key] = [data, Time.now.to_i + ttl]
  true
end