-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This was a legacy class, we now use built-in decorator or cachetools classes
- Loading branch information
1 parent
5e86341
commit 5d8c007
Showing
4 changed files
with
12 additions
and
62 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
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,35 +1,3 @@ | ||
import datetime | ||
from typing import Callable, Optional | ||
|
||
from robotoff.utils import get_logger | ||
|
||
logger = get_logger(__name__) | ||
|
||
|
||
class CachedStore: | ||
def __init__(self, fetch_func: Callable, expiration_interval: Optional[int] = 30): | ||
self.store = None | ||
self.expires_after: Optional[datetime.datetime] = None | ||
self.fetch_func: Callable = fetch_func | ||
self.expiration_timedelta: Optional[datetime.timedelta] | ||
|
||
if expiration_interval is not None: | ||
self.expiration_timedelta = datetime.timedelta(minutes=expiration_interval) | ||
else: | ||
self.expiration_timedelta = None | ||
|
||
def get(self, **kwargs): | ||
if self.store is None or ( | ||
self.expiration_timedelta is not None | ||
and datetime.datetime.utcnow() >= self.expires_after | ||
): | ||
if self.store is not None: | ||
logger.info("CachedStore expired, reloading...") | ||
|
||
if self.expiration_timedelta is not None: | ||
self.expires_after = ( | ||
datetime.datetime.utcnow() + self.expiration_timedelta | ||
) | ||
self.store = self.fetch_func(**kwargs) | ||
|
||
return self.store |