-
Notifications
You must be signed in to change notification settings - Fork 26
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
5 changed files
with
307 additions
and
99 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,29 @@ | ||
import asyncio | ||
import threading | ||
from abc import ABC, abstractmethod | ||
from typing import Any, Union, Hashable, Dict, Optional | ||
|
||
from llist import dllist, dllistnode | ||
from typing import Any, Hashable | ||
|
||
|
||
class CacheBase(ABC): | ||
def __init__(self, max_size: int = 0): | ||
self._max_size: int = max_size | ||
self._loop: Optional[asyncio.AbstractEventLoop] = None | ||
self.usages: dllist = dllist() | ||
self.cache: Dict[Hashable, Any] = dict() | ||
self.lock = threading.RLock() | ||
|
||
@property | ||
def is_overflow(self) -> bool: | ||
if self._max_size == 0: | ||
return False | ||
|
||
if self._max_size < len(self.usages): | ||
return True | ||
__slots__ = "max_size", | ||
|
||
return False | ||
|
||
@property | ||
def loop(self) -> asyncio.AbstractEventLoop: | ||
if self._loop is None: | ||
self._loop = asyncio.get_event_loop() | ||
return self._loop | ||
def __init__(self, max_size: int = 0): | ||
self.max_size = max_size | ||
|
||
@abstractmethod | ||
def _on_set(self, node: dllistnode) -> None: | ||
pass | ||
|
||
def _on_expires(self, node: dllistnode) -> None: | ||
pass | ||
def get(self, key: Hashable): | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def _on_get(self, node: dllistnode) -> Any: | ||
pass | ||
|
||
def __contains__(self, item: Hashable) -> bool: | ||
return item in self.cache | ||
def remove(self, key: Hashable): | ||
raise NotImplementedError | ||
|
||
def get(self, item: Hashable) -> Any: | ||
with self.lock: | ||
node: dllistnode = self.cache[item] | ||
self.loop.call_soon(self._on_get, node) | ||
return node.value[1] | ||
|
||
def expire(self, node: dllistnode): | ||
with self.lock: | ||
item, value = node.value | ||
node: Optional[dllistnode] = self.cache.pop(item, None) | ||
|
||
if node is None: | ||
return | ||
|
||
self.loop.call_soon(self._on_expires, node) | ||
self.usages.remove(node) | ||
|
||
def set(self, item: Hashable, value: Any, | ||
expiration: Union[int, float] = None) -> None: | ||
with self.lock: | ||
node: dllistnode = self.usages.append((item, value)) | ||
self.cache[item] = node | ||
@abstractmethod | ||
def set(self, key: Hashable, value: Any): | ||
raise NotImplementedError | ||
|
||
self.loop.call_soon(self._on_set, node) | ||
@abstractmethod | ||
def __contains__(self, key: Hashable): | ||
raise NotImplementedError | ||
|
||
if expiration is not None: | ||
self.loop.call_later(expiration, self.expire, node) | ||
@abstractmethod | ||
def __len__(self): | ||
raise NotImplementedError |
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.