From fd0c4c1a26166e4e33c165f98f1543cd5ffe7247 Mon Sep 17 00:00:00 2001 From: Administrator Date: Mon, 30 May 2022 11:28:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E7=89=88=E6=9C=AC=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/api_v1/authz/authz.py | 1 - api/api_v1/endpoints/project.py | 1 - db/redisdb.py | 32 ++++++++++++++++++++++++++++++++ db/redisdb_utils.py | 12 ++++++++++++ utils/__init__.py | 1 - 5 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 db/redisdb.py create mode 100644 db/redisdb_utils.py diff --git a/api/api_v1/authz/authz.py b/api/api_v1/authz/authz.py index d029dd3..161d439 100644 --- a/api/api_v1/authz/authz.py +++ b/api/api_v1/authz/authz.py @@ -10,7 +10,6 @@ from db import get_database from db.ckdb import CKDrive, get_ck_db from db.redisdb import RedisDrive, get_redis_pool from models.behavior_analysis import BehaviorAnalysis -from utils import casbin_enforcer router = APIRouter() diff --git a/api/api_v1/endpoints/project.py b/api/api_v1/endpoints/project.py index 0eef266..f1285cf 100644 --- a/api/api_v1/endpoints/project.py +++ b/api/api_v1/endpoints/project.py @@ -9,7 +9,6 @@ from db import get_database from db.ckdb import CKDrive, get_ck_db from schemas.project import ProjectCreate # from utils import casbin_enforcer -from utils import casbin_enforcer router = APIRouter() diff --git a/db/redisdb.py b/db/redisdb.py new file mode 100644 index 0000000..f28d0c8 --- /dev/null +++ b/db/redisdb.py @@ -0,0 +1,32 @@ +from aredis import StrictRedis +import asyncio + + +class RedisDrive: + client: StrictRedis = None + + # async def get_keys(self, *keys, prefix='') -> list: + # res = [] + # for key in keys: + # key = prefix + key + # data = await self.client.get(key, encoding='utf8') + # res.append(data) + # return res + + async def smembers_keys(self, *keys, prefix='') -> dict: + tasks = [] + for key in keys: + task = asyncio.create_task(self.client.smembers(prefix + key)) + tasks.append(task) + data = await asyncio.gather(*tasks) + return {k: v for k, v in zip(keys, data)} + + def __getattr__(self, item): + return getattr(self.client, item) + + +rdb = RedisDrive() + + +def get_redis_pool() -> RedisDrive: + return rdb diff --git a/db/redisdb_utils.py b/db/redisdb_utils.py new file mode 100644 index 0000000..1a6cb48 --- /dev/null +++ b/db/redisdb_utils.py @@ -0,0 +1,12 @@ +from aredis import StrictRedis + +from core.config import settings +from .redisdb import RedisDrive + + +async def connect_to_redis(): + RedisDrive.client = StrictRedis(**settings.REDIS_CONF) + + +async def close_redis_connection(): + pass diff --git a/utils/__init__.py b/utils/__init__.py index c6019dd..32e7aea 100644 --- a/utils/__init__.py +++ b/utils/__init__.py @@ -1,4 +1,3 @@ -from .adapter import * from . import casbin from .func import *