Module: Lux::Cache::RamCache

Extended by:
RamCache
Included in:
RamCache
Defined in:
lib/lux/cache/lib/ram.rb

Constant Summary collapse

@@lock =
Mutex.new
@@ram_cache =
{}
@@ttl_cache =
{}

Instance Method Summary collapse

Instance Method Details

#delete(key) ⇒ Object



29
30
31
32
33
# File 'lib/lux/cache/lib/ram.rb', line 29

def delete(key)
  @@lock.synchronize do
    @@ram_cache.delete(key)
  end
end

#fetch(key, ttl = nil) ⇒ Object



23
24
25
26
27
# File 'lib/lux/cache/lib/ram.rb', line 23

def fetch(key, ttl=nil)
  data = get key
  return data if data
  set(key, yield, ttl)
end

#get(key) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/lux/cache/lib/ram.rb', line 15

def get(key)
  if ttl_check = @@ttl_cache[key]
    return nil if ttl_check < Time.now.to_i
  end

  @@ram_cache[key]
end

#get_multi(*args) ⇒ Object



35
36
37
# File 'lib/lux/cache/lib/ram.rb', line 35

def get_multi(*args)
  @@ram_cache.select{ |k,v| args.index(k) }
end

#set(key, data, ttl = nil) ⇒ Object



8
9
10
11
12
13
# File 'lib/lux/cache/lib/ram.rb', line 8

def set(key, data, ttl=nil)
  @@lock.synchronize do
    @@ttl_cache[key] = Time.now.to_i + ttl if ttl
    @@ram_cache[key] = data
  end
end