import asyncio

from dragons96_tools.cache import Cache, AsyncCache
from cachetools import TTLCache
from loguru import logger
from dragons96_tools.models import BaseModel


cache = Cache(cache=TTLCache(maxsize=100, ttl=60))
async_cache = AsyncCache(cache=TTLCache(maxsize=100, ttl=60))


class D(BaseModel):
    a: int


@cache.cacheable(key='func1_${a}_${b}')
def func1(a: int, b: str):
    return f'{a}_{b}'


@cache.cache_evict(key='func1_${a}_${b}')
def clear_func1_cache(a: int, b: str):
    pass


@cache.cache_put(key='func1_${a}_${b}')
def put_func1_cache(a: int, b: str):
    return f'{a}_{b}'


def test_cache():
    v1 = put_func1_cache(1, 's')
    v1 = func1(1, 's', D(a=123))
    logger.info("v1: {}", v1)
    clear_func1_cache(1, 's')
    v2 = func1(1, 's')
    logger.info("v2: {}", v2)


@async_cache.cacheable(key='func1_${a}_${b}')
async def async_func1(a: int, b: str):
    return f'{a}_{b}'


@async_cache.cache_evict(key='func1_${a}_${b}')
async def async_clear_func1_cache(a: int, b: str):
    pass


@async_cache.cache_put(key='func1_${a}_${b}')
async def async_put_func1_cache(a: int, b: str):
    return f'{a}_{b}'


def test_async_cache():
    v1 = asyncio.run(async_put_func1_cache(1, 's'))
    v1 = asyncio.run(async_func1(1, 's'))
    logger.info("v1: {}", v1)
    asyncio.run(async_clear_func1_cache(1, 's'))
    v2 = asyncio.run(async_func1(1, 's'))
    logger.info("v2: {}", v2)


