Skip to content

Commit

Permalink
switch to redis.asyncio & add redis config
Browse files Browse the repository at this point in the history
  • Loading branch information
cmyui committed Nov 5, 2023
1 parent c99b1ee commit b2f01dc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@ DB_PORT=3306
DB_PASS=your_db_password
DB_USER=root
DB_NAME=akatsuki
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_USER=default
REDIS_PASS=
REDIS_DB=0
REDIS_USE_SSL=false
DISCORD_AC_WEBHOOK=https://discordapp.com/api/webhooks/your_webhook
SERVICE_READINESS_TIMEOUT=60
26 changes: 16 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#!/usr/bin/env python3.9
import asyncio
import time
from typing import Any
from typing import cast

from cmyui.mysql import AsyncSQLPool
from cmyui.discord import Webhook, Embed
from aiohttp import ClientSession

import aioredis
import asyncio
import redis.asyncio as aioredis
import uvloop
import time
from aiohttp import ClientSession
from cmyui.discord import Webhook, Embed
from cmyui.mysql import AsyncSQLPool

import settings

Expand All @@ -29,16 +28,23 @@ async def connect() -> None:
)

global redis
redis = await aioredis.create_redis_pool("redis://localhost:6379/0")

redis = aioredis.Redis(
host=settings.REDIS_HOST,
port=settings.REDIS_PORT,
username=settings.REDIS_USER,
password=settings.REDIS_PASS,
db=settings.REDIS_DB,
ssl=settings.REDIS_USE_SSL,
)

print("Connected to database and redis")


async def disconnect() -> None:
await db.close()

redis.close()
await redis.wait_closed()
await redis.close()

print("Disconnected from database and redis")

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
aiohttp==3.8.1
aiomysql==0.1.1
aioredis==1.3.1
aiosignal==1.2.0
async-timeout==4.0.2
attrs==21.4.0
Expand Down Expand Up @@ -31,6 +30,7 @@ PyMySQL==1.0.2
pyparsing==3.0.9
pyrsistent==0.19.2
python-dotenv==1.0.0
redis==5.0.1
six==1.16.0
tenacity==8.1.0
tomli==2.0.1
Expand Down
12 changes: 12 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@

load_dotenv()


def read_bool(value: str) -> bool:
return value.lower() in ("1", "true")


DB_NAME = os.environ["DB_NAME"]
DB_HOST = os.environ["DB_HOST"]
DB_PORT = int(os.environ["DB_PORT"])
DB_PASS = os.environ["DB_PASS"]
DB_USER = os.environ["DB_USER"]

REDIS_HOST = os.environ["REDIS_HOST"]
REDIS_PORT = int(os.environ["REDIS_PORT"])
REDIS_USER = os.environ["REDIS_USER"]
REDIS_PASS = os.environ["REDIS_PASS"]
REDIS_DB = int(os.environ["REDIS_DB"])
REDIS_USE_SSL = read_bool(os.environ["REDIS_USE_SSL"])

DISCORD_AC_WEBHOOK = os.environ["DISCORD_AC_WEBHOOK"]

0 comments on commit b2f01dc

Please sign in to comment.