mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2025-03-31 18:56:19 +00:00
feat: Add redis pool singleton
This commit is contained in:
parent
8cdacb73dd
commit
19a4ec5377
2 changed files with 33 additions and 0 deletions
32
selfprivacy_api/utils/redis_pool.py
Normal file
32
selfprivacy_api/utils/redis_pool.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
"""
|
||||
Redis pool module for selfprivacy_api
|
||||
"""
|
||||
import redis.asyncio as redis
|
||||
from selfprivacy_api.utils.singleton_metaclass import SingletonMetaclass
|
||||
|
||||
REDIS_SOCKET = "/run/redis-sp-api/redis.sock"
|
||||
|
||||
|
||||
class RedisPool(metaclass=SingletonMetaclass):
|
||||
"""
|
||||
Redis connection pool singleton.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self._pool = redis.ConnectionPool.from_url(
|
||||
f"unix://{REDIS_SOCKET}",
|
||||
decode_responses=True,
|
||||
)
|
||||
self._pubsub_connection = self.get_connection()
|
||||
|
||||
def get_connection(self):
|
||||
"""
|
||||
Get a connection from the pool.
|
||||
"""
|
||||
return redis.Redis(connection_pool=self._pool)
|
||||
|
||||
def get_pubsub(self):
|
||||
"""
|
||||
Get a pubsub connection from the pool.
|
||||
"""
|
||||
return self._pubsub_connection.pubsub()
|
|
@ -18,6 +18,7 @@ let
|
|||
black
|
||||
fastapi
|
||||
uvicorn
|
||||
redis
|
||||
(buildPythonPackage rec {
|
||||
pname = "strawberry-graphql";
|
||||
version = "0.123.0";
|
||||
|
|
Loading…
Add table
Reference in a new issue