-
Notifications
You must be signed in to change notification settings - Fork 1
/
CacheItem.py
82 lines (67 loc) · 2.19 KB
/
CacheItem.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import asyncio
import logging
unsubscribeDelay = 5000 # ms
class CacheItem(object):
# Creates a CacheItem instance
# @param {string} rid Resource ID
# @param {function} unsubscribe Unsubscribe callback
def __init__(self, rid, unsubscribe, loop):
self.rid = rid
self._unsubscribe = unsubscribe
self.type = None
self.item = None
self.direct = 0
self.indirect = 0
self.subscribed = False
self.promise = None
self.queues = dict()
self.tasks = dict()
self.loop = loop
self.unsubTimeout = None
self.logger = logging.getLogger()
def setSubscribed(self, isSubscribed):
self.subscribed = isSubscribed
if not isSubscribed and self.unsubTimeout:
self.unsubTimeout = None
return self
def setPromise(self, promise):
if not self.item:
self.promise = promise
return promise
def setItem(self, item, type):
self.item = item
self.type = type
self.promise = None
self._checkUnsubscribe()
return self
def setType(self, modelType):
self.type = modelType
return self
def addDirect(self):
if self.unsubTimeout:
self.unsubTimeout = None
self.direct += 1
def removeDirect(self):
self.direct -=1
if self.direct < 0:
raise Exception("Direct count reached below 0")
if self.subscribed:
self._checkUnsubscribe()
else:
# The subscription might be stale and should then be removed directly
self._unsubscribe(self)
def _checkUnsubscribe(self):
if not self.subscribed or self.direct or self.unsubTimeout:
return
self.unsubTimeout = True
async def f():
await asyncio.sleep(unsubscribeDelay/1000)
if self.unsubTimeout:
self._unsubscribe()
asyncio.run_coroutine_threadsafe(f(), self.loop)
def addIndirect(self, n = 1):
self.indirect += n
def removeIndirect(self, n = 1):
self.indirect -= n
if self.indirect < 0:
raise Exception("Indirect count reached below 0")