# Copyright (C) 2019-2020 Intel Corporation
#
# SPDX-License-Identifier: MIT
from collections import OrderedDict
_instance = None
DEFAULT_CAPACITY = 2
[docs]class ImageCache:
[docs] @staticmethod
def get_instance():
global _instance
if _instance is None:
_instance = ImageCache()
return _instance
[docs] def __init__(self, capacity=DEFAULT_CAPACITY):
self.capacity = int(capacity)
self.items = OrderedDict()
[docs] def push(self, item_id, image):
if self.capacity <= len(self.items):
self.items.popitem(last=True)
self.items[item_id] = image
[docs] def get(self, item_id):
default = object()
item = self.items.get(item_id, default)
if item is default:
return None
self.items.move_to_end(item_id, last=False) # naive splay tree
return item
[docs] def size(self):
return len(self.items)
[docs] def clear(self):
self.items.clear()