Skip to content

Commit 3c476f7

Browse files
Merge pull request #18 from SebastiaanZ/aioredis-v2
Migrate To aioredis v2
2 parents 6a68850 + c9b40dd commit 3c476f7

File tree

12 files changed

+505
-505
lines changed

12 files changed

+505
-505
lines changed

Pipfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ coveralls = "~=2.2.0"
1818
time-machine = "~=1.3.0"
1919

2020
[packages]
21-
aioredis = "~=1.3.1"
22-
"fakeredis[lua]" = "~=1.4.4"
21+
aioredis = "~=2.0"
22+
"fakeredis[lua]" = "~=1.7.1"
2323

2424
[requires]
2525
python_version = "3.8"

Pipfile.lock

Lines changed: 280 additions & 173 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,22 @@ pip install async-rediscache[fakeredis]
2828
## Basic use
2929

3030
### Creating a `RedisSession`
31-
To use a `RedisCache`, you first have to create a `RedisSession` instance that manages the connection pool to Redis. You can create the `RedisSession` at any point but make sure to call the `connect` method from an asynchronous context (see [this explanation](https://docs.aiohttp.org/en/stable/faq.html#why-is-creating-a-clientsession-outside-of-an-event-loop-dangerous) for why).
31+
To use a `RedisCache`, you first have to create a `RedisSession` instance that manages the connection to Redis. You can create the `RedisSession` at any point but make sure to call the `connect` method from an asynchronous context (see [this explanation](https://docs.aiohttp.org/en/stable/faq.html#why-is-creating-a-clientsession-outside-of-an-event-loop-dangerous) for why).
3232

3333
```python
3434
import async_rediscache
3535

3636
async def main():
37-
session = async_rediscache.RedisSession()
37+
session = async_rediscache.RedisSession("redis://localhost")
3838
await session.connect()
3939

4040
# Do something interesting
41-
42-
await session.close()
4341
```
4442

4543
### Creating a `RedisSession` with a network connection
4644

4745
```python
46+
import async_rediscache
4847
async def main():
4948
connection = {"address": "redis://127.0.0.1:6379"}
5049
async_rediscache.RedisSession(**connection)
@@ -84,7 +83,7 @@ Here are some usage examples:
8483
import async_rediscache
8584

8685
async def main():
87-
session = async_rediscache.RedisSession()
86+
session = async_rediscache.RedisSession("redis://localhost")
8887
await session.connect()
8988

9089
cache = async_rediscache.RedisCache(namespace="python")
@@ -110,8 +109,6 @@ async def main():
110109
await cache.delete("Barry")
111110
await cache.increment("Brett", 1) # Increment Brett's int by 1
112111
await cache.clear()
113-
114-
await session.close()
115112
```
116113

117114
#### `RedisQueue`

async_rediscache/session.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import aioredis
66

7-
__all__ = ['RedisSession', 'RedisSessionClosed', 'RedisSessionNotInitialized']
7+
__all__ = ['RedisSession', 'RedisSessionNotInitialized', 'RedisSessionNotConnected']
88

99
log = logging.getLogger(__name__)
1010

@@ -13,8 +13,8 @@ class RedisSessionNotInitialized(RuntimeError):
1313
"""Raised when the RedisSession instance has not been initialized yet."""
1414

1515

16-
class RedisSessionClosed(RuntimeError):
17-
"""Exception raised when something attempts operations on a closed redis session."""
16+
class RedisSessionNotConnected(RuntimeError):
17+
"""Raised when trying to access the Redis client before a connection has been created."""
1818

1919

2020
class FakeRedisNotInstalled(ImportError):
@@ -55,22 +55,26 @@ class RedisSession(metaclass=RedisSingleton):
5555
creates it, run the loop until the `connect` coroutine is completed, and
5656
passing the loop to `discord.ext.commands.Bot.__init__` afterwards.
5757
58+
The `url` argument, and kwargs are passed directly to the `aioredis.from_url` method.
59+
5860
Example:
59-
redis_session = RedisSession()
61+
redis_session = RedisSession("redis://localhost")
6062
loop = asyncio.get_event_loop()
61-
loop.run_until_complete(redis_session.connect(...))
63+
loop.run_until_complete(redis_session.connect())
6264
bot = discord.ext.commands.Bot(..., loop=loop)
6365
"""
6466

6567
_instance: RedisSession = None
6668

6769
def __init__(
68-
self, *, global_namespace: str = "", use_fakeredis: bool = False, **session_kwargs
70+
self, url: str, *, global_namespace: str = "", use_fakeredis: bool = False, **session_kwargs
6971
) -> None:
7072
self.global_namespace = global_namespace
71-
self._pool = None
73+
self.url = url
74+
self._client = None
7275
self._session_kwargs = session_kwargs
7376
self._use_fakeredis = use_fakeredis
77+
self.connected = False
7478

7579
@classmethod
7680
def get_current_session(cls) -> RedisSession:
@@ -86,28 +90,22 @@ def get_current_session(cls) -> RedisSession:
8690
return cls._instance
8791

8892
@property
89-
def pool(self) -> aioredis.Redis:
93+
def client(self) -> aioredis.Redis:
9094
"""
91-
Get the connection pool after checking if it is still connected.
95+
Get the redis client object to perform commands on.
9296
93-
This property will raise a `RedisSessionClosed` if it's accessed after
94-
the pool has been closed.
97+
This property will raise a `RedisSessionNotConnected` if it is accessed
98+
before the connect method is called.
9599
"""
96-
if self._pool.closed:
97-
raise RedisSessionClosed(
98-
"attempting to access connections pool after it has been closed."
100+
if not self.connected:
101+
raise RedisSessionNotConnected(
102+
"attempting to access the client before the connection has been created."
99103
)
100-
101-
return self._pool
102-
103-
@property
104-
def closed(self) -> bool:
105-
"""Return whether or not the RedisSession is closed."""
106-
return self._pool is None or self._pool.closed
104+
return self._client
107105

108106
async def connect(self) -> None:
109-
"""Connect to Redis by creating the connections pool."""
110-
log.debug("Creating Redis connections pool.")
107+
"""Connect to Redis by instantiating the redis instance."""
108+
log.debug("Creating Redis client.")
111109

112110
# Decide if we want to use `fakeredis` or an actual Redis server. The
113111
# option to use `fakeredis.aioredis` is provided to aid with running the
@@ -126,18 +124,20 @@ async def connect(self) -> None:
126124
)
127125

128126
kwargs = dict(self._session_kwargs)
127+
# Match the behavior of `aioredis.from_url` by updating the kwargs using the URL
128+
url_options = aioredis.connection.parse_url(self.url)
129+
kwargs.update(dict(url_options))
129130

130-
# the `address` and `password` kwargs are not supported by redis
131-
kwargs.pop("address", None)
132-
kwargs.pop("password", None)
131+
# The following kwargs are not supported by fakeredis
132+
[kwargs.pop(kwarg, None) for kwarg in (
133+
"address", "username", "password", "port", "timeout"
134+
)]
133135

134-
pool_constructor = fakeredis.aioredis.create_redis_pool(**kwargs)
136+
self._client = fakeredis.aioredis.FakeRedis(**kwargs)
135137
else:
136-
pool_constructor = aioredis.create_redis_pool(**self._session_kwargs)
137-
138-
self._pool = await pool_constructor
138+
self._client = aioredis.from_url(self.url, **self._session_kwargs)
139139

140-
async def close(self) -> None:
141-
"""Close the pool and wait for pending operations to be processed."""
142-
self._pool.close()
143-
await self._pool.wait_closed()
140+
# The connection pool client does not expose any way to connect to the server, so
141+
# we try to perform a request to confirm the connection
142+
await self._client.ping()
143+
self.connected = True

0 commit comments

Comments
 (0)