ledis-py’s documentation!¶
API Reference¶
-
class
ledis.Ledis(host='localhost', port=6380, db=0, socket_timeout=None, connection_pool=None, charset='utf-8', errors='strict', decode_responses=False, unix_socket_path=None)[source]¶ Implementation of the Redis protocol.
This abstract class provides a Python interface to all LedisDB commands and an implementation of the Redis protocol.
Connection and Pipeline derive from this, implementing how the commands are sent and received to the Ledis server
-
bexpireat(name, when)[source]¶ - Set an expire flag on key name for time seconds. time can be represented by
- an integer or a Python timedelta object.
-
bmsetbit(name, *args)[source]¶ Set any number of offset, value pairs to the key
name. Pairs can be specified in the following way:offset1, value1, offset2, value2, ...
-
bopt(operation, dest, *keys)[source]¶ Perform a bitwise operation using
operationbetweenkeysand store the result indest.operationis one of and, or, xor, not.
-
decr(name, amount=1)[source]¶ Decrements the value of
keybyamount. If no key exists, the value will be initialized as 0 -amount
-
decrby(name, amount=1)[source]¶ Decrements the value of
keybyamount. If no key exists, the value will be initialized as 0 -amount
-
expire(name, time)[source]¶ Set an expire flag on key
namefortimeseconds.timecan be represented by an integer or a Python timedelta object.
-
expireat(name, when)[source]¶ Set an expire flag on key
name.whencan be represented as an integer indicating unix time or a Python datetime object.
-
classmethod
from_url(url, db=None, **kwargs)[source]¶ Return a Ledis client object configured from the given URL.
For example:
ledis://localhost:6380/0 unix:///path/to/socket.sock?db=0
There are several ways to specify a database number. The parse function will return the first specified option:
- A
dbquerystring option, e.g. ledis://localhost?db=0 - If using the ledis:// scheme, the path argument of the url, e.g. ledis://localhost/0
- The
dbargument to this function.
If none of these options are specified, db=0 is used.
Any additional querystring arguments and keyword arguments will be passed along to the ConnectionPool class’s initializer. In the case of conflicting arguments, querystring arguments always win.
- A
-
getset(name, value)[source]¶ Set the value at key
nametovalueif key doesn’t exist Return the value at keynameatomically
-
hexpire(name, time)[source]¶ Set an expire flag on key name for time milliseconds. time can be represented by an integer or a Python timedelta object.
-
hexpireat(name, when)[source]¶ Set an expire flag on key name. when can be represented as an integer representing unix time in milliseconds (unix time * 1000) or a Python datetime object.
-
hmset(name, mapping)[source]¶ Sets each key in the
mappingdict to its corresponding value in the hashname
-
hset(name, key, value)[source]¶ Set
keytovaluewithin hashnameReturns 1 if HSET created a new field, otherwise 0
-
incr(name, amount=1)[source]¶ Increments the value of
keybyamount. If no key exists, the value will be initialized asamount
-
incrby(name, amount=1)[source]¶ Increments the value of
keybyamount. If no key exists, the value will be initialized asamount
-
lexpire(name, time)[source]¶ Set an expire flag on key
namefortimeseconds.timecan be represented by an integer or a Python timedelta object.
-
lexpireat(name, when)[source]¶ Set an expire flag on key
name.whencan be represented as an integer indicating unix time or a Python datetime object.
-
lindex(name, index)[source]¶ Return the item from list
nameat positionindexNegative indexes are supported and will return an item at the end of the list
-
lrange(name, start, end)[source]¶ Return a slice of the list
namebetween positionstartandendstartandendcan be negative numbers just like Python slicing notation
-
mset(*args, **kwargs)[source]¶ Sets key/values based on a mapping. Mapping can be supplied as a single dictionary argument or as kwargs.
-
parse_response(connection, command_name, **options)[source]¶ Parses a response from the Ledis server
-
sdiffstore(dest, keys, *args)[source]¶ Store the difference of sets specified by
keysinto a new set nameddest. Returns the number of keys in the new set.
-
sexpire(name, time)[source]¶ Set an expire flag on key name for time milliseconds. time can be represented by an integer or a Python timedelta object.
-
sexpireat(name, when)[source]¶ Set an expire flag on key name. when can be represented as an integer representing unix time in milliseconds (unix time * 1000) or a Python datetime object.
-
sinterstore(dest, keys, *args)[source]¶ Store the intersection of sets specified by
keysinto a new set nameddest. Returns the number of keys in the new set.
-
sunionstore(dest, keys, *args)[source]¶ Store the union of sets specified by
keysinto a new set nameddest. Returns the number of keys in the new set.
-
zadd(name, *args, **kwargs)[source]¶ Set any number of score, element-name pairs to the key
name. Pairs can be specified in two ways:As *args, in the form of: score1, name1, score2, name2, ... or as **kwargs, in the form of: name1=score1, name2=score2, ...
The following example would add four values to the ‘my-key’ key: ledis.zadd(‘my-key’, 1.1, ‘name1’, 2.2, ‘name2’, name3=3.3, name4=4.4)
-
zcount(name, min, max)[source]¶ Return the number of elements in the sorted set at key
namewith a score betweenminandmax. The min and max arguments have the same semantic as described for ZRANGEBYSCORE.
-
zexpireat(name, when)[source]¶ - Set an expire flag on key name for time seconds. time can be represented by
- an integer or a Python timedelta object.
-
zrange(name, start, end, desc=False, withscores=False)[source]¶ Return a range of values from sorted set
namebetweenstartandendsorted in ascending order.startandendcan be negative, indicating the end of the range.desca boolean indicating whether to sort the results descendinglywithscoresindicates to return the scores along with the values. The return type is a list of (value, score) pairs
-
zrangebyscore(name, min, max, start=None, num=None, withscores=False)[source]¶ Return a range of values from the sorted set
namewith scores betweenminandmax.If
startandnumare specified, then return a slice of the range.withscoresindicates to return the scores along with the values. The return type is a list of (value, score) pairs
-
zremrangebyrank(name, min, max)[source]¶ Remove all elements in the sorted set
namewith ranks betweenminandmax. Values are 0-based, ordered from smallest score to largest. Values can be negative indicating the highest scores. Returns the number of elements removed
-
zremrangebyscore(name, min, max)[source]¶ Remove all elements in the sorted set
namewith scores betweenminandmax. Returns the number of elements removed.
-
zrevrange(name, start, num, withscores=False)[source]¶ Return a range of values from sorted set
namebetweenstartandnumsorted in descending order.startandnumcan be negative, indicating the end of the range.withscoresindicates to return the scores along with the values The return type is a list of (value, score) pairs
-
zrevrangebyscore(name, min, max, start=None, num=None, withscores=False)[source]¶ Return a range of values from the sorted set
namewith scores betweenminandmaxin descending order.If
startandnumare specified, then return a slice of the range.withscoresindicates to return the scores along with the values. The return type is a list of (value, score) pairs
-
-
class
ledis.ConnectionPool(connection_class=<class 'ledis.connection.Connection'>, max_connections=None, **connection_kwargs)[source]¶ Generic connection pool
-
classmethod
from_url(url, db=None, **kwargs)[source]¶ Return a connection pool configured from the given URL.
For example:
ledis://localhost:6380/0 unix:///path/to/socket.sock?db=0
- Three URL schemes are supported:
- ledis:// creates a normal TCP socket connection unix:// creates a Unix Domain Socket connection
There are several ways to specify a database number. The parse function will return the first specified option:
- A
dbquerystring option, e.g. ledis://localhost?db=0 - If using the ledis:// scheme, the path argument of the url, e.g. ledis://localhost/0
- The
dbargument to this function.
If none of these options are specified, db=0 is used.
Any additional querystring arguments and keyword arguments will be passed along to the ConnectionPool class’s initializer. In the case of conflicting arguments, querystring arguments always win.
-
classmethod
-
class
ledis.BlockingConnectionPool(max_connections=50, timeout=20, connection_class=None, queue_class=None, **connection_kwargs)[source]¶ Thread-safe blocking connection pool:
>>> from ledis.client import Ledis >>> client = Ledis(connection_pool=BlockingConnectionPool())
It performs the same function as the default
:py:class: ~ledis.connection.ConnectionPoolimplementation, in that, it maintains a pool of reusable connections that can be shared by multiple ledis clients (safely across threads if required).The difference is that, in the event that a client tries to get a connection from the pool when all of connections are in use, rather than raising a
:py:class: ~ledis.exceptions.ConnectionError(as the default:py:class: ~ledis.connection.ConnectionPoolimplementation does), it makes the client wait (“blocks”) for a specified number of seconds until a connection becomes available.Use
max_connectionsto increase / decrease the pool size:>>> pool = BlockingConnectionPool(max_connections=10)
Use
timeoutto tell it either how many seconds to wait for a connection to become available, or to block forever:# Block forever. >>> pool = BlockingConnectionPool(timeout=None)
# Raise a
ConnectionErrorafter five seconds if a connection is # not available. >>> pool = BlockingConnectionPool(timeout=5)-
get_connection(command_name, *keys, **options)[source]¶ Get a connection, blocking for
self.timeoutuntil a connection is available from the pool.If the connection returned is
Nonethen creates a new connection. Because we use a last-in first-out queue, the existing connections (having been returned to the pool after the initialNonevalues were added) will be returned beforeNonevalues. This means we only create new connections when we need to, i.e.: the actual number of connections will only increase in response to demand.
-