-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.py
30 lines (24 loc) · 866 Bytes
/
cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from collections import OrderedDict
class Cache:
"""
Класс для кэширования положения окна обрезки и скорости объекта в кадре
"""
def __init__(self, size=100000):
self.max_size = size
self.cache = OrderedDict()
def get(self, key):
if key not in self.cache:
return None
else:
self.cache.move_to_end(key)
return self.cache[key]
def put(self, key, value):
self.cache[key] = value
self.cache.move_to_end(key)
if self.cache.__len__() > self.max_size:
self.cache.popitem(last=False)
def get_from_cache(self, cur_id, func, *args, **kwargs):
if cur_id in self.cache:
return self.cache[cur_id]
else:
return func(cur_id, *args, **kwargs)