1
0
Fork 0
onnx-web/api/onnx_web/server/model_cache.py

62 lines
1.8 KiB
Python
Raw Normal View History

from logging import getLogger
2023-02-14 00:12:40 +00:00
from typing import Any, List, Tuple
logger = getLogger(__name__)
class ModelCache:
2023-02-14 00:12:40 +00:00
cache: List[Tuple[str, Any, Any]]
limit: int
def __init__(self, limit: int) -> None:
2023-02-14 00:15:02 +00:00
self.cache = []
self.limit = limit
def drop(self, tag: str, key: Any) -> int:
2023-03-06 01:30:52 +00:00
logger.debug("dropping item from cache: %s", tag)
removed = [
model for model in self.cache if model[0] == tag and model[1] == key
]
for item in removed:
self.cache.remove(item)
return len(removed)
def get(self, tag: str, key: Any) -> Any:
for t, k, v in self.cache:
if tag == t and key == k:
2023-03-06 03:50:01 +00:00
logger.debug("found cached model: %s %s", tag, key)
return v
2023-03-06 03:50:01 +00:00
logger.debug("model not found in cache: %s %s", tag, key)
return None
def set(self, tag: str, key: Any, value: Any) -> None:
if self.limit == 0:
2023-02-17 00:42:05 +00:00
logger.debug("cache limit set to 0, not caching model: %s", tag)
return
for i in range(len(self.cache)):
t, k, v = self.cache[i]
2023-02-19 13:53:20 +00:00
if tag == t and key != k:
2023-03-06 03:50:01 +00:00
logger.debug("updating model cache: %s %s", tag, key)
2023-02-19 13:53:20 +00:00
self.cache[i] = (tag, key, value)
return
2023-03-06 03:50:01 +00:00
logger.debug("adding new model to cache: %s %s", tag, key)
self.cache.append((tag, key, value))
self.prune()
def prune(self):
total = len(self.cache)
if total > self.limit:
logger.info(
2023-03-06 01:30:52 +00:00
"removing models from cache, %s of %s", (total - self.limit), total
)
self.cache[:] = self.cache[-self.limit :]
else:
2023-02-17 00:42:05 +00:00
logger.debug("model cache below limit, %s of %s", total, self.limit)
@property
def size(self):
return len(self.cache)