feat: Update cache function
All checks were successful
Build Docker / BuildImage (push) Successful in 35s

This commit is contained in:
Nathan Woodburn 2024-12-05 14:32:26 +11:00
parent 5f3c88574d
commit e995df2e05
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
14 changed files with 22 additions and 37 deletions

View File

@ -19,24 +19,26 @@ def file_cache(folder="cache", ttl=3600):
def refresh_cache(func, cache_file, cache_key, args, kwargs):
def refresh_loop():
while True:
sleep(ttl)
print(f"Waiting for cache for {func.__name__} to expire...",flush=True)
sleep(ttl/2)
try:
# Check if the cache is less than half the ttl (wait for half ttl to pass)
if os.path.exists(cache_file):
with open(cache_file, "r") as f:
cached_data = json.load(f)
if time() - cached_data["timestamp"] < ttl / 2:
print(f"Cache for {func.__name__} is less than half the TTL, skipping refresh.",flush=True)
return
# Re-compute the result and update the cache
print(f"Refreshing cache for {func.__name__}...")
print(f"Refreshing cache for {func.__name__}...",flush=True)
result = func(*args, **kwargs)
with open(cache_file, "w") as f:
json.dump({"timestamp": time(), "result": result}, f)
except Exception as e:
print(f"Error during cache refresh: {e}")
print(f"Error during cache refresh: {e}",flush=True)
threading.Thread(target=refresh_loop, daemon=True).start()
threading.Thread(target=refresh_loop).start()
def decorator(func):
@ -50,7 +52,7 @@ def file_cache(folder="cache", ttl=3600):
# Start a background thread for auto-refresh if it doesn't exist
if not os.path.exists(cache_file):
print(f"Creating cache for {func.__name__} with auto-refresh.")
print(f"Creating cache for {func.__name__} with auto-refresh.",flush=True)
result = func(*args, **kwargs)
with open(cache_file, "w") as f:
json.dump({"timestamp": time(), "result": result}, f)
@ -65,6 +67,8 @@ def file_cache(folder="cache", ttl=3600):
cached_data = json.load(f)
# Check if the cache has expired
if time() - cached_data["timestamp"] < ttl:
print(f"Cache valid for {func.__name__}.",flush=True)
refresh_cache(func, cache_file, cache_key, args, kwargs)
return cached_data["result"]
except (IOError, ValueError, KeyError):
pass # In case of error, re-compute the result

View File

@ -1 +1 @@
{"timestamp": 1733363360.6520803, "result": "120"}
{"timestamp": 1733367496.996954, "result": "120"}

View File

@ -1 +1 @@
{"timestamp": 1733363478.77673, "result": 1.14}
{"timestamp": 1733367305.82001, "result": 1.18}

View File

@ -1 +1 @@
{"timestamp": 1733363478.8822353, "result": 4.16}
{"timestamp": 1733367305.1029575, "result": 4.18}

View File

@ -1 +1 @@
{"timestamp": 1733363358.2618918, "result": 240.01}
{"timestamp": 1733367303.484976, "result": 248.08}

View File

@ -1 +1 @@
{"timestamp": 1733363355.9661899, "result": 226.21}
{"timestamp": 1733367300.1864865, "result": 231.97}

View File

@ -1 +1 @@
{"timestamp": 1733363361.094433, "result": 120.0}
{"timestamp": 1733367298.2447531, "result": 120.0}

View File

@ -1 +1 @@
{"timestamp": 1733363355.9663823, "result": 1.47422459502}
{"timestamp": 1733367300.1870453, "result": 1.5117628721399998}

View File

@ -1 +1 @@
{"timestamp": 1733363357.525003, "result": 0.999445}
{"timestamp": 1733367301.6211267, "result": 1.001}

View File

@ -1 +1 @@
{"timestamp": 1733363478.8832045, "result": [{"mint": "jupSoLaHXQiZZTSfEWMTRRgpnyFm8f6sZdosWBjx93v", "balance": 0.039815492, "price": 240.01, "value": 9.55611623492, "name": "Jupiter Staked SOL", "symbol": "jupsol"}, {"mint": "27G8MtK7VtTcCHkpASjSDdkWWYfoqT6ggEuKidVJidD4", "balance": 2.402337, "price": 4.16, "value": 9.99372192, "name": "Jupiter Perpetuals Liquidity Provider Token", "symbol": "jlp"}]}
{"timestamp": 1733367305.106161, "result": [{"mint": "jupSoLaHXQiZZTSfEWMTRRgpnyFm8f6sZdosWBjx93v", "balance": 0.039815492, "price": 248.08, "value": 9.87742725536, "name": "Jupiter Staked SOL", "symbol": "jupsol"}, {"mint": "27G8MtK7VtTcCHkpASjSDdkWWYfoqT6ggEuKidVJidD4", "balance": 2.402337, "price": 4.18, "value": 10.04176866, "name": "Jupiter Perpetuals Liquidity Provider Token", "symbol": "jlp"}]}

View File

@ -1 +1 @@
{"timestamp": 1733363478.7767982, "result": 115.43342152993999}
{"timestamp": 1733367305.8206198, "result": 119.1529266475}

View File

@ -1 +1 @@
{"timestamp": 1733363419.3752136, "result": 82.815227}
{"timestamp": 1733367305.512328, "result": 82.815227}

View File

@ -39,8 +39,6 @@ if __name__ == '__main__':
'threads': threads,
}
threading.Thread(target=server.update_data).start()
gunicorn_app = GunicornApp(server.app, options)
print(f'Starting server with {workers} workers and {threads} threads', flush=True)
gunicorn_app.run()

View File

@ -317,8 +317,8 @@ def api_token():
# Get balance of vault
token["SOL"] = {
"name": "Solana",
"amount": getSolBalance() / supply,
"value": getSolValue() / supply
"amount": round(getSolBalance() / supply,4),
"value": round(getSolValue() / supply,2)
}
if token["SOL"]["value"] < 0.01:
token["SOL"]["amount"] = 0
@ -377,23 +377,6 @@ def not_found(e):
# endregion
# region Background Threads
def update_data():
try:
print("Updating Solana data...")
getSolValue()
getTokens()
getVaultBalance()
print("Updating Cardano data...")
getCardanoBalance(vault_cardano_address)
getCardanoValue(vault_cardano_address)
print("Updating data complete.")
except Exception as e:
print(f"Error updating data: {e}")
# endregion
if __name__ == "__main__":
threading.Thread(target=update_data).start()
app.run(debug=True, port=5000, host="0.0.0.0")