Class: Rudis::Hash

Inherits:
Structure show all
Defined in:
lib/rudis/structures/hash.rb

Instance Method Summary collapse

Methods inherited from Structure

#exists?, #expire, #expire_at, #redis_type, #rename, #ttl, #type, #watch

Methods inherited from Base

#initialize, #key, #redis

Methods inherited from Rudis

key

Constructor Details

This class inherits a constructor from Rudis::Base

Instance Method Details

#allObject Also known as: to_h



50
51
52
53
54
# File 'lib/rudis/structures/hash.rb', line 50

def all
  redis.hgetall(key).map! do |k,v|
    [key_type.get(k), type.get(v)]
  end
end

#default_optionsObject



3
4
5
6
7
8
# File 'lib/rudis/structures/hash.rb', line 3

def default_options
  {
    :type => DefaultType,
    :key_type => DefaultType
  }
end

#del(k) ⇒ Object



68
69
70
# File 'lib/rudis/structures/hash.rb', line 68

def del(k)
  redis.hdel(key, key_type.put(k))
end

#empty?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/rudis/structures/hash.rb', line 64

def empty?
  len == 0
end

#get(k) ⇒ Object Also known as: []



14
15
16
17
# File 'lib/rudis/structures/hash.rb', line 14

def get(k)
  e = redis.hget(key, key_type.put(k))
  e && type.get(e)
end

#has_key?(k) ⇒ Boolean Also known as: include?

Returns:

  • (Boolean)


72
73
74
# File 'lib/rudis/structures/hash.rb', line 72

def has_key?(k)
  redis.hexists(key, key_type.put(k))
end

#incr(k) ⇒ Object



81
82
83
# File 'lib/rudis/structures/hash.rb', line 81

def incr(k)
  incrby(k, 1)
end

#incrby(k, i) ⇒ Object



77
78
79
# File 'lib/rudis/structures/hash.rb', line 77

def incrby(k, i)
  redis.hincrby(key, key_type.put(k), i.to_i)
end

#key_typeObject



10
11
12
# File 'lib/rudis/structures/hash.rb', line 10

def key_type
  @options[:key_type]
end

#keysObject



41
42
43
# File 'lib/rudis/structures/hash.rb', line 41

def keys
  redis.hkeys(key).map { |k| key_type.get(k) }
end

#lenObject Also known as: length, count, size



57
58
59
# File 'lib/rudis/structures/hash.rb', line 57

def len
  redis.hlen(key)
end

#mget(*ks) ⇒ Object Also known as: slice



25
26
27
28
29
30
31
# File 'lib/rudis/structures/hash.rb', line 25

def mget(*ks)
  ks.zip(redis.hmget(key, ks.map { |k|
    key_type.put(k)
  }).map { |v|
    type.get(v)
  }).to_h 
end

#mset(hsh) ⇒ Object Also known as: merge!



34
35
36
37
38
# File 'lib/rudis/structures/hash.rb', line 34

def mset(hsh)
  hsh = hsh.dup
  hsh.map! {|k,v| [key_type.put(k), type.put(v)]}
  redis.hmset(key, *hsh.to_a.flatten)
end

#set(k, v) ⇒ Object Also known as: []=



20
21
22
# File 'lib/rudis/structures/hash.rb', line 20

def set(k,v)
  redis.hset(key, key_type.put(k), type.put(v))
end

#valsObject Also known as: values



45
46
47
# File 'lib/rudis/structures/hash.rb', line 45

def vals
  redis.hvals(key).map { |v| type.get(v) }
end