1
0
Fork 0

fix(api): track items removed from cache

This commit is contained in:
Sean Sube 2023-03-06 07:34:09 -06:00
parent 339868de4d
commit af1c3c7839
Signed by: ssube
GPG Key ID: 3EED7B957D362AF1
4 changed files with 43 additions and 5 deletions

View File

@ -17,8 +17,8 @@ pip-dev: check-venv
test:
python -m coverage run -m unittest discover -s tests/
python -m coverage html
python -m coverage xml
python -m coverage html -i
python -m coverage xml -i
package: package-dist package-upload

View File

@ -12,11 +12,15 @@ class ModelCache:
self.cache = []
self.limit = limit
def drop(self, tag: str, key: Any) -> None:
def drop(self, tag: str, key: Any) -> int:
logger.debug("dropping item from cache: %s", tag)
self.cache[:] = [
model for model in self.cache if model[0] != tag and model[1] != key
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:
@ -52,3 +56,7 @@ class ModelCache:
self.cache[:] = self.cache[-self.limit :]
else:
logger.debug("model cache below limit, %s of %s", total, self.limit)
@property
def size(self):
return len(self.cache)

View File

View File

@ -0,0 +1,30 @@
import unittest
from onnx_web.server.model_cache import ModelCache
class TestStringMethods(unittest.TestCase):
def test_drop_existing(self):
cache = ModelCache(10)
cache.set("foo", ("bar",), {})
self.assertGreater(cache.size, 0)
self.assertEqual(cache.drop("foo", ("bar",)), 1)
def test_drop_missing(self):
cache = ModelCache(10)
cache.set("foo", ("bar",), {})
self.assertGreater(cache.size, 0)
self.assertEqual(cache.drop("foo", ("bin",)), 0)
def test_get_existing(self):
cache = ModelCache(10)
value = {}
cache.set("foo", ("bar",), value)
self.assertGreater(cache.size, 0)
self.assertIs(cache.get("foo", ("bar",)), value)
def test_get_missing(self):
cache = ModelCache(10)
value = {}
cache.set("foo", ("bar",), value)
self.assertGreater(cache.size, 0)
self.assertIs(cache.get("foo", ("bin",)), None)