class STRC
Constants
- Exception
Public Class Methods
# File lib/strc.rb, line 6 def initialize @dict = {} end
Public Instance Methods
Append a value to a key
# File lib/strc.rb, line 31 def append(key, value) if exists(key) item = get(key) if item.class == String and value.class == String set(key, item + value) else raise STRC::Exception.new "ERR Operation against a key holding the wrong kind of value" end else set(key, value) end return get(key).length end
Decrement an integer
# File lib/strc.rb, line 46 def decr(key) decrby(key, 1) end
Decrement an integer by a certain amount
# File lib/strc.rb, line 51 def decrby(key, decrement) unless exists(key) set(key, 0) end value = get(key) if value.class == Fixnum set(key, value - decrement) else raise STRC::Exception.new "ERR Operation against a key holding the wrong kind of value" end get(key) end
Delete a key
# File lib/strc.rb, line 86 def del(*keys) keys.each do |key| @dict.delete(key) end return keys.length end
Returns if key exists
# File lib/strc.rb, line 94 def exists(key) @dict.has_key?(key) end
Get the value for the given key
# File lib/strc.rb, line 26 def get(key) @dict[key] end
Deletes fields from hash
# File lib/strc.rb, line 399 def hdel(key, *fields) hash = get_h(key) deleted = 0 fields.each do |field| unless hash.delete(field).nil? deleted += 1 end end set(key, hash) return deleted end
Returns whether or not the field exists in key
# File lib/strc.rb, line 412 def hexists(key, field) get_h(key).has_key?(field) end
Get value at field in hash at key
# File lib/strc.rb, line 394 def hget(key, field) get_h(key)[field] end
Returns an array of all fields and values in hash
# File lib/strc.rb, line 417 def hgetall(key) get_h(key).flatten end
Returns array of all keys in hash at key
# File lib/strc.rb, line 422 def hkeys(key) get_h(key).keys end
Returns number of fields in the hash
# File lib/strc.rb, line 432 def hlen(key) get_h(key).length end
Set file in hash stored at key to value.
# File lib/strc.rb, line 378 def hset(key, field, value) hash = get_h(key) hash[field] = value set(key, hash) end
Sets field in key only if it doesn't exist
# File lib/strc.rb, line 385 def hsetnx(key, field, value) unless hexists(key, field) hset(key, field, value) return true end return false end
Returns array of all values in hash at key
# File lib/strc.rb, line 427 def hvals(key) get_h(key).values end
Increment an integer
# File lib/strc.rb, line 65 def incr(key) incrby(key, 1) end
Increment an integer by a certain amount
# File lib/strc.rb, line 70 def incrby(key, increment) unless exists(key) set(key, 0) end value = get(key) if value.class == Fixnum set(key, value + increment) else raise STRC::Exception.new "ERR Operation against a key holding the wrong kind of value" end get(key) end
Get an element from a list by its index
# File lib/strc.rb, line 339 def lindex(key, index) get_l(key)[index] end
Returns length of list
# File lib/strc.rb, line 288 def llen(key) get_l(key).length end
Remove and get the first element in a list
# File lib/strc.rb, line 331 def lpop(key) list = get_l(key) element = list.shift set(key, list) return element end
Prepend values to a list
# File lib/strc.rb, line 316 def lpush(key, *values) list = get_l(key) list = values + list set(key, list) return list.length end
LPUSH if the list exists
# File lib/strc.rb, line 324 def lpushx(key, *values) if exists(key) lpush(key, *values) end end
Returns elements from key in the given range
# File lib/strc.rb, line 282 def lrange(key, start, stop) list = get_l(key)[start..stop] return list.nil? ? [] : list end
Set value for an element at index in a list
# File lib/strc.rb, line 344 def lset(key, index, value) list = get_l(key) list[index] = value set(key, list) end
Trim a list to the specified range
# File lib/strc.rb, line 351 def ltrim(key, start, stop) set(key, lrange(key, start, stop)) end
Returns a random key
# File lib/strc.rb, line 99 def randomkey @dict.keys.sample end
Renames a key. Overwrites destination.
# File lib/strc.rb, line 104 def rename(key, newkey) if key == newkey raise STRC::Exception.new "ERR Source and destination objects are the same" end @dict[newkey] = @dict[key] del(key) return "OK" end
Renames a key. Does not overwrite destination.
# File lib/strc.rb, line 114 def renamenx(key, newkey) if exists(newkey) return false else rename(key, newkey) return true end end
Remove and get the last element in a list
# File lib/strc.rb, line 308 def rpop(key) list = get_l(key) element = list.pop set(key, list) return element end
Removes an element from the end of one list and puts it at the beginning of another
# File lib/strc.rb, line 357 def rpoplpush(source, destination) lpush(destination, rpop(source)) end
Append values to a list
# File lib/strc.rb, line 293 def rpush(key, *values) list = get_l(key) list += values set(key, list) return list.length end
RPUSH if the list exists
# File lib/strc.rb, line 301 def rpushx(key, *values) if exists(key) rpush(key, *values) end end
Add a member to a set
# File lib/strc.rb, line 128 def sadd(key, *args) if args.length < 1 raise STRC::Exception.new "Wrong number of arguments (1 for 2)" end set = get_s(key) added = 0 args.each do |arg| unless set.include?(arg) set << arg added += 1 end end @dict[key] = set return added end
Gets the length of a set
# File lib/strc.rb, line 248 def scard(key) set = get_s(key) return set.length end
Returns a list of the difference between the first set and subsequent sets
# File lib/strc.rb, line 175 def sdiff(*keys) sets = [] keys.each do |key| sets << get_s(key) end diff = sets.shift sets.each do |set| diff -= set end return diff.to_a end
Similar to SDIFF, but stores in destination instead of returning
# File lib/strc.rb, line 188 def sdiffstore(destination, *keys) elements = sdiff(*keys) sadd(destination, *elements) end
Set a given key to a value
# File lib/strc.rb, line 11 def set(key, value) @dict[key] = value return "OK" end
Set the value of a key only if it doesn't already exist.
# File lib/strc.rb, line 17 def setnx(key, value) unless exists(key) set(key, value) return true end return false end
Returns a list of m
# File lib/strc.rb, line 155 def sinter(*keys) sets = [] keys.each do |key| sets << get_s(key) end inter = sets.shift sets.each do |set| inter &= set end return inter.to_a end
Similar to SINTER, but stores in destination instead of returning
# File lib/strc.rb, line 168 def sinterstore(destination, *keys) elements = sinter(*keys) sadd(destination, *elements) end
Determine if the given value is a member of the set at key
# File lib/strc.rb, line 237 def sismember(key, member) set = get_s(key) return set.include?(member) end
Returns an array of members of the set
# File lib/strc.rb, line 243 def smembers(key) get_s(key).to_a end
Move an item from one set to another
# File lib/strc.rb, line 194 def smove(source, destination, member) unless sismember(source, member) return false end srem(source, member) unless sismember(destination, member) sadd(destination, member) end return true end
Randomly remove and return an item from the set
# File lib/strc.rb, line 230 def spop(key) element = srandmember(key) srem(key, element) return element end
Returns a random element from the set
# File lib/strc.rb, line 225 def srandmember(key) get_s(key).to_a.sample end
Remove a member from a set
# File lib/strc.rb, line 145 def srem(key, member) if sismember(key, member) @dict[key].delete(member) return true else return false end end
Returs a list of all unique items in the given sets
# File lib/strc.rb, line 206 def sunion(*keys) sets = [] keys.each do |key| sets << get_s(key) end union = sets.shift sets.each do |set| union += set end return union.to_a end
Similar to SUNION, but stores in destination instead of returning
# File lib/strc.rb, line 219 def sunionstore(destination, *keys) elements = sunion(*keys) sadd(destination, *elements) end
Private Instance Methods
Gets the hash at key. Private.
# File lib/strc.rb, line 366 def get_h(key) hash = {} if exists(key) hash = get(key) unless hash.class == Hash raise STRC::Exception.new "ERR Operation against a key holding the wrong kind of value" end end return hash end
Gets a list. Private
# File lib/strc.rb, line 270 def get_l(key) list = [] if exists(key) list = get(key) unless list.class == Array raise STRC::Exception.new "ERR Operation against a key holding the wrong kind of value" end end return list end
Gets a set. Private
# File lib/strc.rb, line 254 def get_s(key) set = Set.new if exists(key) set = get(key) unless set.class == Set raise STRC::Exception.new "ERR Operation against a key holding the wrong kind of value" end end return set end