-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add caching to Pincer impl (As it doesnt have it)
- Loading branch information
Showing
5 changed files
with
193 additions
and
34 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,7 +1,8 @@ | ||
import logging | ||
from antispam.libs.shared.substitute_args import SubstituteArgs | ||
from antispam.libs.shared.base import Base | ||
from antispam.libs.shared.timed_cache import TimedCache | ||
|
||
__all__ = ("SubstituteArgs", "Base") | ||
__all__ = ("SubstituteArgs", "Base", "TimedCache") | ||
|
||
logging.getLogger(__name__).addHandler(logging.NullHandler()) |
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,80 @@ | ||
""" | ||
The MIT License (MIT) | ||
Copyright (c) 2020-Current Skelmis | ||
Permission is hereby granted, free of charge, to any person obtaining a | ||
copy of this software and associated documentation files (the "Software"), | ||
to deal in the Software without restriction, including without limitation | ||
the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
and/or sell copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
DEALINGS IN THE SOFTWARE. | ||
""" | ||
# Taken from https://github.com/Skelmis/DPY-Bot-Base/tree/master/bot_base/caches | ||
from datetime import timedelta, datetime | ||
from typing import Any, Dict, Optional | ||
|
||
import attr | ||
|
||
from antispam.exceptions import NonExistentEntry, ExistingEntry | ||
|
||
|
||
@attr.s(slots=True) | ||
class Entry: | ||
value: Any = attr.ib() | ||
expiry_time: Optional[datetime] = attr.ib(default=None) | ||
|
||
|
||
class TimedCache: | ||
__slots__ = ("cache",) | ||
|
||
def __init__(self): | ||
self.cache: Dict[Any, Entry] = {} | ||
|
||
def __contains__(self, item: Any) -> bool: | ||
try: | ||
entry = self.cache[item] | ||
if entry.expiry_time < datetime.now(): | ||
self.delete_entry(item) | ||
return False | ||
except KeyError: | ||
return False | ||
else: | ||
return True | ||
|
||
def add_entry( | ||
self, key: Any, value: Any, *, ttl: timedelta = None, override: bool = False | ||
) -> None: | ||
ttl = ttl or timedelta() | ||
if key in self and not override: | ||
raise ExistingEntry | ||
|
||
self.cache[key] = Entry(value=value, expiry_time=(datetime.now() + ttl)) | ||
|
||
def delete_entry(self, key: Any) -> None: | ||
try: | ||
self.cache.pop(key) | ||
except KeyError: | ||
pass | ||
|
||
def get_entry(self, key: Any) -> Any: | ||
if key not in self: | ||
raise NonExistentEntry | ||
|
||
return self.cache[key].value | ||
|
||
def force_clean(self) -> None: | ||
now = datetime.now() | ||
for k, v in self.cache.items(): | ||
if v.expiry_time < now: | ||
self.delete_entry(k) |
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