-
-
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
13 changed files
with
195 additions
and
178 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from typing import Callable | ||
from collections.abc import Coroutine | ||
from asyncio import Future, create_task | ||
|
||
_cache: dict[str, Future] = {} | ||
_cache_consumers: dict[str, int] = {} | ||
|
||
|
||
async def get_atomic_cache(key: str, get_cache: Callable[[], Coroutine]): | ||
if key not in _cache: | ||
_cache_consumers[key] = 0 | ||
_cache[key] = create_task(get_cache()) | ||
|
||
_cache_consumers[key] += 1 | ||
try: | ||
return await _cache[key] | ||
finally: | ||
_cache_consumers[key] -= 1 | ||
if _cache_consumers[key] == 0: | ||
del _cache[key] | ||
del _cache_consumers[key] |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from pydantic import BaseModel | ||
from sqlalchemy import JSON, TypeDecorator | ||
|
||
|
||
class PydanticModel(TypeDecorator): | ||
impl = JSON | ||
cache_ok = True | ||
|
||
def __init__(self, t_model: type[BaseModel], *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.t_model = t_model | ||
|
||
def process_bind_param(self, value, dialect): | ||
if value is None: | ||
return None | ||
return value.dict() | ||
|
||
def process_result_value(self, value, dialect): | ||
if value is None: | ||
return None | ||
return self.t_model.parse_obj(value) |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.