Sha256: 2472aa86c9c843663ad0ba6d55826fd8a277e4e962dc9d7359249232d4556510

Contents?: true

Size: 840 Bytes

Versions: 1

Compression:

Stored size: 840 Bytes

Contents

module Lux
  class Cache
    class MemoryServer
      @@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

      def clear
        @@ram_cache = {}
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lux-fw-0.6.2 ./lib/lux/cache/lib/memory_server.rb