aiogibson — API Reference¶
Connection Object¶
Low level connection with raw interface.
- aiogibson.connection.create_connection(address, *, encoding=None, loop=None)[source]¶
Creates GibsonConnection connection. Opens connection to Gibson server specified by address argument.
Parameters: - address – str for unix socket path, or tuple for (host, port) tcp connection.
- encoding – this argument can be used to decode byte-replies to strings. By default no decoding is done.
- class aiogibson.connection.GibsonConnection(reader, writer, address, *, encoding=None, loop=None)[source]¶
Gibson connection.
- execute(command, *args, encoding=<object object at 0x7fd650a1b080>)[source]¶
Executes raw gibson command.
Parameters: - command – str or bytes gibson command.
- args – tuple of arguments required for gibson command.
- encoding – str default encoding for unpacked data.
Raises: - TypeError – if any of args can not be encoded as bytes.
- ProtocolError – when response can not be decoded meaning connection is broken.
Connection Pool¶
Pool of connection using context manager protocol:
import asyncio
from aiogibson import create_pool
loop = asyncio.get_event_loop()
@asyncio.coroutine
def go():
pool = yield from create_pool('/tmp/aio.sock', minsize=5, maxsize=10,
loop=loop)
with (yield from pool) as gibson:
yield from gibson.set('foo', 'bar')
value = yield from gibson.get('foo')
print(value)
pool.clear()
loop.run_until_complete(go())
- aiogibson.pool.create_pool(address, *, encoding=None, minsize=10, maxsize=10, commands_factory=<class 'aiogibson.commands.Gibson'>, loop=None)[source]¶
Creates Gibson Pool.
By default it creates pool of commands_factory instances, but it is also possible to create pool of plain connections by passing lambda conn: conn as commands_factory. All arguments are the same as for create_connection. Returns GibsonPool instance.
Protocol Parser¶
The Reader class has two methods that are used when parsing replies from a stream of data. Reader.feed takes a string argument that is appended to the internal buffer. Reader.gets reads this buffer and returns a reply when the buffer contains a full reply. If a single call to feed contains multiple replies, gets should be called multiple times to extract all replies.
>>> reader = aiogibson.Reader()
>>> reader.feed(b'\x06\x00\x05\x03\x00\x00\x00bar')
>>> reader.gets()
b'bar'
When the buffer does not contain a full reply, gets returns False. This means extra data is needed and feed should be called again before calling gets again:
>>> reader.feed(b' ')
>>> reader.gets()
False
>>> reader.feed(b'\x03\x00\x00\x00bar')
>>> reader.gets()
b'bar'
note: | api same as in hiredis. |
---|
This module has encode_command, packs gibson command to binary format suitable to send over socket to gibson server:
>>> encode_command(b'set', 3600, 'foo', 3.14)
b'\x0f\x00\x00\x00\x01\x003600 foo 3.14'
- aiogibson.parser.encode_command(command, *args)[source]¶
Pack and encode gibson command according to gibson binary protocol
See: Parameters: - command – bytes, gibson command (get, set, etc.)
- args – required arguments for given command.
Returns: bytes packed and encoded command.
- class aiogibson.parser.Reader[source]¶
This class is responsible for parsing replies from the stream of data that is read from a Gibson connection. It does not contain functionality to handle I/O
Exceptions¶
- exception aiogibson.errors.ExpectedANumber[source]¶
Expected a number ( TTL or TIME ) but the specified value was invalid.
High Level Commands¶
- aiogibson.commands.create_gibson(address, *, encoding=None, commands_factory=<class 'aiogibson.commands.Gibson'>, loop=None)[source]¶
Create high-level Gibson interface.
Parameters: - address – str for unix socket path, or tuple for (host, port) tcp connection.
- encoding – this argument can be used to decode byte-replies to strings. By default no decoding is done.
- commands_factory –
- loop – event loop to use
Returns: high-level Gibson connection Gibson
- class aiogibson.commands.Gibson(connection)[source]¶
High-level Gibson interface
See: http://gibson-db.in/commands/ - count(prefix)[source]¶
Count items for a given prefix.
Parameters: prefix – bytes The key prefix to use as expression Returns: int number of elements
- dec(key)[source]¶
Decrement by one the given key.
Parameters: key – bytes, key to decrement. Returns: int decremented value in case of success
- delete(key)[source]¶
Delete the given key.
Parameters: key – bytes key to delete. Returns: bool true in case of success.
- get(key)[source]¶
Get the value for a given key.
Parameters: key – bytes key to get. Returns: bytes if value exists else None
- inc(key)[source]¶
Increment by one the given key.
Parameters: key – bytes, key to increment. Returns: int incremented value
- keys(prefix)[source]¶
Return a list of keys matching the given prefix.
Parameters: prefix – key prefix to use as expression. Returns: list of available keys
- lock(key, expire=0)[source]¶
Prevent the given key from being modified for a given amount of seconds.
Parameters: - key – bytes, key to decrement.
- expire – int, time in seconds to lock the item.
Returns: bool
Raises TypeError: if expire argument is not int
- mdec(prefix)[source]¶
Decrement by one keys verifying the given prefix.
Parameters: prefix – prefix for keys. Returns: int, number of modified items, otherwise an error.
- mdelete(prefix)[source]¶
Delete keys verifying the given prefix.
Parameters: prefix – prefix for keys. Returns: int, number of modified items, otherwise an error.
- meta_access(key)[source]¶
Timestamp of the last time the item was accessed.
Parameters: key – bytes, key of interest. Returns: int, timestamp
- meta_created(key)[source]¶
Timestamp of item creation.
Parameters: key – bytes, key of interest. Returns: int, timestamp
- meta_encoding(key)[source]¶
Gibson encoding for given value.
Parameters: key – bytes, key of interest. Returns: int, gibson encoding, 0 - bytes, 2 - int.
- meta_left(key)[source]¶
Number of seconds left for the item to live if a ttl was specified, otherwise -1.
Parameters: key – bytes, key of interest. Returns: int, Number of seconds left.
- meta_lock(key)[source]¶
Number of seconds the item is locked, -1 if there’s no lock.
Parameters: key – bytes, key of interest. Returns: int, number of seconds
- meta_size(key)[source]¶
The size in bytes of the item value.
Parameters: key – bytes, key of interest. Returns: int, value size in bytes
- meta_ttl(key)[source]¶
Item specified time to live, -1 for infinite TTL.
Parameters: key – bytes, key of interest. Returns: int, seconds of TTL.
- mget(prefix)[source]¶
Get the values for keys with given prefix.
Parameters: prefix – prefix for keys. Returns: list of key/value pairs
- minc(prefix)[source]¶
Increment by one keys verifying the given prefix.
Parameters: prefix – prefix for keys. Returns: int, number of modified items, otherwise an error.
- mlock(prefix, expire=0)[source]¶
Prevent keys verifying the given prefix from being modified for a given amount of seconds.
Parameters: prefix – bytes, prefix for keys. :param expire:int, lock period in seconds. :return: int, number of modified items, otherwise an error. :raises TypeError: if expire argument is not int
- mset(prefix, value)[source]¶
Set the value for keys verifying the given prefix.
Parameters: prefix – prefix for keys. Returns: int, number of modified items, otherwise an error.
- mttl(prefix, expire=0)[source]¶
Set the TTL for keys verifying the given prefix.
Parameters: - prefix – prefix for keys.
- expire – int, new expiration time.
Returns: int, number of modified items, otherwise an error.
Raises TypeError: if expire argument is not int
- munlock(prefix)[source]¶
Remove the lock on keys verifying the given prefix.
Parameters: prefix – prefix for keys. Returns: int, number of affected items, otherwise an error.
- ping()[source]¶
Ping the server instance to refresh client last seen timestamp.
Returns: True or error.
- set(key, value, expire=0)[source]¶
Set the value for the given key, with an optional TTL.
Parameters: - key – bytes key to set.
- value – bytes value to set.
- expire – int optional ttl in seconds
Raises TypeError: if expire argument is not int