Skip to content

Commit 1a10115

Browse files
committed
Added local cache for check cache contains in case we are checking with a list. Reduces api requests drastically, but MAY cause minor inconsistencies
1 parent 7bac3f1 commit 1a10115

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

shuffle-tools/1.2.0/src/app.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,20 @@ def check_cache_contains(self, key, value, append):
16491649
"key": key,
16501650
}
16511651

1652+
allvalues = {}
1653+
try:
1654+
for item in self.local_storage:
1655+
if item["execution_id"] == self.current_execution_id and item["key"] == key:
1656+
# Max keeping the local cache properly for 5 seconds due to workflow continuations
1657+
elapsed_time = time.time() - item["time_set"]
1658+
if elapsed_time > 5:
1659+
break
1660+
1661+
allvalues = item["data"]
1662+
1663+
except Exception as e:
1664+
print("[ERROR] Failed cache contains for current execution id local storage: %s" % e)
1665+
16521666
if isinstance(value, dict) or isinstance(value, list):
16531667
try:
16541668
value = json.dumps(value)
@@ -1665,9 +1679,13 @@ def check_cache_contains(self, key, value, append):
16651679
else:
16661680
append = False
16671681

1668-
get_response = requests.post(url, json=data, verify=False)
1682+
if "success" not in allvalues:
1683+
get_response = requests.post(url, json=data, verify=False)
1684+
16691685
try:
1670-
allvalues = get_response.json()
1686+
if "success" not in allvalues:
1687+
allvalues = get_response.json()
1688+
16711689
try:
16721690
if allvalues["value"] == None or allvalues["value"] == "null":
16731691
allvalues["value"] = "[]"
@@ -1686,6 +1704,7 @@ def check_cache_contains(self, key, value, append):
16861704
#allvalues["key"] = key
16871705
#return allvalues
16881706

1707+
16891708
return {
16901709
"success": True,
16911710
"found": False,
@@ -1727,6 +1746,14 @@ def check_cache_contains(self, key, value, append):
17271746
#return "%s %s" % (item, value)
17281747
if item == value:
17291748
if not append:
1749+
try:
1750+
newdata = json.loads(json.dumps(data))
1751+
newdata["time_set"] = time.time()
1752+
newdata["data"] = allvalues
1753+
self.local_storage.append(newdata)
1754+
except Exception as e:
1755+
print("[ERROR] Failed in local storage append: %s" % e)
1756+
17301757
return {
17311758
"success": True,
17321759
"found": True,
@@ -1798,6 +1825,7 @@ def check_cache_contains(self, key, value, append):
17981825
#return allvalues
17991826

18001827
except Exception as e:
1828+
print("[ERROR] Failed check cache contains: %s" % e)
18011829
return {
18021830
"success": False,
18031831
"key": key,

0 commit comments

Comments
 (0)