forked from kroketio/quart-session
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
88 lines (65 loc) · 2.46 KB
/
client.py
File metadata and controls
88 lines (65 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- coding: utf-8 -*-
"""
quart_session.redis_trio
~~~~~~~~~~~~~~~~~~~~~~
A simple Redis Trio client.
:copyright: (c) 2017 by Bogdan Paul Popa.
:copyright: (c) 2019 by Oleksii Aleksieiev.
:copyright: (c) 2020 by dsc.
:license: BSD, see LICENSE for more details.
"""
from typing import Union
from .connection import RedisConnection
class RedisTrio:
"""A simple Redis+Trio client.
Parameters:
addr(str): The IP address the Redis server is listening on.
port(int): The port the Redis server is listening on.
Examples:
>>> async with RedisTrio() as redis:
... await redis.set("foo", 42)
... await redis.get("foo")
b'42'
"""
def __init__(self, addr: Union[bytes, str] = b"127.0.0.1", port: int = 6379, password: bytes = b""):
self.conn = RedisConnection(addr, port)
self.password = password
async def connect(self):
"""Open a connection to the Redis server.
Returns:
RedisTrio: This instance.
"""
await self.conn.connect()
if self.password:
await self.auth(self.password)
return self
async def close(self):
"""Close the connection to the Redis server.
"""
await self.quit()
self.conn.close()
async def auth(self, password):
return await self.conn.process_command_ok(b"AUTH", password)
async def delete(self, *keys):
return await self.conn.process_command(b"DEL", *keys)
async def echo(self, message):
return await self.conn.process_command(b"ECHO", message)
async def flushall(self):
return await self.conn.process_command_ok(b"FLUSHALL")
async def get(self, key) -> bytes:
return await self.conn.process_command(b"GET", key)
async def quit(self):
return await self.conn.process_command(b"QUIT")
async def set(self, key, value):
return await self.conn.process_command_ok(b"SET", key, value)
async def setex(self, key: str, value: str, seconds: int):
"""Set the value and expiration of a key.
:raises TypeError: if seconds is not int
"""
if not isinstance(seconds, int):
raise TypeError("milliseconds argument must be int")
return await self.conn.process_command_ok(b"SETEX", key, seconds, value)
async def __aenter__(self):
return await self.connect()
async def __aexit__(self, exc_type, exc_value, traceback):
self.close()