class Collectr::RedisHashExpiry

Attributes

store[R]

Public Class Methods

new(name, options={}) click to toggle source
# File lib/collectr/redis/redis_hash_expiry.rb, line 19
def initialize(name, options={})
  @title = name
  # Use raw only when both the keys and values are strings.
  @raw = options.fetch(:raw) { false }
  @store = Redis.current
  @default_expires_in = options[:expires_in]
end

Public Instance Methods

[](key) click to toggle source
# File lib/collectr/redis/redis_hash_expiry.rb, line 27
def [](key)
  deserialize @store.get(key_name(key))
end
[]=(key, val, options={}) click to toggle source
# File lib/collectr/redis/redis_hash_expiry.rb, line 31
def []=(key, val, options={})
    write(key, val, options)
end
cache(key, options={}) { |key| ... } click to toggle source
# File lib/collectr/redis/redis_hash_expiry.rb, line 60
def cache(key, options={})
    fetch(key, options) do
            result = yield key
            write(key, result, options)
            result
    end
end
clear() click to toggle source
# File lib/collectr/redis/redis_hash_expiry.rb, line 107
def clear
  destroy
  # keys.each{ |key| delete key }
end
dekey(key) click to toggle source
# File lib/collectr/redis/redis_hash_expiry.rb, line 116
def dekey(key)
    deserialize(key[key_prefix.size-1..-1])
end
delete(key) click to toggle source
# File lib/collectr/redis/redis_hash_expiry.rb, line 72
def delete(key)
  @store.del key_name(key)
end
destroy() click to toggle source
# File lib/collectr/redis/redis_hash_expiry.rb, line 68
def destroy 
    keys.each{ |key| delete key }
end
empty?() click to toggle source
# File lib/collectr/redis/redis_hash_expiry.rb, line 76
def empty?
  size == 0
end
fetch(key, options={}) { |key| ... } click to toggle source
# File lib/collectr/redis/redis_hash_expiry.rb, line 47
def fetch(key, options={})
  result = self[key]
  if result.nil?
    return nil if has_key?(key)
    if block_given?
      result = yield key
    else
      raise KeyError
    end
  end
  result
end
has_key?(key) click to toggle source
# File lib/collectr/redis/redis_hash_expiry.rb, line 84
def has_key?(key)
  key? key
end
key?(key) click to toggle source
# File lib/collectr/redis/redis_hash_expiry.rb, line 87
def key?(key)
  @store.exists key_name(key)
end
key_name(key) click to toggle source
# File lib/collectr/redis/redis_hash_expiry.rb, line 112
def key_name(key)
    "#{@title}-#{serialize(key)}"
end
key_prefix() click to toggle source
# File lib/collectr/redis/redis_hash_expiry.rb, line 120
def key_prefix
    @key_prefix ||= "#{@title}-*"
end
keys() click to toggle source
# File lib/collectr/redis/redis_hash_expiry.rb, line 91
def keys
  @store.keys(key_prefix).collect{ |key| dekey key }
end
set_expiration(key, seconds) click to toggle source
# File lib/collectr/redis/redis_hash_expiry.rb, line 35
def set_expiration(key, seconds)
    return unless seconds
    @store.expire(key_name(key), seconds)
end
size() click to toggle source
# File lib/collectr/redis/redis_hash_expiry.rb, line 80
def size
  keys.size
end
to_hash() click to toggle source
# File lib/collectr/redis/redis_hash_expiry.rb, line 99
def to_hash
  hash = {}
  keys.each do |key|
    hash[key] = self[key]
  end
  hash
end
values() click to toggle source
# File lib/collectr/redis/redis_hash_expiry.rb, line 95
def values
  keys.collect{ |key| self[key] }
end
write(key, val, options={}) click to toggle source
# File lib/collectr/redis/redis_hash_expiry.rb, line 40
def write(key, val, options={})
  expiration = options.fetch(:expires_in) { @default_expires_in }
  @store.set key_name(key), serialize(val)
  set_expiration(key, expiration)
  val
end