-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
117 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
from ultra_cache.main import get_storage, init_cache | ||
from fakeredis import FakeAsyncRedis | ||
from ultra_cache.decorator import UltraCache | ||
from ultra_cache.storage.inmemory import InMemoryStorage | ||
import pytest | ||
|
||
from ultra_cache.storage.redis import RedisStorage | ||
|
||
def test_init_cache(): | ||
with pytest.raises(ValueError): | ||
get_storage() | ||
|
||
created_storage = InMemoryStorage() | ||
init_cache(created_storage) | ||
s2 = get_storage() | ||
@pytest.fixture(params=[InMemoryStorage(), RedisStorage(FakeAsyncRedis())]) | ||
def storage(request): | ||
return request.param | ||
|
||
assert created_storage == s2 | ||
|
||
def test_init_cache(storage): | ||
cache = UltraCache(storage=storage) | ||
|
||
assert isinstance(cache.storage, type(storage)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
from fastapi import FastAPI | ||
from ultra_cache.decorator import cache | ||
from ultra_cache.decorator import UltraCache | ||
from ultra_cache.storage.inmemory import InMemoryStorage | ||
|
||
app = FastAPI() | ||
storage = InMemoryStorage() | ||
cache = UltraCache(storage=storage) | ||
|
||
|
||
@app.get("/items/{item_id}") | ||
@cache(storage=storage) | ||
@cache() | ||
async def read_item(item_id: int): | ||
return {"item_id": item_id} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +0,0 @@ | ||
from contextlib import AbstractAsyncContextManager | ||
from typing import Union | ||
from ultra_cache.storage.base import BaseStorage | ||
|
||
_storage_instance: Union[BaseStorage, None] = None | ||
|
||
|
||
def init_cache(storage: BaseStorage) -> None: | ||
global _storage_instance | ||
_storage_instance = storage | ||
|
||
|
||
def get_storage() -> BaseStorage: | ||
if _storage_instance is None: | ||
raise ValueError("Cache not initialized") | ||
|
||
return _storage_instance | ||
|
||
|
||
class FastCache(AbstractAsyncContextManager): | ||
storage: Union[BaseStorage, None] = None | ||
|
||
def __init__(self) -> None: | ||
pass | ||