Protocol::Redis SourceProtocolRedisMethodsHashes

module Hashes

Definitions

def hlen(key)

Get the number of fields in a hash. O(1).

Implementation

def hlen(key)
	call('HLEN', key)
end

def hset(key, field, value)

Set the string value of a hash field. O(1) for each field/value pair added, so O(N) to add N field/value pairs when the command is called with multiple field/value pairs.

Implementation

def hset(key, field, value)
	call('HSET', key, field, value)
end

def hsetnx(key, field, value)

Set the value of a hash field, only if the field does not exist. O(1).

Implementation

def hsetnx(key, field, value)
	call('HSETNX', key, field, value) > 0
end

def hmset(key, *attrs)

Set multiple hash fields to multiple values. O(N) where N is the number of fields being set.

Implementation

def hmset(key, *attrs)
	call('HMSET', key, *attrs)
end

def mapped_hmset(key, hash)

Set multiple hash fields to multiple values, by providing a hash

redis.mapped_hmset("hash", "f1" => "v1", "f2" => "v2" ) # => "OK"

Implementation

def mapped_hmset(key, hash)
	hmset(key, *hash.flatten)
end

def hget(key, field)

Get the value of a hash field. O(1).

Implementation

def hget(key, field)
	call('HGET', key, field)
end

def hmget(key, *fields)

Get the values of all the given hash fields. O(N) where N is the number of fields being requested.

Implementation

def hmget(key, *fields)
	call('HMGET', key, *fields)
end

def mapped_hmget(key, *fields)

Get the values of all the given hash fields and return as array

redis.mapped_hmget("hash", "f1", "f2") # => "f1" => "v1", "f2" => "v2"

Implementation

def mapped_hmget(key, *fields)
	reply = hmget(key, *fields)
	Hash[fields.zip(reply)]
end

def hdel(key, *fields)

Delete one or more hash fields. O(N) where N is the number of fields to be removed.

Implementation

def hdel(key, *fields)
	call('HDEL', key, *fields)
end

def hexists(key, field)

Determine if a hash field exists. O(1).

Implementation

def hexists(key, field)
	call('HEXISTS', key, field) > 0
end

def hincrby(key, field, increment)

Increment the integer value of a hash field by the given number. O(1).

Implementation

def hincrby(key, field, increment)
	call('HINCRBY', key, field, increment)
end

def hincrbyfloat(key, field, increment)

Increment the float value of a hash field by the given amount. O(1).

Implementation

def hincrbyfloat(key, field, increment)
	Float(call('HINCRBYFLOAT', key, field, increment))
end

def hkeys(key)

Get all the fields in a hash. O(N) where N is the size of the hash.

Implementation

def hkeys(key)
	call('HKEYS', key)
end

def hvals(key)

Get all the values in a hash. O(N) where N is the size of the hash.

Implementation

def hvals(key)
	call('HVALS', key)
end

def hgetall(key)

Get all the fields and values in a hash. O(N) where N is the size of the hash.

Implementation

def hgetall(key)
	call('HGETALL', key).each_slice(2).to_h
end

def hscan(key, cursor = "0", match: nil, count: nil)

Iterates fields of Hash types and their associated values. O(1) for every call. O(N) for a complete iteration, including enough command calls for the cursor to return back to 0. N is the number of elements inside the collection.

Implementation

def hscan(key, cursor = "0", match: nil, count: nil)
	arguments = [key, cursor]

	if match
		arguments.append("MATCH", match)
	end

	if count
		arguments.append("COUNT", count)
	end

	call("HSCAN", *arguments)
end

def hscan_each(key, cursor = "0", match: nil, count: nil, &block)

Iterate over each field and the value of the hash, using HSCAN.

Implementation

def hscan_each(key, cursor = "0", match: nil, count: nil, &block)
	return enum_for(:hscan_each, key, cursor, match: match, count: count) unless block_given?

	while true
		cursor, data = hscan(key, cursor, match: match, count: count)

		data.each_slice(2, &block)

		break if cursor == "0"
	end
end