Commit 20ab7ad1 authored by Massimo Costantini's avatar Massimo Costantini
Browse files

Renamed redis to conn

parent 0b0085ad
Loading
Loading
Loading
Loading
+12 −14
Original line number Diff line number Diff line
@@ -9,9 +9,7 @@ from Lib.IO import put_str

from redis.asyncio import Redis

from typing import Awaitable, Callable
from typing import Dict

from typing import Awaitable, Callable, Dict
import json

with open("Conf/Redis.json", "r") as f:
@@ -29,15 +27,15 @@ def conn_redis() -> Callable[[object], Awaitable[Redis]]:
    """

    async def inner(io: object) -> Redis:
        client = Redis(host=config["host"], port=config["port"])
        conn = Redis(host=config["host"], port=config["port"])
        await put_str("Connected to Redis")(io)
        return client
        return conn

    return inner


def set_redis(
    redis: Redis,
    conn: Redis,
) -> Callable[[Dict[str, str]], Callable[[object], Awaitable[object]]]:
    """
    Stores a key-value pair in Redis.
@@ -45,7 +43,7 @@ def set_redis(
    Functional signature: RedisConn -> { key: String, value: String } -> IO -> IO ()

    Args:
        redis: A connected Redis client.
        conn: A connected Redis client.

    Returns:
        Callable[[Dict[str, str]], Callable[[IO], Awaitable[IO]]]: A CPS IO action that sets a key-value pair.
@@ -53,7 +51,7 @@ def set_redis(

    def inner_cmd(command: Dict[str, str]) -> Callable[[object], Awaitable[object]]:
        async def inner(io: object) -> object:
            await redis.set(command["key"], command["value"])
            await conn.set(command["key"], command["value"])
            await put_str(f"Redis: set {command['key']}")(io)
            return io

@@ -63,7 +61,7 @@ def set_redis(


def get_redis(
    redis: Redis,
    conn: Redis,
) -> Callable[[str], Callable[[object], Awaitable[Dict[str, str]]]]:
    """
    Retrieves a value from Redis by key.
@@ -71,7 +69,7 @@ def get_redis(
    Functional signature: RedisConn -> String -> IO -> IO { tag: "just", value: String } | { tag: "nothing" }

    Args:
        redis: A connected Redis client.
        conn: A connected Redis client.

    Returns:
        Callable[[str], Callable[[IO], Awaitable[Dict]]]: A CPS IO action that retrieves a key from Redis.
@@ -79,7 +77,7 @@ def get_redis(

    def inner_cmd(key: str) -> Callable[[object], Awaitable[Dict[str, str]]]:
        async def inner(io: object) -> Dict[str, str]:
            value = await redis.get(key)
            value = await conn.get(key)
            if value is not None:
                await put_str(f"Redis: get {key} -> HIT")(io)
                return {"tag": "just", "value": value.decode("utf-8")}
@@ -92,21 +90,21 @@ def get_redis(
    return inner_cmd


def quit_redis(redis: Redis) -> Callable[[object], Awaitable[object]]:
def quit_redis(conn: Redis) -> Callable[[object], Awaitable[object]]:
    """
    Closes the Redis connection.

    Functional signature: RedisConn -> IO -> IO ()

    Args:
        redis: A connected Redis client.
        conn: A connected Redis client.

    Returns:
        Callable[[IO], Awaitable[IO]]: A CPS IO action that closes the Redis connection.
    """

    async def inner(io: object) -> object:
        await redis.close()
        await conn.close()
        await put_str("Redis connection closed")(io)
        return io