Redis 是一个高性能的key-value数据库。 它支持存储的value类型包括string、hash、list、set 和 zset等。redis还支持各种不同方式的排序。
All the commands available in Redis 2.0.0.
API Reference
append(self, key, value)
Appends the string
bgrewriteaof(self)
Tell the Redis server to rewrite the AOF file from data in memory.
bgsave(self)
Tell the Redis server to save its data to disk. Unlike save(), this method is asynchronous and returns immediately.
blpop(self, keys, timeout=0)
LPOP a value off of the first non-empty list named in the
If none of the lists in
If timeout is 0, then block indefinitely.
brpop(self, keys, timeout=0)
RPOP a value off of the first non-empty list named in the
If none of the lists in
If timeout is 0, then block indefinitely.
dbsize(self)
Returns the number of keys in the current database
decr(self, name, amount=1)
Decrements the value of
delete(self, *names)
Delete one or more keys specified by
encode(self, value)
Encode
execute_command(self, *args, **options)
Sends the command to the redis server and returns it's response
exists(self, name)
Returns a boolean indicating whether key
expire(self, name, time)
Set an expire flag on key
expireat(self, name, when)
Set an expire flag on key
flush(self, all_dbs=False)
flushall(self)
Delete all keys in all databases on the current host
flushdb(self)
Delete all keys in the current database
get(self, name)
Return the value at key
get_connection(self, host, port, db, password, socket_timeout)
Returns a connection object
getset(self, name, value)
Set the value at key
hdel(self, name, key)
Delete
hexists(self, name, key)
Returns a boolean indicating if
hget(self, name, key)
Return the value of
hgetall(self, name)
Return a Python dict of the hash's name/value pairs
hincrby(self, name, key, amount=1)
Increment the value of
hkeys(self, name)
Return the list of keys within hash
hlen(self, name)
Return the number of elements in hash
hmget(self, name, keys)
Returns a list of values ordered identically to
hmset(self, name, mapping)
Sets each key in the
hset(self, name, key, value)
Set
hsetnx(self, name, key, value)
Set
hvals(self, name)
Return the list of values within hash
incr(self, name, amount=1)
Increments the value of
info(self)
Returns a dictionary containing information about the Redis server
keys(self, pattern='*')
Returns a list of keys matching
lastsave(self)
Return a Python datetime object representing the last time the Redis database was saved to disk
lindex(self, name, index)
Return the item from list
Negative indexes are supported and will return an item at the end of the list
listen(self)
Listen for messages on channels this client has been subscribed to
llen(self, name)
Return the length of the list
lock(self, name, timeout=None, sleep=0.10000000000000001)
Return a new Lock object using key
If specified,
sleep
lpop(self, name)
Remove and return the first item of the list
lpush(self, name, value)
Push
lrange(self, name, start, end)
Return a slice of the list
start
lrem(self, name, value, num=0)
Remove the first
If
lset(self, name, index, value)
Set
ltrim(self, name, start, end)
Trim the list
start
mget(self, keys, *args)
Returns a list of values ordered identically to
- Passing *args to this method has been deprecated *
move(self, name, db)
Moves the key
mset(self, mapping)
Sets each key in the
msetnx(self, mapping)
Sets each key in the
parse_response(self, command_name, catch_errors=False, **options)
Parses a response from the Redis server
ping(self)
Ping the Redis server
pipeline(self, transaction=True)
Return a new pipeline object that can queue multiple commands for later execution.
pop(self, name, tail=False)
Pop and return the first or last element of list
This method has been deprecated, use
psubscribe(self, patterns)
Subscribe to all channels matching any pattern in
publish(self, channel, message)
Publish
punsubscribe(self, patterns=[])
Unsubscribe from any channel matching any pattern in
push(self, name, value, head=False)
Push
This method has been deprecated, use
randomkey(self)
Returns the name of a random key
rename(self, src, dst, **kwargs)
Rename key
- The following flags have been deprecated *If
preserve is True, rename the key only if the destination name doesn't already exist
renamenx(self, src, dst)
Rename key
rpop(self, name)
Remove and return the last item of the list
rpoplpush(self, src, dst)
RPOP a value off of the
rpush(self, name, value)
Push
sadd(self, name, value)
Add
save(self)
Tell the Redis server to save its data to disk, blocking until the save is complete
scard(self, name)
Return the number of elements in set
sdiff(self, keys, *args)
Return the difference of sets specified by
sdiffstore(self, dest, keys, *args)
Store the difference of sets specified by
select(self, db, host=None, port=None, password=None, socket_timeout=None)
Switch to a different Redis connection.
If the host and port aren't provided and there's an existing connection, use the existing connection's host and port instead.
Note this method actually replaces the underlying connection object prior to issuing the SELECT command. This makes sure we protect the thread-safe connections
set(self, name, value, **kwargs)
Set the value at key
- The following flags have been deprecated *If
preserve is True, set the value only if key doesn't alreadyexistIf getset is True, set the value only if key doesn't already existand return the resulting value of key
setex(self, name, value, time)
Set the value of key
setnx(self, name, value)
Set the value of key
sinter(self, keys, *args)
Return the intersection of sets specified by
sinterstore(self, dest, keys, *args)
Store the intersection of sets specified by
sismember(self, name, value)
Return a boolean indicating if
smembers(self, name)
Return all members of the set
smove(self, src, dst, value)
Move
sort(self, name, start=None, num=None, by=None, get=None, desc=False, alpha=False, store=None)
Sort and return the list, set or sorted set at
start
by
get
desc
alpha
store
spop(self, name)
Remove and return a random member of set
srandmember(self, name)
Return a random member of set
srem(self, name, value)
Remove
subscribe(self, channels)
Subscribe to
substr(self, name, start, end=-1)
Return a substring of the string at key
sunion(self, keys, *args)
Return the union of sets specifiued by
sunionstore(self, dest, keys, *args)
Store the union of sets specified by
ttl(self, name)
Returns the number of seconds until the key
type(self, name)
Returns the type of key
unsubscribe(self, channels=[])
Unsubscribe from
zadd(self, name, value, score)
Add member
zcard(self, name)
Return the number of elements in the sorted set
zincr(self, key, member, value=1)
This has been deprecated, use zincrby instead
zincrby(self, name, value, amount=1)
Increment the score of
zinter(self, dest, keys, aggregate=None)
zinterstore(self, dest, keys, aggregate=None)
Intersect multiple sorted sets specified by
zrange(self, name, start, end, desc=False, withscores=False)
Return a range of values from sorted set
start
desc
withscores
zrangebyscore(self, name, min, max, start=None, num=None, withscores=False)
Return a range of values from the sorted set
If
withscores
zrank(self, name, value)
Returns a 0-based value indicating the rank of
zrem(self, name, value)
Remove member
zremrangebyrank(self, name, min, max)
Remove all elements in the sorted set
zremrangebyscore(self, name, min, max)
Remove all elements in the sorted set
zrevrange(self, name, start, num, withscores=False)
Return a range of values from sorted set
start
withscores
zrevrank(self, name, value)
Returns a 0-based value indicating the descending rank of
zscore(self, name, value)
Return the score of element
zunion(self, dest, keys, aggregate=None)
zunionstore(self, dest, keys, aggregate=None)
Union multiple sorted sets specified by