Sha256: 085a81120072292f9514db69b4ea3f9a4cbd3c37ea1f443984ec8ea372570acc

Contents?: true

Size: 914 Bytes

Versions: 1

Compression:

Stored size: 914 Bytes

Contents

module FakeRedis
  # Represents a normal hash with some additional expiration information
  # associated with each key
  class ExpiringHash < Hash
    attr_reader :expires

    def initialize(*)
      super
      @expires = {}
    end

    def [](key)
      delete(key) if expired?(key)
      super
    end

    def []=(key, val)
      expire(key)
      super
    end

    def delete(key)
      expire(key)
      super
    end

    def expire(key)
      expires.delete(key)
    end

    def expired?(key)
      expires.include?(key) && expires[key] < Time.now
    end

    def key?(key)
      delete(key) if expired?(key)
      super
    end

    def values_at(*keys)
      keys.each {|key| delete(key) if expired?(key)}
      super
    end

    def keys
      super.select do |key|
        if expired?(key)
          delete(key)
          false
        else
          true
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fakeredis-0.3.3 lib/fakeredis/expiring_hash.rb