Commit ed9b544e authored by antirez's avatar antirez
Browse files

first commit

parents
require 'benchmark'
$:.push File.join(File.dirname(__FILE__), 'lib')
require 'redis'
times = 20000
@r = Redis.new
(0..1000000).each{|x|
@r[x] = "Hello World"
puts x if (x > 0 and x % 10000) == 0
}
#--
# = timeout.rb
#
# execution timeout
#
# = Copyright
#
# Copyright - (C) 2008 Evan Phoenix
# Copyright:: (C) 2000 Network Applied Communication Laboratory, Inc.
# Copyright:: (C) 2000 Information-technology Promotion Agency, Japan
#
#++
#
# = Description
#
# A way of performing a potentially long-running operation in a thread, and
# terminating it's execution if it hasn't finished within fixed amount of
# time.
#
# Previous versions of timeout didn't use a module for namespace. This version
# provides both Timeout.timeout, and a backwards-compatible #timeout.
#
# = Synopsis
#
# require 'timeout'
# status = Timeout::timeout(5) {
# # Something that should be interrupted if it takes too much time...
# }
#
require 'thread'
module Timeout
##
# Raised by Timeout#timeout when the block times out.
class Error<Interrupt
end
# A mutex to protect @requests
@mutex = Mutex.new
# All the outstanding TimeoutRequests
@requests = []
# Represents +thr+ asking for it to be timeout at in +secs+
# seconds. At timeout, raise +exc+.
class TimeoutRequest
def initialize(secs, thr, exc)
@left = secs
@thread = thr
@exception = exc
end
attr_reader :thread, :left
# Called because +time+ seconds have gone by. Returns
# true if the request has no more time left to run.
def elapsed(time)
@left -= time
@left <= 0
end
# Raise @exception if @thread.
def cancel
if @thread and @thread.alive?
@thread.raise @exception, "execution expired"
end
@left = 0
end
# Abort this request, ie, we don't care about tracking
# the thread anymore.
def abort
@thread = nil
@left = 0
end
end
def self.add_timeout(time, exc)
@controller ||= Thread.new do
while true
if @requests.empty?
sleep
next
end
min = nil
@mutex.synchronize do
min = @requests.min { |a,b| a.left <=> b.left }
end
slept_for = sleep(min.left)
@mutex.synchronize do
@requests.delete_if do |r|
if r.elapsed(slept_for)
r.cancel
true
else
false
end
end
end
end
end
req = TimeoutRequest.new(time, Thread.current, exc)
@mutex.synchronize do
@requests << req
end
@controller.run
return req
end
##
# Executes the method's block. If the block execution terminates before +sec+
# seconds has passed, it returns true. If not, it terminates the execution
# and raises +exception+ (which defaults to Timeout::Error).
#
# Note that this is both a method of module Timeout, so you can 'include
# Timeout' into your classes so they have a #timeout method, as well as a
# module method, so you can call it directly as Timeout.timeout().
def timeout(sec, exception=Error)
return yield if sec == nil or sec.zero?
raise ThreadError, "timeout within critical session" if Thread.critical
req = Timeout.add_timeout sec, exception
begin
yield sec
ensure
req.abort
end
end
module_function :timeout
end
##
# Identical to:
#
# Timeout::timeout(n, e, &block).
#
# Defined for backwards compatibility with earlier versions of timeout.rb, see
# Timeout#timeout.
def timeout(n, e=Timeout::Error, &block) # :nodoc:
Timeout::timeout(n, e, &block)
end
##
# Another name for Timeout::Error, defined for backwards compatibility with
# earlier versions of timeout.rb.
TimeoutError = Timeout::Error # :nodoc:
if __FILE__ == $0
p timeout(5) {
45
}
p timeout(5, TimeoutError) {
45
}
p timeout(nil) {
54
}
p timeout(0) {
54
}
p timeout(5) {
loop {
p 10
sleep 1
}
}
end
require 'redis'
require 'hash_ring'
class DistRedis
attr_reader :ring
def initialize(*servers)
srvs = []
servers.each do |s|
server, port = s.split(':')
srvs << Redis.new(:host => server, :port => port)
end
@ring = HashRing.new srvs
end
def node_for_key(key)
if key =~ /\{(.*)?\}/
key = $1
end
@ring.get_node(key)
end
def add_server(server)
server, port = server.split(':')
@ring.add_node Redis.new(:host => server, :port => port)
end
def method_missing(sym, *args, &blk)
if redis = node_for_key(args.first)
redis.send sym, *args, &blk
else
super
end
end
def keys(glob)
keyz = []
@ring.nodes.each do |red|
keyz.concat red.keys(glob)
end
keyz
end
def save
@ring.nodes.each do |red|
red.save
end
end
def bgsave
@ring.nodes.each do |red|
red.bgsave
end
end
def quit
@ring.nodes.each do |red|
red.quit
end
end
def delete_cloud!
@ring.nodes.each do |red|
red.keys("*").each do |key|
red.delete key
end
end
end
end
if __FILE__ == $0
r = DistRedis.new 'localhost:6379', 'localhost:6380', 'localhost:6381', 'localhost:6382'
r['urmom'] = 'urmom'
r['urdad'] = 'urdad'
r['urmom1'] = 'urmom1'
r['urdad1'] = 'urdad1'
r['urmom2'] = 'urmom2'
r['urdad2'] = 'urdad2'
r['urmom3'] = 'urmom3'
r['urdad3'] = 'urdad3'
p r['urmom']
p r['urdad']
p r['urmom1']
p r['urdad1']
p r['urmom2']
p r['urdad2']
p r['urmom3']
p r['urdad3']
r.push_tail 'listor', 'foo1'
r.push_tail 'listor', 'foo2'
r.push_tail 'listor', 'foo3'
r.push_tail 'listor', 'foo4'
r.push_tail 'listor', 'foo5'
p r.pop_tail 'listor'
p r.pop_tail 'listor'
p r.pop_tail 'listor'
p r.pop_tail 'listor'
p r.pop_tail 'listor'
puts "key distribution:"
r.ring.nodes.each do |red|
p [red.port, red.keys("*")]
end
r.delete_cloud!
p r.keys('*')
end
require 'digest/md5'
class HashRing
attr_reader :ring, :sorted_keys, :replicas, :nodes
# nodes is a list of objects that have a proper to_s representation.
# replicas indicates how many virtual points should be used pr. node,
# replicas are required to improve the distribution.
def initialize(nodes=[], replicas=3)
@replicas = replicas
@ring = {}
@nodes = []
@sorted_keys = []
nodes.each do |node|
add_node(node)
end
end
# Adds a `node` to the hash ring (including a number of replicas).
def add_node(node)
@nodes << node
@replicas.times do |i|
key = gen_key("#{node}:#{i}")
@ring[key] = node
@sorted_keys << key
end
@sorted_keys.sort!
end
def remove_node(node)
@replicas.times do |i|
key = gen_key("#{node}:#{count}")
@ring.delete(key)
@sorted_keys.reject! {|k| k == key}
end
end
# get the node in the hash ring for this key
def get_node(key)
get_node_pos(key)[0]
end
def get_node_pos(key)
return [nil,nil] if @ring.size == 0
key = gen_key(key)
nodes = @sorted_keys
nodes.size.times do |i|
node = nodes[i]
if key <= node
return [@ring[node], i]
end
end
[@ring[nodes[0]], 0]
end
def iter_nodes(key)
return [nil,nil] if @ring.size == 0
node, pos = get_node_pos(key)
@sorted_keys[pos..-1].each do |k|
yield @ring[k]
end
end
def gen_key(key)
key = Digest::MD5.hexdigest(key)
((key[3] << 24) | (key[2] << 16) | (key[1] << 8) | key[0])
end
end
# ring = HashRing.new ['server1', 'server2', 'server3']
# p ring
# #
# p ring.get_node "kjhjkjlkjlkkh"
#
\ No newline at end of file
require 'socket'
require File.join(File.dirname(__FILE__),'better_timeout')
require 'set'
class RedisError < StandardError
end
class Redis
OK = "+OK".freeze
ERRCODE = "-".freeze
NIL = 'nil'.freeze
CTRLF = "\r\n".freeze
def to_s
"#{host}:#{port}"
end
def port
@opts[:port]
end
def host
@opts[:host]
end
def initialize(opts={})
@opts = {:host => 'localhost', :port => '6379'}.merge(opts)
end
# SET key value
# Time complexity: O(1)
# Set the string value as value of the key. The string can't be longer
# than 1073741824 bytes (1 GB).
#
# Return value: status code reply
def []=(key, val)
val = redis_marshal(val)
timeout_retry(3, 3){
write "SET #{key} #{val.to_s.size}\r\n#{val}\r\n"
status_code_reply
}
end
# SETNX key value
#
# Time complexity: O(1)
# SETNX works exactly like SET with the only difference that if the key
# already exists no operation is performed. SETNX actually means "SET if Not eXists".
#
# *Return value: integer reply, specifically:
#
# 1 if the key was set 0 if the key was not set
def set_unless_exists(key, val)
val = redis_marshal(val)
timeout_retry(3, 3){
write "SETNX #{key} #{val.to_s.size}\r\n#{val}\r\n"
integer_reply == 1
}
end
# GET key
# Time complexity: O(1)
# Get the value of the specified key. If the key does not exist the special value
# 'nil' is returned. If the value stored at key is not a string an error is
# returned because GET can only handle string values.
#
# Return value: bulk reply
def [](key)
timeout_retry(3, 3){
write "GET #{key}\r\n"
redis_unmarshal(bulk_reply)
}
end
# INCR key
# INCRBY key value
# Time complexity: O(1)
# Increment the number stored at key by one. If the key does not exist or contains
# a value of a wrong type, set the key to the value of "1" (like if the previous
# value was zero).
#
# INCRBY works just like INCR but instead to increment by 1 the increment is value.
#
# Return value: integer reply
def incr(key, increment=nil)
timeout_retry(3, 3){
if increment
write "INCRBY #{key} #{increment}\r\n"
else
write "INCR #{key}\r\n"
end
integer_reply
}
end
# DECR key
#
# DECRBY key value
#
# Time complexity: O(1) Like INCR/INCRBY but decrementing instead of incrementing.
def decr(key, increment=nil)
timeout_retry(3, 3){
if increment
write "DECRBY #{key} #{increment}\r\n"
else
write "DECR #{key}\r\n"
end
integer_reply
}
end
# RANDOMKEY
# Time complexity: O(1)
# Returns a random key from the currently seleted DB.
#
# Return value: single line reply
def randkey
timeout_retry(3, 3){
write "RANDOMKEY\r\n"
single_line_reply
}
end
# RENAME oldkey newkey
#
# Atomically renames the key oldkey to newkey. If the source and destination
# name are the same an error is returned. If newkey already exists it is
# overwritten.
#
# Return value: status code reply
def rename!(oldkey, newkey)
timeout_retry(3, 3){
write "RENAME #{oldkey} #{newkey}\r\n"
status_code_reply
}
end
# RENAMENX oldkey newkey
# Just like RENAME but fails if the destination key newkey already exists.
#
# *Return value: integer reply, specifically:
#
# 1 if the key was renamed 0 if the target key already exist -1 if the
# source key does not exist -3 if source and destination keys are the same
def rename(oldkey, newkey)
timeout_retry(3, 3){
write "RENAMENX #{oldkey} #{newkey}\r\n"
case integer_reply
when -1
raise RedisError, "source key: #{oldkey} does not exist"
when 0
raise RedisError, "target key: #{oldkey} already exists"
when -3
raise RedisError, "source and destination keys are the same"
when 1
true
end
}
end
# EXISTS key
# Time complexity: O(1)
# Test if the specified key exists. The command returns "0" if the key
# exists, otherwise "1" is returned. Note that even keys set with an empty
# string as value will return "1".
#
# *Return value: integer reply, specifically:
#
# 1 if the key exists 0 if the key does not exist
def key?(key)
timeout_retry(3, 3){
write "EXISTS #{key}\r\n"
integer_reply == 1
}
end
# DEL key
# Time complexity: O(1)
# Remove the specified key. If the key does not exist no operation is
# performed. The command always returns success.
#
# *Return value: integer reply, specifically:
#
# 1 if the key was removed 0 if the key does not exist
def delete(key)
timeout_retry(3, 3){
write "DEL #{key}\r\n"
integer_reply == 1
}
end
# KEYS pattern
# Time complexity: O(n) (with n being the number of keys in the DB)
# Returns all the keys matching the glob-style pattern as space separated strings.
# For example if you have in the database the keys "foo" and "foobar" the command
# "KEYS foo*" will return "foo foobar".
#
# Note that while the time complexity for this operation is O(n) the constant times
# are pretty low. For example Redis running on an entry level laptop can scan a 1
# million keys database in 40 milliseconds. Still it's better to consider this one
# of the slow commands that may ruin the DB performance if not used with care.
#
# Return value: bulk reply
def keys(glob)
timeout_retry(3, 3){
write "KEYS #{glob}\r\n"
bulk_reply.split(' ')
}
end
# TYPE key
#
# Time complexity: O(1) Return the type of the value stored at key in form of
# a string. The type can be one of "none", "string", "list", "set". "none" is
# returned if the key does not exist.
#
# Return value: single line reply
def type?(key)
timeout_retry(3, 3){
write "TYPE #{key}\r\n"
single_line_reply
}
end
# RPUSH key string
#
# Time complexity: O(1)
# Add the given string to the tail of the list contained at key. If the key
# does not exist an empty list is created just before the append operation.
# If the key exists but is not a List an error is returned.
#
# Return value: status code reply
def push_tail(key, string)
timeout_retry(3, 3){
write "RPUSH #{key} #{string.to_s.size}\r\n#{string.to_s}\r\n"
status_code_reply
}
end
# LPUSH key string
# Time complexity: O(1)
# Add the given string to the head of the list contained at key. If the
# key does not exist an empty list is created just before the append operation.
# If the key exists but is not a List an error is returned.
#
# Return value: status code reply
def push_head(key, string)
timeout_retry(3, 3){
write "LPUSH #{key} #{string.to_s.size}\r\n#{string.to_s}\r\n"
status_code_reply
}
end
# LPOP key
#
# Time complexity: O(1)
# Atomically return and remove the first element of the list. For example if
# the list contains the elements "a","b","c" LPOP will return "a" and the
# list will become "b","c".
#
# If the key does not exist or the list is already empty the special value
# 'nil' is returned.
#
# Return value: bulk reply
def pop_head(key)
timeout_retry(3, 3){
write "LPOP #{key}\r\n"
bulk_reply
}
end
# RPOP key
# This command works exactly like LPOP, but the last element instead
# of the first element of the list is returned/deleted.
def pop_tail(key)
timeout_retry(3, 3){
write "RPOP #{key}\r\n"
bulk_reply
}
end
# LSET key index value
# Time complexity: O(N) (with N being the length of the list)
# Set the list element at index (see LINDEX for information about the index argument) with the new value. Out of range indexes will generate an error. Note that setting the first or last elements of the list is O(1).
#
# Return value: status code reply
def list_set(key, index, val)
timeout_retry(3, 3){
write "LSET #{key} #{index} #{val.to_s.size}\r\n#{val}\r\n"
status_code_reply
}
end
# LLEN key
# Time complexity: O(1)
# Return the length of the list stored at the specified key. If the key does not
# exist zero is returned (the same behaviour as for empty lists). If the value
# stored at key is not a list the special value -1 is returned. Note: client
# library should raise an exception when -1 is returned instead to pass the
# value back to the caller like a normal list length value.
#
# *Return value: integer reply, specifically:
#
# the length of the list as an integer
# >=
# 0 if the operation succeeded -2 if the specified key does not hold a list valu
def list_length(key)
timeout_retry(3, 3){
write "LLEN #{key}\r\n"
case i = integer_reply
when -2
raise RedisError, "key: #{key} does not hold a list value"
else
i
end
}
end
# LRANGE key start end
# Time complexity: O(n) (with n being the length of the range)
# Return the specified elements of the list stored at the specified key. Start
# and end are zero-based indexes. 0 is the first element of the list (the list head),
# 1 the next element and so on.
#
# For example LRANGE foobar 0 2 will return the first three elements of the list.
#
# start and end can also be negative numbers indicating offsets from the end of the list.
# For example -1 is the last element of the list, -2 the penultimate element and so on.
#
# Indexes out of range will not produce an error: if start is over the end of the list,
# or start > end, an empty list is returned. If end is over the end of the list Redis
# will threat it just like the last element of the list.
#
# Return value: multi bulk reply
def list_range(key, start, ending)
timeout_retry(3, 3){
write "LRANGE #{key} #{start} #{ending}\r\n"
multi_bulk_reply
}
end
# LTRIM key start end
# Time complexity: O(n) (with n being len of list - len of range)
# Trim an existing list so that it will contain only the specified range of
# elements specified. Start and end are zero-based indexes. 0 is the first
# element of the list (the list head), 1 the next element and so on.
#
# For example LTRIM foobar 0 2 will modify the list stored at foobar key so that
# only the first three elements of the list will remain.
#
# start and end can also be negative numbers indicating offsets from the end of
# the list. For example -1 is the last element of the list, -2 the penultimate
# element and so on.
#
# Indexes out of range will not produce an error: if start is over the end of
# the list, or start > end, an empty list is left as value. If end over the
# end of the list Redis will threat it just like the last element of the list.
#
# Hint: the obvious use of LTRIM is together with LPUSH/RPUSH. For example:
#
# LPUSH mylist <someelement> LTRIM mylist 0 99
# The above two commands will push elements in the list taking care that the
# list will not grow without limits. This is very useful when using Redis
# to store logs for example. It is important to note that when used in this
# way LTRIM is an O(1) operation because in the average case just one element
# is removed from the tail of the list.
#
# Return value: status code reply
def list_trim(key, start, ending)
timeout_retry(3, 3){
write "LTRIM #{key} #{start} #{ending}\r\n"
status_code_reply
}
end
# LINDEX key index
# Time complexity: O(n) (with n being the length of the list)
# Return the specified element of the list stored at the specified key. 0 is
# the first element, 1 the second and so on. Negative indexes are supported,
# for example -1 is the last element, -2 the penultimate and so on.
#
# If the value stored at key is not of list type an error is returned. If
# the index is out of range an empty string is returned.
#
# Note that even if the average time complexity is O(n) asking for the first
# or the last element of the list is O(1).
#
# Return value: bulk reply
def list_index(key, index)
timeout_retry(3, 3){
write "LINDEX #{key} #{index}\r\n"
bulk_reply
}
end
# SADD key member
# Time complexity O(1)
# Add the specified member to the set value stored at key. If member is
# already a member of the set no operation is performed. If key does not
# exist a new set with the specified member as sole member is crated. If
# the key exists but does not hold a set value an error is returned.
#
# *Return value: integer reply, specifically:
#
# 1 if the new element was added 0 if the new element was already a member
# of the set -2 if the key contains a non set value
def set_add(key, member)
timeout_retry(3, 3){
write "SADD #{key} #{member.to_s.size}\r\n#{member}\r\n"
case integer_reply
when 1
true
when 0
false
when -2
raise RedisError, "key: #{key} contains a non set value"
end
}
end
# SREM key member
#
# Time complexity O(1)
# Remove the specified member from the set value stored at key. If member
# was not a member of the set no operation is performed. If key does not
# exist or does not hold a set value an error is returned.
#
# *Return value: integer reply, specifically:
#
# 1 if the new element was removed 0 if the new element was not a member
# of the set -2 if the key does not hold a set value
def set_delete(key, member)
timeout_retry(3, 3){
write "SREM #{key} #{member.to_s.size}\r\n#{member}\r\n"
case integer_reply
when 1
true
when 0
false
when -2
raise RedisError, "key: #{key} contains a non set value"
end
}
end
# SCARD key
# Time complexity O(1)
# Return the set cardinality (number of elements). If the key does not
# exist 0 is returned, like for empty sets. If the key does not hold a
# set value -1 is returned. Client libraries should raise an error when -1
# is returned instead to pass the value to the caller.
#
# *Return value: integer reply, specifically:
#
# the cardinality (number of elements) of the set as an integer
# >=
# 0 if the operation succeeded -2 if the specified key does not hold a set value
def set_count(key)
timeout_retry(3, 3){
write "SCARD #{key}\r\n"
case i = integer_reply
when -2
raise RedisError, "key: #{key} contains a non set value"
else
i
end
}
end
# SISMEMBER key member
#
# Time complexity O(1)
# Return 1 if member is a member of the set stored at key, otherwise 0 is
# returned. On error a negative value is returned. Client libraries should
# raise an error when a negative value is returned instead to pass the value
# to the caller.
#
# *Return value: integer reply, specifically:
#
# 1 if the element is a member of the set 0 if the element is not a member of
# the set OR if the key does not exist -2 if the key does not hold a set value
def set_member?(key, member)
timeout_retry(3, 3){
write "SISMEMBER #{key} #{member.to_s.size}\r\n#{member}\r\n"
case integer_reply
when 1
true
when 0
false
when -2
raise RedisError, "key: #{key} contains a non set value"
end
}
end
# SINTER key1 key2 ... keyN
# Time complexity O(N*M) worst case where N is the cardinality of the smallest
# set and M the number of sets
# Return the members of a set resulting from the intersection of all the sets
# hold at the specified keys. Like in LRANGE the result is sent to the client
# as a multi-bulk reply (see the protocol specification for more information).
# If just a single key is specified, then this command produces the same
# result as SELEMENTS. Actually SELEMENTS is just syntax sugar for SINTERSECT.
#
# If at least one of the specified keys does not exist or does not hold a set
# value an error is returned.
#
# Return value: multi bulk reply
def set_intersect(*keys)
timeout_retry(3, 3){
write "SINTER #{keys.join(' ')}\r\n"
Set.new(multi_bulk_reply)
}
end
# SINTERSTORE dstkey key1 key2 ... keyN
#
# Time complexity O(N*M) worst case where N is the cardinality of the smallest set and M the number of sets
# This commnad works exactly like SINTER but instead of being returned the resulting set is sotred as dstkey.
#
# Return value: status code reply
def set_inter_store(destkey, *keys)
timeout_retry(3, 3){
write "SINTERSTORE #{destkey} #{keys.join(' ')}\r\n"
status_code_reply
}
end
# SMEMBERS key
#
# Time complexity O(N)
# Return all the members (elements) of the set value stored at key.
# This is just syntax glue for SINTERSECT.
def set_members(key)
timeout_retry(3, 3){
write "SMEMBERS #{key}\r\n"
Set.new(multi_bulk_reply)
}
end
# SORT key [BY pattern] [GET|DEL|INCR|DECR pattern] [ASC|DESC] [LIMIT start count]
# Sort the elements contained in the List or Set value at key. By default sorting is
# numeric with elements being compared as double precision floating point numbers.
# This is the simplest form of SORT.
# SORT mylist
#
# Assuming mylist contains a list of numbers, the return value will be the list of
# numbers ordered from the smallest to the bigger number. In order to get the sorting
# in reverse order use DESC:
# SORT mylist DESC
#
# ASC is also supported but it's the default so you don't really need it. If you
# want to sort lexicographically use ALPHA. Note that Redis is utf-8 aware
# assuming you set the right value for the LC_COLLATE environment variable.
#
# Sort is able to limit the number of results using the LIMIT option:
# SORT mylist LIMIT 0 10
# In the above example SORT will return only 10 elements, starting from the first one
# (star is zero-based). Almost all the sort options can be mixed together. For example:
# SORT mylist LIMIT 0 10 ALPHA DESC
# Will sort mylist lexicographically, in descending order, returning only the first
# 10 elements.
# Sometimes you want to sort elements using external keys as weights to compare
# instead to compare the actual List or Set elements. For example the list mylist
# may contain the elements 1, 2, 3, 4, that are just the unique IDs of objects
# stored at object_1, object_2, object_3 and object_4, while the keys weight_1,
# weight_2, weight_3 and weight_4 can contain weights we want to use to sort the
# list of objects identifiers. We can use the following command:
# SORT mylist BY weight_*
# the BY option takes a pattern (weight_* in our example) that is used in order to
# generate the key names of the weights used for sorting. Weight key names are obtained
# substituting the first occurrence of * with the actual value of the elements on the
# list (1,2,3,4 in our example).
# Still our previous example will return just the sorted IDs. Often it is needed to
# get the actual objects sorted (object_1, ..., object_4 in the example). We can do
# it with the following command:
# SORT mylist BY weight_* GET object_*
# Note that GET can be used multiple times in order to get more key for every
# element of the original List or Set sorted.
# redis.sort 'index', :by => 'weight_*',
# :order => 'DESC ALPHA',
# :limit => [0,10],
# :get => 'obj_*'
def sort(key, opts={})
cmd = "SORT #{key}"
cmd << " BY #{opts[:by]}" if opts[:by]
cmd << " GET #{opts[:get]}" if opts[:get]
cmd << " INCR #{opts[:incr]}" if opts[:incr]
cmd << " DEL #{opts[:del]}" if opts[:del]
cmd << " DECR #{opts[:decr]}" if opts[:decr]
cmd << " #{opts[:order]}" if opts[:order]
cmd << " LIMIT #{opts[:limit].join(' ')}" if opts[:limit]
cmd << "\r\n"
write cmd
multi_bulk_reply
end
# ADMIN functions for redis
# SELECT index
#
# Select the DB with having the specified zero-based numeric index.
# For default every new client connection is automatically selected to DB 0.
# Return value: status code reply
def select_db(index)
timeout_retry(3, 3){
write "SELECT #{index}\r\n"
status_code_reply
}
end
# MOVE key dbindex
#
# Move the specified key from the currently selected DB to the specified
# destination DB. Note that this command returns 1 only if the key was
# successfully moved, and 0 if the target key was already there or if
# the source key was not found at all, so it is possible to use MOVE
# as a locking primitive.
#
# *Return value: integer reply, specifically:
#
# 1 if the key was moved 0 if the key was not moved because already
# present on the target DB or was not found in the current DB. -3
# if the destination DB is the same as the source DB -4 if the database
# index if out of range
def move(key, index)
timeout_retry(3, 3){
write "MOVE #{index}\r\n"
case integer_reply
when 1
true
when 0
false
when -3
raise RedisError, "destination db same as source db"
when -4
raise RedisError, "db index if out of range"
end
}
end
# SAVE
#
# Save the DB on disk. The server hangs while the saving is not completed,
# no connection is served in the meanwhile. An OK code is returned when
# the DB was fully stored in disk.
# Return value: status code reply
def save
timeout_retry(3, 3){
write "SAVE\r\n"
status_code_reply
}
end
# BGSAVE
#
# Save the DB in background. The OK code is immediately returned. Redis
# forks, the parent continues to server the clients, the child saves
# the DB on disk then exit. A client my be able to check if the operation
# succeeded using the LASTSAVE command.
# Return value: status code reply
def bgsave
timeout_retry(3, 3){
write "BGSAVE\r\n"
status_code_reply
}
end
# LASTSAVE
#
# Return the UNIX TIME of the last DB save executed with success. A client
# may check if a BGSAVE command succeeded reading the LASTSAVE value, then
# issuing a BGSAVE command and checking at regular intervals every N seconds
# if LASTSAVE changed.
#
# Return value: integer reply (UNIX timestamp)
def lastsave
timeout_retry(3, 3){
write "LASTSAVE\r\n"
integer_reply
}
end
def quit
timeout_retry(3, 3){
write "QUIT\r\n"
status_code_reply
}
end
private
def redis_unmarshal(obj)
if obj[0] == 4
Marshal.load(obj)
else
obj
end
end
def redis_marshal(obj)
case obj
when String, Integer
obj
else
Marshal.dump(obj)
end
end
def close
socket.close unless socket.closed?
end
def timeout_retry(time, retries, &block)
timeout(time, &block)
rescue TimeoutError
retries -= 1
retry unless retries < 0
end
def socket
connect if (!@socket or @socket.closed?)
@socket
end
def connect
@socket = TCPSocket.new(@opts[:host], @opts[:port])
@socket.sync = true
@socket
end
def read(length, nodebug=true)
retries = 3
res = socket.read(length)
puts "read: #{res}" if @opts[:debug] && nodebug
res
rescue
retries -= 1
if retries > 0
connect
retry
end
end
def write(data)
puts "write: #{data}" if @opts[:debug]
retries = 3
socket.write(data)
rescue
retries -= 1
if retries > 0
connect
retry
end
end
def nibble_end
read(2)
end
def read_proto
print "read proto: " if @opts[:debug]
buff = ""
while (char = read(1, false))
print char if @opts[:debug]
buff << char
break if buff[-2..-1] == CTRLF
end
puts if @opts[:debug]
buff[0..-3]
end
def status_code_reply
res = read_proto
if res.index(ERRCODE) == 0
raise RedisError, res
else
true
end
end
def bulk_reply
res = read_proto
if res.index(ERRCODE) == 0
err = read(res.to_i.abs)
nibble_end
raise RedisError, err
elsif res != NIL
val = read(res.to_i.abs)
nibble_end
val
else
nil
end
end
def multi_bulk_reply
res = read_proto
if res.index(ERRCODE) == 0
err = read(res.to_i.abs)
nibble_end
raise RedisError, err
elsif res == NIL
nil
else
items = Integer(res)
list = []
items.times do
len = Integer(read_proto)
if len == -1
nil
else
list << read(len)
end
nibble_end
end
list
end
end
def single_line_reply
read_proto
end
def integer_reply
Integer(read_proto)
end
end
require File.dirname(__FILE__) + '/spec_helper'
class Foo
attr_accessor :bar
def initialize(bar)
@bar = bar
end
def ==(other)
@bar == other.bar
end
end
describe "redis" do
before do
@r = Redis.new
@r.select_db(15) # use database 15 for testing so we dont accidentally step on you real data
@r['foo'] = 'bar'
end
after do
@r.keys('*').each {|k| @r.delete k }
end
it "should properly marshall objects" do
class MyFail; def fail; 'it will' end; end
@r['fail'] = MyFail.new
@r['fail'].fail.should == 'it will'
end
it "should be able to GET a key" do
@r['foo'].should == 'bar'
end
it "should be able to SET a key" do
@r['foo'] = 'nik'
@r['foo'].should == 'nik'
end
it "should be able to SETNX(set_unless_exists)" do
@r['foo'] = 'nik'
@r['foo'].should == 'nik'
@r.set_unless_exists 'foo', 'bar'
@r['foo'].should == 'nik'
end
it "should be able to INCR(increment) a key" do
@r.delete('counter')
@r.incr('counter').should == 1
@r.incr('counter').should == 2
@r.incr('counter').should == 3
end
it "should be able to DECR(decrement) a key" do
@r.delete('counter')
@r.incr('counter').should == 1
@r.incr('counter').should == 2
@r.incr('counter').should == 3
@r.decr('counter').should == 2
@r.decr('counter').should == 1
@r.decr('counter').should == 0
end
it "should be able to RANDKEY(return a random key)" do
@r.randkey.should_not be_nil
end
it "should be able to RENAME a key" do
@r.delete 'foo'
@r.delete 'bar'
@r['foo'] = 'hi'
@r.rename! 'foo', 'bar'
@r['bar'].should == 'hi'
end
it "should be able to RENAMENX(rename unless the new key already exists) a key" do
@r.delete 'foo'
@r.delete 'bar'
@r['foo'] = 'hi'
@r['bar'] = 'ohai'
lambda {@r.rename 'foo', 'bar'}.should raise_error(RedisError)
@r['bar'].should == 'ohai'
end
it "should be able to EXISTS(check if key exists)" do
@r['foo'] = 'nik'
@r.key?('foo').should be_true
@r.delete 'foo'
@r.key?('foo').should be_false
end
it "should be able to KEYS(glob for keys)" do
@r.keys("f*").each do |key|
@r.delete key
end
@r['f'] = 'nik'
@r['fo'] = 'nak'
@r['foo'] = 'qux'
@r.keys("f*").sort.should == ['f','fo', 'foo'].sort
end
it "should be able to check the TYPE of a key" do
@r['foo'] = 'nik'
@r.type?('foo').should == "string"
@r.delete 'foo'
@r.type?('foo').should == "none"
end
it "should be able to push to the head of a list" do
@r.push_head "list", 'hello'
@r.push_head "list", 42
@r.type?('list').should == "list"
@r.list_length('list').should == 2
@r.pop_head('list').should == '42'
@r.delete('list')
end
it "should be able to push to the tail of a list" do
@r.push_tail "list", 'hello'
@r.type?('list').should == "list"
@r.list_length('list').should == 1
@r.delete('list')
end
it "should be able to pop the tail of a list" do
@r.push_tail "list", 'hello'
@r.push_tail "list", 'goodbye'
@r.type?('list').should == "list"
@r.list_length('list').should == 2
@r.pop_tail('list').should == 'goodbye'
@r.delete('list')
end
it "should be able to pop the head of a list" do
@r.push_tail "list", 'hello'
@r.push_tail "list", 'goodbye'
@r.type?('list').should == "list"
@r.list_length('list').should == 2
@r.pop_head('list').should == 'hello'
@r.delete('list')
end
it "should be able to get the length of a list" do
@r.push_tail "list", 'hello'
@r.push_tail "list", 'goodbye'
@r.type?('list').should == "list"
@r.list_length('list').should == 2
@r.delete('list')
end
it "should be able to get a range of values from a list" do
@r.push_tail "list", 'hello'
@r.push_tail "list", 'goodbye'
@r.push_tail "list", '1'
@r.push_tail "list", '2'
@r.push_tail "list", '3'
@r.type?('list').should == "list"
@r.list_length('list').should == 5
@r.list_range('list', 2, -1).should == ['1', '2', '3']
@r.delete('list')
end
it "should be able to trim a list" do
@r.push_tail "list", 'hello'
@r.push_tail "list", 'goodbye'
@r.push_tail "list", '1'
@r.push_tail "list", '2'
@r.push_tail "list", '3'
@r.type?('list').should == "list"
@r.list_length('list').should == 5
@r.list_trim 'list', 0, 1
@r.list_length('list').should == 2
@r.list_range('list', 0, -1).should == ['hello', 'goodbye']
@r.delete('list')
end
it "should be able to get a value by indexing into a list" do
@r.push_tail "list", 'hello'
@r.push_tail "list", 'goodbye'
@r.type?('list').should == "list"
@r.list_length('list').should == 2
@r.list_index('list', 1).should == 'goodbye'
@r.delete('list')
end
it "should be able to set a value by indexing into a list" do
@r.push_tail "list", 'hello'
@r.push_tail "list", 'hello'
@r.type?('list').should == "list"
@r.list_length('list').should == 2
@r.list_set('list', 1, 'goodbye').should be_true
@r.list_index('list', 1).should == 'goodbye'
@r.delete('list')
end
it "should be able add members to a set" do
@r.set_add "set", 'key1'
@r.set_add "set", 'key2'
@r.type?('set').should == "set"
@r.set_count('set').should == 2
@r.set_members('set').sort.should == ['key1', 'key2'].sort
@r.delete('set')
end
it "should be able delete members to a set" do
@r.set_add "set", 'key1'
@r.set_add "set", 'key2'
@r.type?('set').should == "set"
@r.set_count('set').should == 2
@r.set_members('set').should == Set.new(['key1', 'key2'])
@r.set_delete('set', 'key1')
@r.set_count('set').should == 1
@r.set_members('set').should == Set.new(['key2'])
@r.delete('set')
end
it "should be able count the members of a set" do
@r.set_add "set", 'key1'
@r.set_add "set", 'key2'
@r.type?('set').should == "set"
@r.set_count('set').should == 2
@r.delete('set')
end
it "should be able test for set membership" do
@r.set_add "set", 'key1'
@r.set_add "set", 'key2'
@r.type?('set').should == "set"
@r.set_count('set').should == 2
@r.set_member?('set', 'key1').should be_true
@r.set_member?('set', 'key2').should be_true
@r.set_member?('set', 'notthere').should be_false
@r.delete('set')
end
it "should be able to do set intersection" do
@r.set_add "set", 'key1'
@r.set_add "set", 'key2'
@r.set_add "set2", 'key2'
@r.set_intersect('set', 'set2').should == Set.new(['key2'])
@r.delete('set')
end
it "should be able to do set intersection and store the results in a key" do
@r.set_add "set", 'key1'
@r.set_add "set", 'key2'
@r.set_add "set2", 'key2'
@r.set_inter_store('newone', 'set', 'set2')
@r.set_members('newone').should == Set.new(['key2'])
@r.delete('set')
end
it "should be able to do crazy SORT queries" do
@r['dog_1'] = 'louie'
@r.push_tail 'dogs', 1
@r['dog_2'] = 'lucy'
@r.push_tail 'dogs', 2
@r['dog_3'] = 'max'
@r.push_tail 'dogs', 3
@r['dog_4'] = 'taj'
@r.push_tail 'dogs', 4
@r.sort('dogs', :get => 'dog_*', :limit => [0,1]).should == ['louie']
@r.sort('dogs', :get => 'dog_*', :limit => [0,1], :order => 'desc alpha').should == ['taj']
end
end
\ No newline at end of file
require 'rubygems'
$TESTING=true
$:.push File.join(File.dirname(__FILE__), '..', 'lib')
require 'redis'
# Inspired by rabbitmq.rake the Redbox project at http://github.com/rick/redbox/tree/master
require 'fileutils'
class RedisRunner
def self.redisdir
"/tmp/redis/"
end
def self.redisconfdir
'/etc/redis.conf'
end
def self.dtach_socket
'/tmp/redis.dtach'
end
# Just check for existance of dtach socket
def self.running?
File.exists? dtach_socket
end
def self.start
puts 'Detach with Ctrl+\ Re-attach with rake redis:attach'
sleep 3
exec "dtach -A #{dtach_socket} redis-server #{redisconfdir}"
end
def self.attach
exec "dtach -a #{dtach_socket}"
end
def self.stop
sh 'killall redis-server'
end
end
namespace :redis do
desc 'About redis'
task :about do
puts "\nSee http://code.google.com/p/redis/ for information about redis.\n\n"
end
desc 'Start redis'
task :start do
RedisRunner.start
end
desc 'Stop redis'
task :stop do
RedisRunner.stop
end
desc 'Attach to redis dtach socket'
task :attach do
RedisRunner.attach
end
desc 'Install the lastest redis from svn'
task :install => [:about, :download, :make] do
sh 'sudo cp /tmp/redis/redis-server /usr/bin/'
sh 'sudo cp /tmp/redis/redis-benchmark /usr/bin/'
puts 'Installed redis-server and redis-benchmark to /usr/bin/'
unless File.exists?('/etc/redis.conf')
sh 'sudo cp /tmp/redis/redis.conf /etc/'
puts "Installed redis.conf to /etc/ \n You should look at this file!"
end
end
task :make do
sh "cd #{RedisRunner.redisdir} && make clean"
sh "cd #{RedisRunner.redisdir} && make"
end
desc "Download package"
task :download do
system 'svn checkout http://redis.googlecode.com/svn/trunk /tmp/redis' unless File.exists?(RedisRunner.redisdir)
system 'svn up' if File.exists?("#{RedisRunner.redisdir}/.svn")
end
end
namespace :dtach do
desc 'About dtach'
task :about do
puts "\nSee http://dtach.sourceforge.net/ for information about dtach.\n\n"
end
desc 'Install dtach 0.8 from source'
task :install => [:about] do
Dir.chdir('/tmp/')
unless File.exists?('/tmp/dtach-0.8.tar.gz')
require 'net/http'
Net::HTTP.start('superb-west.dl.sourceforge.net') do |http|
resp = http.get('/sourceforge/dtach/dtach-0.8.tar.gz')
open('/tmp/dtach-0.8.tar.gz', 'wb') do |file| file.write(resp.body) end
end
end
unless File.directory?('/tmp/dtach-0.8')
system('tar xzf dtach-0.8.tar.gz')
end
Dir.chdir('/tmp/dtach-0.8/')
sh 'cd /tmp/dtach-0.8/ && ./configure && make'
sh 'sudo cp /tmp/dtach-0.8/dtach /usr/bin/'
puts 'Dtach successfully installed to /usr/bin.'
end
end
\ No newline at end of file
/* Hash Tables Implementation.
*
* This file implements in memory hash tables with insert/del/replace/find/
* get-random-element operations. Hash tables will auto resize if needed
* tables of power of two in size are used, collisions are handled by
* chaining. See the source code for more information... :)
*
* Copyright (c) 2006-2009, Salvatore Sanfilippo <antirez at gmail dot com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#include "dict.h"
#include "zmalloc.h"
/* ---------------------------- Utility funcitons --------------------------- */
static void _dictPanic(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "\nDICT LIBRARY PANIC: ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n\n");
va_end(ap);
}
/* ------------------------- Heap Management Wrappers------------------------ */
static void *_dictAlloc(int size)
{
void *p = zmalloc(size);
if (p == NULL)
_dictPanic("Out of memory");
return p;
}
static void _dictFree(void *ptr) {
zfree(ptr);
}
/* -------------------------- private prototypes ---------------------------- */
static int _dictExpandIfNeeded(dict *ht);
static unsigned int _dictNextPower(unsigned int size);
static int _dictKeyIndex(dict *ht, const void *key);
static int _dictInit(dict *ht, dictType *type, void *privDataPtr);
/* -------------------------- hash functions -------------------------------- */
/* Thomas Wang's 32 bit Mix Function */
unsigned int dictIntHashFunction(unsigned int key)
{
key += ~(key << 15);
key ^= (key >> 10);
key += (key << 3);
key ^= (key >> 6);
key += ~(key << 11);
key ^= (key >> 16);
return key;
}
/* Identity hash function for integer keys */
unsigned int dictIdentityHashFunction(unsigned int key)
{
return key;
}
/* Generic hash function (a popular one from Bernstein).
* I tested a few and this was the best. */
unsigned int dictGenHashFunction(const unsigned char *buf, int len) {
unsigned int hash = 5381;
while (len--)
hash = ((hash << 5) + hash) + (*buf++); /* hash * 33 + c */
return hash;
}
/* ----------------------------- API implementation ------------------------- */
/* Reset an hashtable already initialized with ht_init().
* NOTE: This function should only called by ht_destroy(). */
static void _dictReset(dict *ht)
{
ht->table = NULL;
ht->size = 0;
ht->sizemask = 0;
ht->used = 0;
}
/* Create a new hash table */
dict *dictCreate(dictType *type,
void *privDataPtr)
{
dict *ht = _dictAlloc(sizeof(*ht));
_dictInit(ht,type,privDataPtr);
return ht;
}
/* Initialize the hash table */
int _dictInit(dict *ht, dictType *type,
void *privDataPtr)
{
_dictReset(ht);
ht->type = type;
ht->privdata = privDataPtr;
return DICT_OK;
}
/* Resize the table to the minimal size that contains all the elements,
* but with the invariant of a USER/BUCKETS ration near to <= 1 */
int dictResize(dict *ht)
{
int minimal = ht->used;
if (minimal < DICT_HT_INITIAL_SIZE)
minimal = DICT_HT_INITIAL_SIZE;
return dictExpand(ht, minimal);
}
/* Expand or create the hashtable */
int dictExpand(dict *ht, unsigned int size)
{
dict n; /* the new hashtable */
unsigned int realsize = _dictNextPower(size), i;
/* the size is invalid if it is smaller than the number of
* elements already inside the hashtable */
if (ht->used > size)
return DICT_ERR;
_dictInit(&n, ht->type, ht->privdata);
n.size = realsize;
n.sizemask = realsize-1;
n.table = _dictAlloc(realsize*sizeof(dictEntry*));
/* Initialize all the pointers to NULL */
memset(n.table, 0, realsize*sizeof(dictEntry*));
/* Copy all the elements from the old to the new table:
* note that if the old hash table is empty ht->size is zero,
* so dictExpand just creates an hash table. */
n.used = ht->used;
for (i = 0; i < ht->size && ht->used > 0; i++) {
dictEntry *he, *nextHe;
if (ht->table[i] == NULL) continue;
/* For each hash entry on this slot... */
he = ht->table[i];
while(he) {
unsigned int h;
nextHe = he->next;
/* Get the new element index */
h = dictHashKey(ht, he->key) & n.sizemask;
he->next = n.table[h];
n.table[h] = he;
ht->used--;
/* Pass to the next element */
he = nextHe;
}
}
assert(ht->used == 0);
_dictFree(ht->table);
/* Remap the new hashtable in the old */
*ht = n;
return DICT_OK;
}
/* Add an element to the target hash table */
int dictAdd(dict *ht, void *key, void *val)
{
int index;
dictEntry *entry;
/* Get the index of the new element, or -1 if
* the element already exists. */
if ((index = _dictKeyIndex(ht, key)) == -1)
return DICT_ERR;
/* Allocates the memory and stores key */
entry = _dictAlloc(sizeof(*entry));
entry->next = ht->table[index];
ht->table[index] = entry;
/* Set the hash entry fields. */
dictSetHashKey(ht, entry, key);
dictSetHashVal(ht, entry, val);
ht->used++;
return DICT_OK;
}
/* Add an element, discarding the old if the key already exists */
int dictReplace(dict *ht, void *key, void *val)
{
dictEntry *entry;
/* Try to add the element. If the key
* does not exists dictAdd will suceed. */
if (dictAdd(ht, key, val) == DICT_OK)
return DICT_OK;
/* It already exists, get the entry */
entry = dictFind(ht, key);
/* Free the old value and set the new one */
dictFreeEntryVal(ht, entry);
dictSetHashVal(ht, entry, val);
return DICT_OK;
}
/* Search and remove an element */
static int dictGenericDelete(dict *ht, const void *key, int nofree)
{
unsigned int h;
dictEntry *he, *prevHe;
if (ht->size == 0)
return DICT_ERR;
h = dictHashKey(ht, key) & ht->sizemask;
he = ht->table[h];
prevHe = NULL;
while(he) {
if (dictCompareHashKeys(ht, key, he->key)) {
/* Unlink the element from the list */
if (prevHe)
prevHe->next = he->next;
else
ht->table[h] = he->next;
if (!nofree) {
dictFreeEntryKey(ht, he);
dictFreeEntryVal(ht, he);
}
_dictFree(he);
ht->used--;
return DICT_OK;
}
prevHe = he;
he = he->next;
}
return DICT_ERR; /* not found */
}
int dictDelete(dict *ht, const void *key) {
return dictGenericDelete(ht,key,0);
}
int dictDeleteNoFree(dict *ht, const void *key) {
return dictGenericDelete(ht,key,1);
}
/* Destroy an entire hash table */
int _dictClear(dict *ht)
{
unsigned int i;
/* Free all the elements */
for (i = 0; i < ht->size && ht->used > 0; i++) {
dictEntry *he, *nextHe;
if ((he = ht->table[i]) == NULL) continue;
while(he) {
nextHe = he->next;
dictFreeEntryKey(ht, he);
dictFreeEntryVal(ht, he);
_dictFree(he);
ht->used--;
he = nextHe;
}
}
/* Free the table and the allocated cache structure */
_dictFree(ht->table);
/* Re-initialize the table */
_dictReset(ht);
return DICT_OK; /* never fails */
}
/* Clear & Release the hash table */
void dictRelease(dict *ht)
{
_dictClear(ht);
_dictFree(ht);
}
dictEntry *dictFind(dict *ht, const void *key)
{
dictEntry *he;
unsigned int h;
if (ht->size == 0) return NULL;
h = dictHashKey(ht, key) & ht->sizemask;
he = ht->table[h];
while(he) {
if (dictCompareHashKeys(ht, key, he->key))
return he;
he = he->next;
}
return NULL;
}
dictIterator *dictGetIterator(dict *ht)
{
dictIterator *iter = _dictAlloc(sizeof(*iter));
iter->ht = ht;
iter->index = -1;
iter->entry = NULL;
iter->nextEntry = NULL;
return iter;
}
dictEntry *dictNext(dictIterator *iter)
{
while (1) {
if (iter->entry == NULL) {
iter->index++;
if (iter->index >=
(signed)iter->ht->size) break;
iter->entry = iter->ht->table[iter->index];
} else {
iter->entry = iter->nextEntry;
}
if (iter->entry) {
/* We need to save the 'next' here, the iterator user
* may delete the entry we are returning. */
iter->nextEntry = iter->entry->next;
return iter->entry;
}
}
return NULL;
}
void dictReleaseIterator(dictIterator *iter)
{
_dictFree(iter);
}
/* Return a random entry from the hash table. Useful to
* implement randomized algorithms */
dictEntry *dictGetRandomKey(dict *ht)
{
dictEntry *he;
unsigned int h;
int listlen, listele;
if (ht->size == 0) return NULL;
do {
h = random() & ht->sizemask;
he = ht->table[h];
} while(he == NULL);
/* Now we found a non empty bucket, but it is a linked
* list and we need to get a random element from the list.
* The only sane way to do so is to count the element and
* select a random index. */
listlen = 0;
while(he) {
he = he->next;
listlen++;
}
listele = random() % listlen;
he = ht->table[h];
while(listele--) he = he->next;
return he;
}
/* ------------------------- private functions ------------------------------ */
/* Expand the hash table if needed */
static int _dictExpandIfNeeded(dict *ht)
{
/* If the hash table is empty expand it to the intial size,
* if the table is "full" dobule its size. */
if (ht->size == 0)
return dictExpand(ht, DICT_HT_INITIAL_SIZE);
if (ht->used == ht->size)
return dictExpand(ht, ht->size*2);
return DICT_OK;
}
/* Our hash table capability is a power of two */
static unsigned int _dictNextPower(unsigned int size)
{
unsigned int i = DICT_HT_INITIAL_SIZE;
if (size >= 2147483648U)
return 2147483648U;
while(1) {
if (i >= size)
return i;
i *= 2;
}
}
/* Returns the index of a free slot that can be populated with
* an hash entry for the given 'key'.
* If the key already exists, -1 is returned. */
static int _dictKeyIndex(dict *ht, const void *key)
{
unsigned int h;
dictEntry *he;
/* Expand the hashtable if needed */
if (_dictExpandIfNeeded(ht) == DICT_ERR)
return -1;
/* Compute the key hash value */
h = dictHashKey(ht, key) & ht->sizemask;
/* Search if this slot does not already contain the given key */
he = ht->table[h];
while(he) {
if (dictCompareHashKeys(ht, key, he->key))
return -1;
he = he->next;
}
return h;
}
void dictEmpty(dict *ht) {
_dictClear(ht);
}
#define DICT_STATS_VECTLEN 50
void dictPrintStats(dict *ht) {
unsigned int i, slots = 0, chainlen, maxchainlen = 0;
unsigned int totchainlen = 0;
unsigned int clvector[DICT_STATS_VECTLEN];
if (ht->used == 0) {
printf("No stats available for empty dictionaries\n");
return;
}
for (i = 0; i < DICT_STATS_VECTLEN; i++) clvector[i] = 0;
for (i = 0; i < ht->size; i++) {
dictEntry *he;
if (ht->table[i] == NULL) {
clvector[0]++;
continue;
}
slots++;
/* For each hash entry on this slot... */
chainlen = 0;
he = ht->table[i];
while(he) {
chainlen++;
he = he->next;
}
clvector[(chainlen < DICT_STATS_VECTLEN) ? chainlen : (DICT_STATS_VECTLEN-1)]++;
if (chainlen > maxchainlen) maxchainlen = chainlen;
totchainlen += chainlen;
}
printf("Hash table stats:\n");
printf(" table size: %d\n", ht->size);
printf(" number of elements: %d\n", ht->used);
printf(" different slots: %d\n", slots);
printf(" max chain length: %d\n", maxchainlen);
printf(" avg chain length (counted): %.02f\n", (float)totchainlen/slots);
printf(" avg chain length (computed): %.02f\n", (float)ht->used/slots);
printf(" Chain length distribution:\n");
for (i = 0; i < DICT_STATS_VECTLEN-1; i++) {
if (clvector[i] == 0) continue;
printf(" %s%d: %d (%.02f%%)\n",(i == DICT_STATS_VECTLEN-1)?">= ":"", i, clvector[i], ((float)clvector[i]/ht->size)*100);
}
}
/* ----------------------- StringCopy Hash Table Type ------------------------*/
static unsigned int _dictStringCopyHTHashFunction(const void *key)
{
return dictGenHashFunction(key, strlen(key));
}
static void *_dictStringCopyHTKeyDup(void *privdata, const void *key)
{
int len = strlen(key);
char *copy = _dictAlloc(len+1);
DICT_NOTUSED(privdata);
memcpy(copy, key, len);
copy[len] = '\0';
return copy;
}
static void *_dictStringKeyValCopyHTValDup(void *privdata, const void *val)
{
int len = strlen(val);
char *copy = _dictAlloc(len+1);
DICT_NOTUSED(privdata);
memcpy(copy, val, len);
copy[len] = '\0';
return copy;
}
static int _dictStringCopyHTKeyCompare(void *privdata, const void *key1,
const void *key2)
{
DICT_NOTUSED(privdata);
return strcmp(key1, key2) == 0;
}
static void _dictStringCopyHTKeyDestructor(void *privdata, void *key)
{
DICT_NOTUSED(privdata);
_dictFree((void*)key); /* ATTENTION: const cast */
}
static void _dictStringKeyValCopyHTValDestructor(void *privdata, void *val)
{
DICT_NOTUSED(privdata);
_dictFree((void*)val); /* ATTENTION: const cast */
}
dictType dictTypeHeapStringCopyKey = {
_dictStringCopyHTHashFunction, /* hash function */
_dictStringCopyHTKeyDup, /* key dup */
NULL, /* val dup */
_dictStringCopyHTKeyCompare, /* key compare */
_dictStringCopyHTKeyDestructor, /* key destructor */
NULL /* val destructor */
};
/* This is like StringCopy but does not auto-duplicate the key.
* It's used for intepreter's shared strings. */
dictType dictTypeHeapStrings = {
_dictStringCopyHTHashFunction, /* hash function */
NULL, /* key dup */
NULL, /* val dup */
_dictStringCopyHTKeyCompare, /* key compare */
_dictStringCopyHTKeyDestructor, /* key destructor */
NULL /* val destructor */
};
/* This is like StringCopy but also automatically handle dynamic
* allocated C strings as values. */
dictType dictTypeHeapStringCopyKeyValue = {
_dictStringCopyHTHashFunction, /* hash function */
_dictStringCopyHTKeyDup, /* key dup */
_dictStringKeyValCopyHTValDup, /* val dup */
_dictStringCopyHTKeyCompare, /* key compare */
_dictStringCopyHTKeyDestructor, /* key destructor */
_dictStringKeyValCopyHTValDestructor, /* val destructor */
};
/* Hash Tables Implementation.
*
* This file implements in memory hash tables with insert/del/replace/find/
* get-random-element operations. Hash tables will auto resize if needed
* tables of power of two in size are used, collisions are handled by
* chaining. See the source code for more information... :)
*
* Copyright (c) 2006-2009, Salvatore Sanfilippo <antirez at gmail dot com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __DICT_H
#define __DICT_H
#define DICT_OK 0
#define DICT_ERR 1
/* Unused arguments generate annoying warnings... */
#define DICT_NOTUSED(V) ((void) V)
typedef struct dictEntry {
void *key;
void *val;
struct dictEntry *next;
} dictEntry;
typedef struct dictType {
unsigned int (*hashFunction)(const void *key);
void *(*keyDup)(void *privdata, const void *key);
void *(*valDup)(void *privdata, const void *obj);
int (*keyCompare)(void *privdata, const void *key1, const void *key2);
void (*keyDestructor)(void *privdata, void *key);
void (*valDestructor)(void *privdata, void *obj);
} dictType;
typedef struct dict {
dictEntry **table;
dictType *type;
unsigned int size;
unsigned int sizemask;
unsigned int used;
void *privdata;
} dict;
typedef struct dictIterator {
dict *ht;
int index;
dictEntry *entry, *nextEntry;
} dictIterator;
/* This is the initial size of every hash table */
#define DICT_HT_INITIAL_SIZE 16
/* ------------------------------- Macros ------------------------------------*/
#define dictFreeEntryVal(ht, entry) \
if ((ht)->type->valDestructor) \
(ht)->type->valDestructor((ht)->privdata, (entry)->val)
#define dictSetHashVal(ht, entry, _val_) do { \
if ((ht)->type->valDup) \
entry->val = (ht)->type->valDup((ht)->privdata, _val_); \
else \
entry->val = (_val_); \
} while(0)
#define dictFreeEntryKey(ht, entry) \
if ((ht)->type->keyDestructor) \
(ht)->type->keyDestructor((ht)->privdata, (entry)->key)
#define dictSetHashKey(ht, entry, _key_) do { \
if ((ht)->type->keyDup) \
entry->key = (ht)->type->keyDup((ht)->privdata, _key_); \
else \
entry->key = (_key_); \
} while(0)
#define dictCompareHashKeys(ht, key1, key2) \
(((ht)->type->keyCompare) ? \
(ht)->type->keyCompare((ht)->privdata, key1, key2) : \
(key1) == (key2))
#define dictHashKey(ht, key) (ht)->type->hashFunction(key)
#define dictGetEntryKey(he) ((he)->key)
#define dictGetEntryVal(he) ((he)->val)
#define dictGetHashTableSize(ht) ((ht)->size)
#define dictGetHashTableUsed(ht) ((ht)->used)
/* API */
dict *dictCreate(dictType *type, void *privDataPtr);
int dictExpand(dict *ht, unsigned int size);
int dictAdd(dict *ht, void *key, void *val);
int dictReplace(dict *ht, void *key, void *val);
int dictDelete(dict *ht, const void *key);
int dictDeleteNoFree(dict *ht, const void *key);
void dictRelease(dict *ht);
dictEntry * dictFind(dict *ht, const void *key);
int dictResize(dict *ht);
dictIterator *dictGetIterator(dict *ht);
dictEntry *dictNext(dictIterator *iter);
void dictReleaseIterator(dictIterator *iter);
dictEntry *dictGetRandomKey(dict *ht);
void dictPrintStats(dict *ht);
unsigned int dictGenHashFunction(const unsigned char *buf, int len);
void dictEmpty(dict *ht);
/* Hash table types */
extern dictType dictTypeHeapStringCopyKey;
extern dictType dictTypeHeapStrings;
extern dictType dictTypeHeapStringCopyKeyValue;
#endif /* __DICT_H */
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<link type="text/css" rel="stylesheet" href="style.css" />
</head>
<body>
<div id="page">
<div id='header'>
<a href="index.html">
<img style="border:none" alt="Redis Documentation" src="redis.png">
</a>
</div>
<div id="pagecontent">
<div class="index">
<!-- This is a (PRE) block. Make sure it's left aligned or your toc title will be off. -->
<b>Benchmarks: Contents</b><br>&nbsp;&nbsp;<a href="#How Fast is Redis?">How Fast is Redis?</a><br>&nbsp;&nbsp;<a href="#Latency percentiles">Latency percentiles</a>
</div>
<h1 class="wikiname">Benchmarks</h1>
<div class="summary">
</div>
<div class="narrow">
<h1><a name="How Fast is Redis?">How Fast is Redis?</a></h1>Redis includes the <code name="code" class="python">redis-benchmark</code> utility that simulates SETs/GETs done by N clients at the same time sending M total queries (it is similar to the Apache's <code name="code" class="python">ab</code> utility). Below you'll find the full output of the benchmark executed against a Linux box.<br/><br/><ul><li> The test was done with 50 simultaneous clients performing 100000 requests.</li><li> The value SET and GET is a 256 bytes string.</li><li> The Linux box is running <b>Linux 2.6</b>, it's <b>Xeon X3320 2.5Ghz</b>.</li><li> Text executed using the loopback interface (127.0.0.1).</li></ul>
Results: <b>about 110000 SETs per second, about 81000 GETs per second.</b><h1><a name="Latency percentiles">Latency percentiles</a></h1><pre class="codeblock python" name="code">
./redis-benchmark -n 100000
====== SET ======
100007 requests completed in 0.88 seconds
50 parallel clients
3 bytes payload
keep alive: 1
58.50% &lt;= 0 milliseconds
99.17% &lt;= 1 milliseconds
99.58% &lt;= 2 milliseconds
99.85% &lt;= 3 milliseconds
99.90% &lt;= 6 milliseconds
100.00% &lt;= 9 milliseconds
114293.71 requests per second
====== GET ======
100000 requests completed in 1.23 seconds
50 parallel clients
3 bytes payload
keep alive: 1
43.12% &lt;= 0 milliseconds
96.82% &lt;= 1 milliseconds
98.62% &lt;= 2 milliseconds
100.00% &lt;= 3 milliseconds
81234.77 requests per second
====== INCR ======
100018 requests completed in 1.46 seconds
50 parallel clients
3 bytes payload
keep alive: 1
32.32% &lt;= 0 milliseconds
96.67% &lt;= 1 milliseconds
99.14% &lt;= 2 milliseconds
99.83% &lt;= 3 milliseconds
99.88% &lt;= 4 milliseconds
99.89% &lt;= 5 milliseconds
99.96% &lt;= 9 milliseconds
100.00% &lt;= 18 milliseconds
68458.59 requests per second
====== LPUSH ======
100004 requests completed in 1.14 seconds
50 parallel clients
3 bytes payload
keep alive: 1
62.27% &lt;= 0 milliseconds
99.74% &lt;= 1 milliseconds
99.85% &lt;= 2 milliseconds
99.86% &lt;= 3 milliseconds
99.89% &lt;= 5 milliseconds
99.93% &lt;= 7 milliseconds
99.96% &lt;= 9 milliseconds
100.00% &lt;= 22 milliseconds
100.00% &lt;= 208 milliseconds
88109.25 requests per second
====== LPOP ======
100001 requests completed in 1.39 seconds
50 parallel clients
3 bytes payload
keep alive: 1
54.83% &lt;= 0 milliseconds
97.34% &lt;= 1 milliseconds
99.95% &lt;= 2 milliseconds
99.96% &lt;= 3 milliseconds
99.96% &lt;= 4 milliseconds
100.00% &lt;= 9 milliseconds
100.00% &lt;= 208 milliseconds
71994.96 requests per second
</pre>Notes: changing the payload from 256 to 1024 or 4096 bytes does not change the numbers significantly (but reply packets are glued together up to 1024 bytes so GETs may be slower with big payloads). The same for the number of clients, from 50 to 256 clients I got the same numbers. With only 10 clients it starts to get a bit slower.<br/><br/>You can expect different results from different boxes. For example a low profile box like <b>Intel core duo T5500 clocked at 1.66Ghz running Linux 2.6</b> will output the following:
<pre class="codeblock python python" name="code">
./redis-benchmark -q -n 100000
SET: 53684.38 requests per second
GET: 45497.73 requests per second
INCR: 39370.47 requests per second
LPUSH: 34803.41 requests per second
LPOP: 37367.20 requests per second
</pre>
</div>
</div>
</div>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<link type="text/css" rel="stylesheet" href="style.css" />
</head>
<body>
<div id="page">
<div id='header'>
<a href="index.html">
<img style="border:none" alt="Redis Documentation" src="redis.png">
</a>
</div>
<div id="pagecontent">
<div class="index">
<!-- This is a (PRE) block. Make sure it's left aligned or your toc title will be off. -->
<b>BgsaveCommand: Contents</b><br>&nbsp;&nbsp;<a href="#BGSAVE">BGSAVE</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Return value">Return value</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#See also">See also</a>
</div>
<h1 class="wikiname">BgsaveCommand</h1>
<div class="summary">
</div>
<div class="narrow">
<h1><a name="BGSAVE">BGSAVE</a></h1>
<blockquote>Save the DB in background. The OK code is immediately returned.Redis forks, the parent continues to server the clients, the childsaves the DB on disk then exit. A client my be able to check if theoperation succeeded using the <a href="LastsaveCommand.html">LASTSAVE</a> command.</blockquote>
<h2><a name="Return value">Return value</a></h2><a href="ReplyTypes.html">Status code reply</a><h2><a name="See also">See also</a></h2>
<ul><li> <a href="SaveCommand.html">SAVE</a></li><li> <a href="ShutdownCommand.html">SHUTDOWN</a></li></ul>
</div>
</div>
</div>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<link type="text/css" rel="stylesheet" href="style.css" />
</head>
<body>
<div id="page">
<div id='header'>
<a href="index.html">
<img style="border:none" alt="Redis Documentation" src="redis.png">
</a>
</div>
<div id="pagecontent">
<div class="index">
<!-- This is a (PRE) block. Make sure it's left aligned or your toc title will be off. -->
<b>CommandReference: Contents</b><br>&nbsp;&nbsp;<a href="#Redis Command Reference">Redis Command Reference</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Connection handling">Connection handling</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Commands operating on string values">Commands operating on string values</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Commands operating on the key space">Commands operating on the key space</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Commands operating on lists">Commands operating on lists</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Commands operating on sets">Commands operating on sets</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Multiple databases handling commands">Multiple databases handling commands</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Sorting">Sorting</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Persistence control commands">Persistence control commands</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Remote server control commands">Remote server control commands</a>
</div>
<h1 class="wikiname">CommandReference</h1>
<div class="summary">
</div>
<div class="narrow">
<h1><a name="Redis Command Reference">Redis Command Reference</a></h1>Every command name links to a specific wiki page describing the behavior of the command.<h2><a name="Connection handling">Connection handling</a></h2><ul><li> <a href="QuitCommand.html">QUIT</a> <code name="code" class="python">close the connection</code></li></ul>
<h2><a name="Commands operating on string values">Commands operating on string values</a></h2><ul><li> <a href="SetCommand.html">SET</a> <i>key</i> <i>value</i> <code name="code" class="python">set a key to a string value</code></li><li> <a href="GetCommand.html">GET</a> <i>key</i> <code name="code" class="python">return the string value of the key</code></li><li> <a href="SetnxCommand.html">SETNX</a> <i>key</i> <i>value</i> <code name="code" class="python">set a key to a string value if the key does not exist</code></li><li> <a href="IncrCommand.html">INCR</a> <i>key</i> <code name="code" class="python">increment the integer value of key</code></li><li> <a href="IncrCommand.html">INCRBY</a> <i>key</i> <i>integer</i><code name="code" class="python"> increment the integer value of key by integer</code></li><li> <a href="IncrCommand.html">INCR</a> <i>key</i> <code name="code" class="python">decrement the integer value of key</code></li><li> <a href="IncrCommand.html">DECRBY</a> <i>key</i> <i>integer</i> <code name="code" class="python">decrement the integer value of key by integer</code></li><li> <a href="ExistsCommand.html">EXISTS</a> <i>key</i> <code name="code" class="python">test if a key exists</code></li><li> <a href="DelCommand.html">DEL</a> <i>key</i> <code name="code" class="python">delete a key</code></li><li> <a href="TypeCommand.html">TYPE</a> <i>key</i> <code name="code" class="python">return the type of the value stored at key</code></li></ul>
<h2><a name="Commands operating on the key space">Commands operating on the key space</a></h2><ul><li> <a href="KeysCommand.html">KEYS</a> <i>pattern</i> <code name="code" class="python">return all the keys matching a given pattern</code></li><li> <a href="RandomkeyCommand.html">RANDOMKEY</a> <code name="code" class="python">return a random key from the key space</code></li><li> <a href="RenameCommand.html">RENAME</a> <i>oldname</i> <i>newname</i> <code name="code" class="python">rename the old key in the new one, destroing the newname key if it already exists</code></li><li> <a href="RenamenxCommand.html">RENAMENX</a> <i>oldname</i> <i>newname</i> <code name="code" class="python">rename the old key in the new one, if the newname key does not already exist</code></li><li> <a href="Dbsize.html">DBSIZE</a> <code name="code" class="python">return the number of keys in the current db</code></li></ul>
<h2><a name="Commands operating on lists">Commands operating on lists</a></h2><ul><li> <a href="RpushCommand.html">RPUSH</a> <i>key</i> <i>value</i> <code name="code" class="python">Append an element to the tail of the List value at key</code></li><li> <a href="RpushCommand.html">LPUSH</a> <i>key</i> <i>value</i> <code name="code" class="python">Append an element to the head of the List value at key</code></li><li> <a href="LlenCommand.html">LLEN</a> <i>key</i> <code name="code" class="python">Return the length of the List value at key</code></li><li> <a href="LrangeCommand.html">LRANGE</a> <i>key</i> <i>start</i> <i>end</i> <code name="code" class="python">Return a range of elements from the List at key</code></li><li> <a href="LtrimCommand.html">LTRIM</a> <i>key</i> <i>start</i> <i>end</i> <code name="code" class="python">Trim the list at key to the specified range of elements</code></li><li> <a href="LindexCommand.html">LINDEX</a> <i>key</i> <i>index</i> <code name="code" class="python">Return the element at index position from the List at key</code></li><li> <a href="LsetCommand.html">LSET</a> <i>key</i> <i>index</i> <i>value</i> <code name="code" class="python">Set a new value as the element at index position of the List at key</code></li><li> <a href="LremCommand.html">LREM</a> <i>key</i> <i>count</i> <i>value</i> <code name="code" class="python">Remove the first-N, last-N, or all the elements matching value from the List at key</code></li><li> <a href="LpopCommand.html">LPOP</a> <i>key</i> <code name="code" class="python">Return and remove (atomically) the first element of the List at key</code></li><li> <a href="LpopCommand.html">RPOP</a> <i>key</i> <code name="code" class="python">Return and remove (atomically) the last element of the List at key</code></li></ul>
<h2><a name="Commands operating on sets">Commands operating on sets</a></h2><ul><li> <a href="SaddCommand.html">SADD</a> <i>key</i> <i>member</i> <code name="code" class="python">Add the specified member to the Set value at key</code></li><li> <a href="SremCommand.html">SREM</a> <i>key</i> <i>member</i> <code name="code" class="python">Remove the specified member from the Set value at key</code></li><li> <a href="ScardCommand.html">SCARD</a> <i>key</i> <code name="code" class="python">Return the number of elements (the cardinality) of the Set at key</code></li><li> <a href="SismemberCommand.html">SISMEMBER</a> <i>key</i> <i>member</i> <code name="code" class="python">Test if the specified value is a member of the Set at key</code></li><li> <a href="SinterCommand.html">SINTER</a> <i>key1</i> <i>key2</i> ... <i>keyN</i> <code name="code" class="python">Return the intersection between the Sets stored at key1, key2, ..., keyN</code></li><li> <a href="SinterCommand.html">SINTERSTORE</a> <i>dstkey</i> <i>key1</i> <i>key2</i> ... <i>keyN</i> <code name="code" class="python">Compute the intersection between the Sets stored at key1, key2, ..., keyN, and store the resulting Set at dstkey</code></li><li> <a href="SmembersCommand.html">SMEMBERS</a> <i>key</i> <code name="code" class="python">Return all the members of the Set value at key</code></li></ul>
<h2><a name="Multiple databases handling commands">Multiple databases handling commands</a></h2><ul><li> <a href="SelectCommand.html">SELECT</a> <i>index</i> <code name="code" class="python">Select the DB having the specified index</code></li><li> <a href="MoveCommand.html">MOVE</a> <i>key</i> <i>dbindex</i> <code name="code" class="python">Move the key from the currently selected DB to the DB having as index dbindex</code></li><li> <a href="FlushdbCommand.html">FLUSHDB</a> <code name="code" class="python">Remove all the keys of the currently selected DB</code></li><li> <a href="FlushallCommand.html">FLUSHALL</a> <code name="code" class="python">Remove all the keys from all the databases</code></li></ul>
<h2><a name="Sorting">Sorting</a></h2><ul><li> <a href="SortCommand.html">SORT</a> <i>key</i> BY <i>pattern</i> LIMIT <i>start</i> <i>end</i> GET <i>pattern</i> ASC|DESC ALPHA <code name="code" class="python">Sort a Set or a List accordingly to the specified parameters</code></li></ul>
<h2><a name="Persistence control commands">Persistence control commands</a></h2><ul><li> <a href="SaveCommand.html">SAVE</a> <code name="code" class="python">Synchronously save the DB on disk</code></li><li> <a href="BgsaveCommand.html">BGSAVE</a> <code name="code" class="python">Asynchronously save the DB on disk</code></li><li> <a href="LastsaveCommand.html">LASTSAVE</a> <code name="code" class="python">Return the UNIX time stamp of the last successfully saving of the dataset on disk</code></li><li> <a href="ShutdownCommand.html">SHUTDOWN</a> <code name="code" class="python">Synchronously save the DB on disk, then shutdown the server</code></li></ul>
<h2><a name="Remote server control commands">Remote server control commands</a></h2><ul><li> <a href="InfoCommand.html">INFO</a> <code name="code" class="python">provide information and statistics about the server</code></li></ul>
</div>
</div>
</div>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<link type="text/css" rel="stylesheet" href="style.css" />
</head>
<body>
<div id="page">
<div id='header'>
<a href="index.html">
<img style="border:none" alt="Redis Documentation" src="redis.png">
</a>
</div>
<div id="pagecontent">
<div class="index">
<!-- This is a (PRE) block. Make sure it's left aligned or your toc title will be off. -->
<b>Credits: Contents</b><br>&nbsp;&nbsp;<a href="#Credits">Credits</a>
</div>
<h1 class="wikiname">Credits</h1>
<div class="summary">
</div>
<div class="narrow">
<h1><a name="Credits">Credits</a></h1><ul><li> The Redis server was designed and written by <a href="http://invece.org" target="_blank">Salvatore Sanfilippo (aka antirez)</a></li><li> The Ruby client library was written by <a href="http://brainspl.at/" target="_blank">Ezra Zygmuntowicz (aka ezmobius)</a></li><li> The Python and PHP client libraries were written by <a href="http://qix.it" target="_blank">Ludovico Magnocavallo (aka ludo)</a></li><li> The Erlang client library was written by <a href="http://www.adroll.com/" target="_blank">Valentino Volonghi of Adroll</a></li><li> <b>brettbender</b> found and fixed a but in sds.c that caused the server to crash at least on 64 bit systems, and anyway to be buggy since we used the same vararg thing against vsprintf without to call va_start and va_end every time.</li></ul>
</div>
</div>
</div>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<link type="text/css" rel="stylesheet" href="style.css" />
</head>
<body>
<div id="page">
<div id='header'>
<a href="index.html">
<img style="border:none" alt="Redis Documentation" src="redis.png">
</a>
</div>
<div id="pagecontent">
<div class="index">
<!-- This is a (PRE) block. Make sure it's left aligned or your toc title will be off. -->
<b>DbsizeCommand: Contents</b><br>&nbsp;&nbsp;<a href="#DBSIZE">DBSIZE</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Return value">Return value</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#See also">See also</a>
</div>
<h1 class="wikiname">DbsizeCommand</h1>
<div class="summary">
</div>
<div class="narrow">
<h1><a name="DBSIZE">DBSIZE</a></h1><blockquote>Return the number of keys in the currently selected database.</blockquote>
<h2><a name="Return value">Return value</a></h2><a href="ReplyTypes.html">Integer reply</a><h2><a name="See also">See also</a></h2>
<ul><li> <a href="SelectCommand.html">SELECT</a></li><li> <a href="InfoCommand.html">INFO</a></li></ul>
</div>
</div>
</div>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<link type="text/css" rel="stylesheet" href="style.css" />
</head>
<body>
<div id="page">
<div id='header'>
<a href="index.html">
<img style="border:none" alt="Redis Documentation" src="redis.png">
</a>
</div>
<div id="pagecontent">
<div class="index">
<!-- This is a (PRE) block. Make sure it's left aligned or your toc title will be off. -->
<b>DelCommand: Contents</b><br>&nbsp;&nbsp;<a href="#DEL _key_">DEL _key_</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Return value">Return value</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#See also">See also</a>
</div>
<h1 class="wikiname">DelCommand</h1>
<div class="summary">
</div>
<div class="narrow">
<h1><a name="DEL _key_">DEL _key_</a></h1>
<i>Time complexity: O(1)</i><blockquote>Remove the specified key. If the key does not existno operation is performed. The command always returns success.</blockquote>
<h2><a name="Return value">Return value</a></h2><a href="ReplyTypes.html">Integer reply</a>, specifically:<br/><br/><pre class="codeblock python" name="code">
1 if the key was removed
0 if the key does not exist
</pre><h2><a name="See also">See also</a></h2>
<ul><li> <a href="SetCommand.html">SET</a></li><li> <a href="GetCommand.html">GET</a></li><li> <a href="ExistsCommand.html">EXISTS</a></li><li> <a href="LdelCommand.html">LDEL</a></li></ul>
</div>
</div>
</div>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<link type="text/css" rel="stylesheet" href="style.css" />
</head>
<body>
<div id="page">
<div id='header'>
<a href="index.html">
<img style="border:none" alt="Redis Documentation" src="redis.png">
</a>
</div>
<div id="pagecontent">
<div class="index">
<!-- This is a (PRE) block. Make sure it's left aligned or your toc title will be off. -->
<b>DesignPatterns: Contents</b>
</div>
<h1 class="wikiname">DesignPatterns</h1>
<div class="summary">
</div>
<div class="narrow">
Use random keys instead of incremental keys in order to avoid a single-key that gets incremented by many servers. This can can't be distributed among servers.
</div>
</div>
</div>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<link type="text/css" rel="stylesheet" href="style.css" />
</head>
<body>
<div id="page">
<div id='header'>
<a href="index.html">
<img style="border:none" alt="Redis Documentation" src="redis.png">
</a>
</div>
<div id="pagecontent">
<div class="index">
<!-- This is a (PRE) block. Make sure it's left aligned or your toc title will be off. -->
<b>ExistsCommand: Contents</b><br>&nbsp;&nbsp;<a href="#EXISTS _key_">EXISTS _key_</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Return value">Return value</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#See also">See also</a>
</div>
<h1 class="wikiname">ExistsCommand</h1>
<div class="summary">
</div>
<div class="narrow">
<h1><a name="EXISTS _key_">EXISTS _key_</a></h1>
<i>Time complexity: O(1)</i><blockquote>Test if the specified key exists. The command returns&quot;0&quot; if the key exists, otherwise &quot;1&quot; is returned.Note that even keys set with an empty string as value willreturn &quot;1&quot;.</blockquote>
<h2><a name="Return value">Return value</a></h2><a href="ReplyTypes.html">Integer reply</a>, specifically:<br/><br/><pre class="codeblock python" name="code">
1 if the key exists.
0 if the key does not exist.
</pre><h2><a name="See also">See also</a></h2>
<ul><li> <a href="SetnxCommand.html">SETNX</a> is a <code name="code" class="python">SET if not EXISTS</code> atomic operation.</li><li> <a href="SismemberCommand.html">SISMEMBER</a> test if an element is a member of a Set.</li></ul>
</div>
</div>
</div>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<link type="text/css" rel="stylesheet" href="style.css" />
</head>
<body>
<div id="page">
<div id='header'>
<a href="index.html">
<img style="border:none" alt="Redis Documentation" src="redis.png">
</a>
</div>
<div id="pagecontent">
<div class="index">
<!-- This is a (PRE) block. Make sure it's left aligned or your toc title will be off. -->
<b>FAQ: Contents</b><br>&nbsp;&nbsp;<a href="#Why I need Redis if there is already memcachedb, Tokyo Cabinet, ...?">Why I need Redis if there is already memcachedb, Tokyo Cabinet, ...?</a><br>&nbsp;&nbsp;<a href="#Isn't this key-value thing just hype?">Isn't this key-value thing just hype?</a><br>&nbsp;&nbsp;<a href="#Can I backup a Redis DB while the server is working?">Can I backup a Redis DB while the server is working?</a><br>&nbsp;&nbsp;<a href="#What's the Redis memory footprint?">What's the Redis memory footprint?</a><br>&nbsp;&nbsp;<a href="#I like Redis high level operations and features, but I don't like it takes everything in memory and I can't have a dataset larger the memory. Plans to change this?">I like Redis high level operations and features, but I don't like it takes everything in memory and I can't have a dataset larger the memory. Plans to change this?</a><br>&nbsp;&nbsp;<a href="#Ok but I absolutely need to have a DB larger than memory, still I need the Redis features">Ok but I absolutely need to have a DB larger than memory, still I need the Redis features</a><br>&nbsp;&nbsp;<a href="#What happens if Redis runs out of memory?">What happens if Redis runs out of memory?</a><br>&nbsp;&nbsp;<a href="#What Redis means actually?">What Redis means actually?</a><br>&nbsp;&nbsp;<a href="#Why did you started the Redis project?">Why did you started the Redis project?</a>
</div>
<h1 class="wikiname">FAQ</h1>
<div class="summary">
</div>
<div class="narrow">
<h1><a name="Why I need Redis if there is already memcachedb, Tokyo Cabinet, ...?">Why I need Redis if there is already memcachedb, Tokyo Cabinet, ...?</a></h1>Memcachedb is basically memcached done persistent. Redis is a different evolution
path in the key-value DBs, the idea is that the main advantages of key-value DBs
are retained even without a so severe loss of comfort of plain key-value DBs.
So Redis offers more features:<br/><br/><ul><li> Keys can store different data types, not just strings. Notably Lists and Sets. For example if you want to use Redis as a log storage system for different computers every computer can just <code name="code" class="python">RPUSH data to the computer_ID key</code>. Don't want to save more than 1000 log lines per computer? Just issue a <code name="code" class="python">LTRIM computer_ID 0 999</code> command to trim the list after every push.</li></ul>
<ul><li> Another example is about Sets. Imagine to build a social news site like <a href="http://reddit.com" target="_blank">Reddit</a>. Every time a user upvote a given news you can just add to the news_ID_upmods key holding a value of type SET the id of the user that did the upmodding. Sets can also be used to index things. Every key can be a tag holding a SET with the IDs of all the objects associated to this tag. Using Redis set intersection you obtain the list of IDs having all this tags at the same time.</li></ul>
<ul><li> We wrote a <a href="http://retwis.antirez.com" target="_blank">simple Twitter Clone</a> using just Redis as database. Download the source code from the download section and imagine to write it with a plain key-value DB without support for lists and sets... it's <b>much</b> harder.</li></ul>
<ul><li> Multiple DBs. Using the SELECT command the client can select different datasets. This is useful because Redis provides a MOVE atomic primitive that moves a key form a DB to another one, if the target DB already contains such a key it returns an error: this basically means a way to perform locking in distributed processing.</li></ul>
<ul><li> <b>So what is Redis really about?</b> The User interface with the programmer. Redis aims to export to the programmer the right tools to model a wide range of problems. <b>Sets, Lists with O(1) push operation, lrange and ltrim, server-side fast intersection between sets, are primitives that allow to model complex problems with a key value database</b>.</li></ul>
<h1><a name="Isn't this key-value thing just hype?">Isn't this key-value thing just hype?</a></h1>I imagine key-value DBs, in the short term future, to be used like you use memory in a program, with lists, hashes, and so on. With Redis it's like this, but this special kind of memory containing your data structures is shared, atomic, persistent.<br/><br/>When we write code it is obvious, when we take data in memory, to use the most sensible data structure for the work, right? Incredibly when data is put inside a relational DB this is no longer true, and we create an absurd data model even if our need is to put data and get this data back in the same order we put it inside (an ORDER BY is required when the data should be already sorted. Strange, dont' you think?).<br/><br/>Key-value DBs bring this back at home, to create sensible data models and use the right data structures for the problem we are trying to solve.<h1><a name="Can I backup a Redis DB while the server is working?">Can I backup a Redis DB while the server is working?</a></h1>Yes you can. When Redis saves the DB it actually creates a temp file, then rename(2) that temp file name to the destination file name. So even while the server is working it is safe to save the database file just with the <i>cp</i> unix command. Note that you can use master-slave replication in order to have redundancy of data, but if all you need is backups, cp or scp will do the work pretty well.<h1><a name="What's the Redis memory footprint?">What's the Redis memory footprint?</a></h1>Worst case scenario: 1 Million keys with the key being the natural numbers from 0 to 999999 and the string &quot;Hello World&quot; as value use 100MB on my Intel macbook (32bit). Note that the same data stored linearly in an unique string takes something like 16MB, this is the norm because with small keys and values there is a lot of overhead. Memcached will perform similarly.<br/><br/>With large keys/values the ratio is much better of course.<br/><br/>64 bit systems will use much more memory than 32 bit systems to store the same keys, especially if the keys and values are small, this is because pointers takes 8 bytes in 64 bit systems. But of course the advantage is that you can have a lot of memory in 64 bit systems, so to run large Redis servers a 64 bit system is more or less required.<h1><a name="I like Redis high level operations and features, but I don't like it takes everything in memory and I can't have a dataset larger the memory. Plans to change this?">I like Redis high level operations and features, but I don't like it takes everything in memory and I can't have a dataset larger the memory. Plans to change this?</a></h1>The whole key-value hype started for a reason: performances. Redis takes the whole dataset in memory and writes asynchronously on disk in order to be very fast, you have the best of both worlds: hyper-speed and persistence of data, but the price to pay is exactly this, that the dataset must fit on your computers RAM.<br/><br/>If the data is larger then memory, and this data is stored on disk, what happens is that the bottleneck of the disk I/O speed will start to ruin the performances. Maybe not in benchmarks, but once you have real load with distributed key accesses the data must come from disk, and the disk is damn slow. Not only, but Redis supports higher level data structures than the plain values. To implement this things on disk is even slower.<br/><br/>Redis will always continue to hold the whole dataset in memory because this days scalability requires to use RAM as storage media, and RAM is getting cheaper and cheaper. Today it is common for an entry level server to have 16 GB of RAM! And in the 64-bit era there are no longer limits to the amount of RAM you can have in theory.<h1><a name="Ok but I absolutely need to have a DB larger than memory, still I need the Redis features">Ok but I absolutely need to have a DB larger than memory, still I need the Redis features</a></h1>One possible solution is to use both MySQL and Redis at the same time, basically take the state on Redis, and all the things that get accessed very frequently: user auth tokens, Redis Lists with chronologically ordered IDs of the last N-comments, N-posts, and so on. Then use MySQL as a simple storage engine for larger data, that is just create a table with an auto-incrementing ID as primary key and a large BLOB field as data field. Access MySQL data only by primary key (the ID). The application will run the high traffic queries against Redis but when there is to take the big data will ask MySQL for specific resources IDs.<h1><a name="What happens if Redis runs out of memory?">What happens if Redis runs out of memory?</a></h1>With modern operating systems malloc() returning NULL is not common, usually the server will start swapping and Redis performances will be disastrous so you'll know it's time to use more Redis servers or get more RAM.<br/><br/>However it is planned to add a configuration directive to tell Redis to stop accepting queries but instead to SAVE the latest data and quit if it is using more than a given amount of memory. Also the new INFO command (work in progress in this days) will report the amount of memory Redis is using so you can write scripts that monitor your Redis servers checking for critical conditions.<br/><br/>Update: redis SVN is able to know how much memory it is using and report it via the <a href="InfoCommand.html">INFO</a> command.<h1><a name="What Redis means actually?">What Redis means actually?</a></h1>Redis means two things:
<ul><li> it's a joke on the word Redistribute (instead to use just a Relational DB redistribute your workload among Redis servers)</li><li> it means REmote DIctionary Server</li></ul>
<h1><a name="Why did you started the Redis project?">Why did you started the Redis project?</a></h1>In order to scale <a href="http://lloogg.com" target="_blank">LLOOGG</a>. But after I got the basic server working I liked the idea to share the work with other guys, and Redis was turned into an open source project.
</div>
</div>
</div>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<link type="text/css" rel="stylesheet" href="style.css" />
</head>
<body>
<div id="page">
<div id='header'>
<a href="index.html">
<img style="border:none" alt="Redis Documentation" src="redis.png">
</a>
</div>
<div id="pagecontent">
<div class="index">
<!-- This is a (PRE) block. Make sure it's left aligned or your toc title will be off. -->
<b>FlushallCommand: Contents</b><br>&nbsp;&nbsp;<a href="#FLUSHALL">FLUSHALL</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Return value">Return value</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#See also">See also</a>
</div>
<h1 class="wikiname">FlushallCommand</h1>
<div class="summary">
</div>
<div class="narrow">
<h1><a name="FLUSHALL">FLUSHALL</a></h1>
<blockquote>Delete all the keys of all the existing databases, not just the currently selected one. This command never fails.</blockquote>
<h2><a name="Return value">Return value</a></h2><a href="ReplyTypes.html">Status code reply</a><h2><a name="See also">See also</a></h2>
<ul><li> <a href="FlushdbCommand.html">FLUSHDB</a></li></ul>
</div>
</div>
</div>
</body>
</html>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment