Sha256: 903944258a928c7065523d99e6fdb01a34abe25750f301f6e1719ace3bcaf6d3

Contents?: true

Size: 641 Bytes

Versions: 6

Compression:

Stored size: 641 Bytes

Contents

class Lux::Cache::MemoryCache
  @@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

6 entries across 6 versions & 1 rubygems

Version Path
lux-fw-0.5.37 ./lib/lux/cache/lib/memory.rb
lux-fw-0.5.36 ./lib/lux/cache/lib/memory.rb
lux-fw-0.5.35 ./lib/lux/cache/lib/memory.rb
lux-fw-0.5.34 ./lib/lux/cache/lib/memory.rb
lux-fw-0.5.33 ./lib/lux/cache/lib/memory.rb
lux-fw-0.5.32 ./lib/lux/cache/lib/memory.rb