Sha256: 64b33a9cfeff94d87f673c9b7fe54e209976d7cda2e86c8325f5e8ed2359bf55

Contents?: true

Size: 658 Bytes

Versions: 4

Compression:

Stored size: 658 Bytes

Contents

module Lux::Cache::RamCache
  extend self

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

  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

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

    @@ram_cache[key]
  end

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

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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lux-fw-0.2.3 ./lib/lux/cache/lib/ram.rb
lux-fw-0.2.1 ./lib/lux/cache/lib/ram.rb
lux-fw-0.1.35 ./lib/lux/cache/lib/ram.rb
lux-fw-0.1.17 ./lib/lux/cache/lib/ram.rb