Class: Utest::InmemoryRedis
- Inherits:
-
Object
- Object
- Utest::InmemoryRedis
- Defined in:
- lib/utest/inmemory_redis.rb
Instance Method Summary collapse
- #del(key) ⇒ Object
- #get(key) ⇒ Object
- #hget(key, field) ⇒ Object
- #hgetall(key) ⇒ Object
- #hincrby(key, field, incr) ⇒ Object
- #hset(key, field, val) ⇒ Object
- #incr(key) ⇒ Object
- #incrby(key, incr) ⇒ Object
-
#initialize(data = {}) ⇒ InmemoryRedis
constructor
A new instance of InmemoryRedis.
- #lindex(key, index) ⇒ Object
- #llen(key) ⇒ Object
- #lpop(key) ⇒ Object
- #lrange(key, start, stop) ⇒ Object
- #lrem(key, count, value) ⇒ Object
- #lset(key, index, value) ⇒ Object
- #multi ⇒ Object
- #rpop(key) ⇒ Object
- #rpush(key, value) ⇒ Object
- #sadd(key, member) ⇒ Object
- #scard(key) ⇒ Object
- #set(key, val) ⇒ Object
- #smembers(key) ⇒ Object
- #srem(key, member) ⇒ Object
Constructor Details
#initialize(data = {}) ⇒ InmemoryRedis
Returns a new instance of InmemoryRedis.
4 5 6 7 8 9 |
# File 'lib/utest/inmemory_redis.rb', line 4 def initialize(data = {}) @values = {} @sets = {} @lists = {} @hashes = {} end |
Instance Method Details
#del(key) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/utest/inmemory_redis.rb', line 11 def del(key) count = 0 count = del_if_set(key,@values,count) count = del_if_set(key,@sets,count) count = del_if_set(key,@lists,count) count = del_if_set(key,@hashes,count) count end |
#get(key) ⇒ Object
57 58 59 |
# File 'lib/utest/inmemory_redis.rb', line 57 def get(key) @values[key] end |
#hget(key, field) ⇒ Object
21 22 23 24 |
# File 'lib/utest/inmemory_redis.rb', line 21 def hget(key,field) return nil unless @hashes.key?(key) @hashes[key][field] end |
#hgetall(key) ⇒ Object
48 49 50 |
# File 'lib/utest/inmemory_redis.rb', line 48 def hgetall(key) @hashes[key] || {} end |
#hincrby(key, field, incr) ⇒ Object
42 43 44 45 46 |
# File 'lib/utest/inmemory_redis.rb', line 42 def hincrby(key,field,incr) new_value = (hget(key,field) || 0).to_i + incr.to_i hset(key,field,"#{new_value}") new_value end |
#hset(key, field, val) ⇒ Object
26 27 28 29 30 |
# File 'lib/utest/inmemory_redis.rb', line 26 def hset(key,field,val) @hashes[key] ||= {} @hashes[key][field] = val true end |
#incr(key) ⇒ Object
32 33 34 |
# File 'lib/utest/inmemory_redis.rb', line 32 def incr(key) incrby(key,1) end |
#incrby(key, incr) ⇒ Object
36 37 38 39 40 |
# File 'lib/utest/inmemory_redis.rb', line 36 def incrby(key,incr) new_value = (get(key) || 0).to_i + incr.to_i set(key,"#{new_value}") new_value end |
#lindex(key, index) ⇒ Object
90 91 92 93 |
# File 'lib/utest/inmemory_redis.rb', line 90 def lindex(key,index) return nil if @lists[key].nil? @lists[key][index] end |
#llen(key) ⇒ Object
85 86 87 88 |
# File 'lib/utest/inmemory_redis.rb', line 85 def llen(key) return 0 if @lists[key].nil? @lists[key].size end |
#lpop(key) ⇒ Object
138 139 140 141 |
# File 'lib/utest/inmemory_redis.rb', line 138 def lpop(key) return nil if @lists[key].nil? @lists[key].shift end |
#lrange(key, start, stop) ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'lib/utest/inmemory_redis.rb', line 129 def lrange(key,start,stop) return [] if @lists[key].nil? start = 0 if start < 0 max_stop = llen(key) - 1 stop = max_stop if (stop == -1 || stop > max_stop) return [] if start > stop @lists[key][start..stop] end |
#lrem(key, count, value) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/utest/inmemory_redis.rb', line 95 def lrem(key,count,value) return 0 if @lists[key].nil? my_list = @lists[key] old_size = my_list.size if count == 0 my_list.delete(value) else my_list.reverse! if count < 0 num_found = 0 my_list.reject! do |x| num_found += 1 if x == value num_found > count.abs ? false : x == value end my_list.reverse! if count < 0 end new_size = my_list.size old_size - new_size end |
#lset(key, index, value) ⇒ Object
116 117 118 119 120 121 |
# File 'lib/utest/inmemory_redis.rb', line 116 def lset(key,index,value) raise "ERR no such key" if @lists[key].nil? raise "ERR index out of range" if @lists[key][index].nil? @lists[key][index] = value true end |
#multi ⇒ Object
148 149 150 |
# File 'lib/utest/inmemory_redis.rb', line 148 def multi yield end |
#rpop(key) ⇒ Object
143 144 145 146 |
# File 'lib/utest/inmemory_redis.rb', line 143 def rpop(key) return nil if @lists[key].nil? @lists[key].pop end |
#rpush(key, value) ⇒ Object
123 124 125 126 127 |
# File 'lib/utest/inmemory_redis.rb', line 123 def rpush(key,value) @lists[key] = [] if @lists[key].nil? @lists[key] << value true end |
#sadd(key, member) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/utest/inmemory_redis.rb', line 66 def sadd(key,member) @sets[key] = [] if @sets[key].nil? return 0 if @sets[key].include?(member) @sets[key] << member 1 end |
#scard(key) ⇒ Object
61 62 63 64 |
# File 'lib/utest/inmemory_redis.rb', line 61 def scard(key) return 0 if @sets[key].nil? @sets[key].size end |
#set(key, val) ⇒ Object
52 53 54 55 |
# File 'lib/utest/inmemory_redis.rb', line 52 def set(key,val) @values[key] = val true end |
#smembers(key) ⇒ Object
80 81 82 83 |
# File 'lib/utest/inmemory_redis.rb', line 80 def smembers(key) @sets[key] = [] if @sets[key].nil? @sets[key] end |
#srem(key, member) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/utest/inmemory_redis.rb', line 73 def srem(key,member) return 0 if @sets[key].nil? return 0 unless @sets[key].include?(member) @sets[key].delete(member) 1 end |