class Redis::HashKey
Class representing a Redis
hash.
Public Class Methods
Redis::BaseObject::new
# File lib/redis/hash_key.rb, line 8 def initialize(key, *args) super @options[:marshal_keys] ||= {} end
Public Instance Methods
Retrieve the entire hash. Redis: HGETALL
# File lib/redis/hash_key.rb, line 63 def all h = redis.hgetall(key) || {} h.each{|k,v| h[k] = unmarshal(v, options[:marshal_keys][k]) } h end
Get keys in bulk, takes an array of fields as arguments. Redis: HMGET
# File lib/redis/hash_key.rb, line 115 def bulk_get(*fields) hsh = {} get_fields = *fields.flatten return hsh if get_fields.empty? res = redis.hmget(key, get_fields) get_fields.each do |k| hsh[k] = unmarshal(res.shift, options[:marshal_keys][k]) end hsh end
Set
keys in bulk, takes a hash of field/values {‘field1’ => ‘val1’}. Redis: HMSET
# File lib/redis/hash_key.rb, line 94 def bulk_set(*args) raise ArgumentError, "Argument to bulk_set must be hash of key/value pairs" unless args.last.is_a?(::Hash) allow_expiration do redis.hmset(key, *args.last.inject([]){ |arr,kv| arr + [kv[0], marshal(kv[1], options[:marshal_keys][kv[0]])] }) end end
Get values in bulk, takes an array of fields as arguments. Values are returned in a collection in the same order than their keys in *keys Redis: HMGET
# File lib/redis/hash_key.rb, line 128 def bulk_values(*fields) get_fields = *fields.flatten return [] if get_fields.empty? res = redis.hmget(key, get_fields) get_fields.collect{|k| unmarshal(res.shift, options[:marshal_keys][k])} end
Decrement value by integer at field. Redis: HINCRBY
# File lib/redis/hash_key.rb, line 145 def decrby(field, by=1) incrby(field, -by) end
Decrement value by float at field. Redis: HINCRBYFLOAT
# File lib/redis/hash_key.rb, line 159 def decrbyfloat(field, by=1.0) incrbyfloat(field, -by) end
Delete fields. Redis: HDEL
# File lib/redis/hash_key.rb, line 37 def delete(*field) redis.hdel(key, field) end
Enumerate through each keys. Redis: HKEYS
# File lib/redis/hash_key.rb, line 72 def each_key(&block) keys.each(&block) end
Enumerate through all values. Redis: HVALS
# File lib/redis/hash_key.rb, line 77 def each_value(&block) values.each(&block) end
Returns true if dict is empty
# File lib/redis/hash_key.rb, line 89 def empty? true if size == 0 end
Fetch a key in a way similar to Ruby’s Hash#fetch
# File lib/redis/hash_key.rb, line 42 def fetch(field, *args, &block) value = hget(field) default = args[0] return value if value || (!default && !block_given?) block_given? ? block.call(field) : default end
Set
keys in bulk if they do not exist. Takes a hash of field/values {‘field1’ => ‘val1’}. Redis: HSETNX
# File lib/redis/hash_key.rb, line 105 def fill(pairs={}) raise ArgumentError, "Argument to fill must be a hash of key/value pairs" unless pairs.is_a?(::Hash) allow_expiration do pairs.each do |field, value| redis.hsetnx(key, field, marshal(value, options[:marshal_keys][field])) end end end
Verify that a field exists. Redis: HEXISTS
# File lib/redis/hash_key.rb, line 29 def has_key?(field) redis.hexists(key, field) end
Redis: HGET
# File lib/redis/hash_key.rb, line 22 def hget(field) unmarshal redis.hget(key, field), options[:marshal_keys][field] end
Increment value by integer at field. Redis: HINCRBY
# File lib/redis/hash_key.rb, line 136 def incrby(field, by=1) allow_expiration do ret = redis.hincrby(key, field, by) ret.to_i end end
Increment value by float at field. Redis: HINCRBYFLOAT
# File lib/redis/hash_key.rb, line 151 def incrbyfloat(field, by=1.0) allow_expiration do ret = redis.hincrbyfloat(key, field, by) ret.to_f end end
Return all the keys of the hash. Redis: HKEYS
# File lib/redis/hash_key.rb, line 52 def keys redis.hkeys(key) end
Return the size of the dict. Redis: HLEN
# File lib/redis/hash_key.rb, line 82 def size redis.hlen(key) end
Redis: HSET
# File lib/redis/hash_key.rb, line 14 def store(field, value) allow_expiration do redis.hset(key, field, marshal(value, options[:marshal_keys][field])) end end
Return all the values of the hash. Redis: HVALS
# File lib/redis/hash_key.rb, line 57 def values redis.hvals(key).map{|v| unmarshal(v) } end