Class: Familia::HashKey
Instance Attribute Summary
Attributes inherited from RedisType
#dump_method, #keystring, #load_method, #opts, #parent
Instance Method Summary
collapse
-
#[](field) ⇒ Object
(also: #get)
-
#[]=(field, val) ⇒ Object
(also: #put, #store)
return [Integer] Returns 1 if the field is new and added, 0 if the field already existed and the value was updated.
-
#decrement(field, by = 1) ⇒ Object
(also: #decr, #decrby)
-
#delete(field) ⇒ Object
(also: #remove, #rem, #del)
-
#empty? ⇒ Boolean
-
#fetch(field, default = nil) ⇒ Object
-
#hgetall ⇒ Object
(also: #all)
-
#increment(field, by = 1) ⇒ Object
(also: #incr, #incrby)
-
#key?(field) ⇒ Boolean
(also: #has_key?, #include?, #member?)
-
#keys ⇒ Object
-
#size ⇒ Object
(also: #length)
-
#update(hsh = {}) ⇒ Object
(also: #merge!)
-
#values ⇒ Object
-
#values_at(*fields) ⇒ Object
Methods inherited from RedisType
#class?, #db, inherited, #initialize, #parent?, #parent_class?, #parent_instance?, #redis, #rediskey, register, #ttl, #uri, valid_keys_only
#from_redis, #multi_from_redis, #multi_from_redis_with_nil, #to_redis, #update_expiration
#delete!, #echo, #exists?, #expire, #expireat, #move, #persist, #realttl, #rename, #renamenx, #type
Methods included from Base
add_feature
Instance Method Details
#[](field) ⇒ Object
Also known as:
get
31
32
33
|
# File 'lib/familia/types/hashkey.rb', line 31
def [](field)
from_redis redis.hget(rediskey, field)
end
|
#[]=(field, val) ⇒ Object
Also known as:
put, store
return [Integer] Returns 1 if the field is new and added, 0 if the
field already existed and the value was updated.
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/familia/types/hashkey.rb', line 16
def []=(field, val)
ret = redis.hset rediskey, field, to_redis(val)
update_expiration
ret
rescue TypeError => e
Familia.le "[hset]= #{e.message}"
Familia.ld "[hset]= #{rediskey} #{field}=#{val}" if Familia.debug
echo :hset, caller(1..1).first if Familia.debug klass = val.class
msg = "Cannot store #{field} => #{val.inspect} (#{klass}) in #{rediskey}"
raise e.class, msg
end
|
#decrement(field, by = 1) ⇒ Object
Also known as:
decr, decrby
83
84
85
|
# File 'lib/familia/types/hashkey.rb', line 83
def decrement(field, by = 1)
increment field, -by
end
|
#delete(field) ⇒ Object
Also known as:
remove, rem, del
70
71
72
|
# File 'lib/familia/types/hashkey.rb', line 70
def delete(field)
redis.hdel rediskey, field
end
|
#empty? ⇒ Boolean
10
11
12
|
# File 'lib/familia/types/hashkey.rb', line 10
def empty?
size.zero?
end
|
#fetch(field, default = nil) ⇒ Object
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/familia/types/hashkey.rb', line 36
def fetch(field, default = nil)
ret = self[field]
if ret.nil?
raise IndexError, "No such index for: #{field}" if default.nil?
default
else
ret
end
end
|
#hgetall ⇒ Object
Also known as:
all
56
57
58
59
60
|
# File 'lib/familia/types/hashkey.rb', line 56
def hgetall
redis.hgetall rediskey
end
|
#increment(field, by = 1) ⇒ Object
Also known as:
incr, incrby
77
78
79
|
# File 'lib/familia/types/hashkey.rb', line 77
def increment(field, by = 1)
redis.hincrby(rediskey, field, by).to_i
end
|
#key?(field) ⇒ Boolean
Also known as:
has_key?, include?, member?
63
64
65
|
# File 'lib/familia/types/hashkey.rb', line 63
def key?(field)
redis.hexists rediskey, field
end
|
#keys ⇒ Object
47
48
49
|
# File 'lib/familia/types/hashkey.rb', line 47
def keys
redis.hkeys rediskey
end
|
#size ⇒ Object
Also known as:
length
5
6
7
|
# File 'lib/familia/types/hashkey.rb', line 5
def size
redis.hlen rediskey
end
|
#update(hsh = {}) ⇒ Object
Also known as:
merge!
89
90
91
92
93
94
95
96
97
|
# File 'lib/familia/types/hashkey.rb', line 89
def update(hsh = {})
raise ArgumentError, 'Argument to bulk_set must be a hash' unless hsh.is_a?(Hash)
data = hsh.inject([]) { |ret, pair| ret << [pair[0], to_redis(pair[1])] }.flatten
ret = redis.hmset(rediskey, *data)
update_expiration
ret
end
|
#values ⇒ Object
51
52
53
54
|
# File 'lib/familia/types/hashkey.rb', line 51
def values
el = redis.hvals(rediskey)
multi_from_redis(*el)
end
|
#values_at(*fields) ⇒ Object
100
101
102
103
|
# File 'lib/familia/types/hashkey.rb', line 100
def values_at *fields
el = redis.hmget(rediskey, *fields.flatten.compact)
multi_from_redis(*el)
end
|