main
 1#!/usr/bin/env python
 2# -*- coding: utf-8 -*-
 3from urllib.parse import quote_plus, unquote_plus
 4
 5from loguru import logger
 6
 7from config import cache
 8
 9
10def get_memory_cache(key: str, *, silent: bool = False) -> dict:
11    """Get from memory cache."""
12    key = quote_plus(unquote_plus(key))
13    if kv := cache.get(key):
14        if not silent:
15            logger.trace(f"GET DB from memory cache for {key}: {kv}")
16        return kv
17    return {}
18
19
20def set_memory_cache(key: str, data: dict | list | str, ttl: int | None = None, *, silent: bool = False) -> None:
21    """Set to memory cache."""
22    if ttl is None:
23        ttl = 600
24    key = quote_plus(unquote_plus(key))
25    cache.set(key, data, ttl=ttl)
26    if not silent:
27        logger.trace(f"SET DB to memory cache for {key}: {data}")
28
29
30def del_memory_cache(key: str, *, silent: bool = False):
31    """Delete from memory cache."""
32    key = quote_plus(unquote_plus(key))
33    cache.delete(key)
34    if not silent:
35        logger.trace(f"DEL DB from memory cache for {key}")