generated from nathanwoodburn/python-webserver-template
feat: Update cache function
All checks were successful
Build Docker / BuildImage (push) Successful in 35s
All checks were successful
Build Docker / BuildImage (push) Successful in 35s
This commit is contained in:
parent
5f3c88574d
commit
e995df2e05
14
cache.py
14
cache.py
@ -19,24 +19,26 @@ def file_cache(folder="cache", ttl=3600):
|
|||||||
def refresh_cache(func, cache_file, cache_key, args, kwargs):
|
def refresh_cache(func, cache_file, cache_key, args, kwargs):
|
||||||
def refresh_loop():
|
def refresh_loop():
|
||||||
while True:
|
while True:
|
||||||
sleep(ttl)
|
print(f"Waiting for cache for {func.__name__} to expire...",flush=True)
|
||||||
|
sleep(ttl/2)
|
||||||
try:
|
try:
|
||||||
# Check if the cache is less than half the ttl (wait for half ttl to pass)
|
# Check if the cache is less than half the ttl (wait for half ttl to pass)
|
||||||
if os.path.exists(cache_file):
|
if os.path.exists(cache_file):
|
||||||
with open(cache_file, "r") as f:
|
with open(cache_file, "r") as f:
|
||||||
cached_data = json.load(f)
|
cached_data = json.load(f)
|
||||||
if time() - cached_data["timestamp"] < ttl / 2:
|
if time() - cached_data["timestamp"] < ttl / 2:
|
||||||
|
print(f"Cache for {func.__name__} is less than half the TTL, skipping refresh.",flush=True)
|
||||||
return
|
return
|
||||||
|
|
||||||
# Re-compute the result and update the cache
|
# 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)
|
result = func(*args, **kwargs)
|
||||||
with open(cache_file, "w") as f:
|
with open(cache_file, "w") as f:
|
||||||
json.dump({"timestamp": time(), "result": result}, f)
|
json.dump({"timestamp": time(), "result": result}, f)
|
||||||
except Exception as e:
|
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):
|
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
|
# Start a background thread for auto-refresh if it doesn't exist
|
||||||
if not os.path.exists(cache_file):
|
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)
|
result = func(*args, **kwargs)
|
||||||
with open(cache_file, "w") as f:
|
with open(cache_file, "w") as f:
|
||||||
json.dump({"timestamp": time(), "result": result}, f)
|
json.dump({"timestamp": time(), "result": result}, f)
|
||||||
@ -65,6 +67,8 @@ def file_cache(folder="cache", ttl=3600):
|
|||||||
cached_data = json.load(f)
|
cached_data = json.load(f)
|
||||||
# Check if the cache has expired
|
# Check if the cache has expired
|
||||||
if time() - cached_data["timestamp"] < ttl:
|
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"]
|
return cached_data["result"]
|
||||||
except (IOError, ValueError, KeyError):
|
except (IOError, ValueError, KeyError):
|
||||||
pass # In case of error, re-compute the result
|
pass # In case of error, re-compute the result
|
||||||
|
2
cache/10118a51009b13b2592c87579e15e61e.json
vendored
2
cache/10118a51009b13b2592c87579e15e61e.json
vendored
@ -1 +1 @@
|
|||||||
{"timestamp": 1733363360.6520803, "result": "120"}
|
{"timestamp": 1733367496.996954, "result": "120"}
|
2
cache/1ccff5c6f117409fea0c861aa44b8e62.json
vendored
2
cache/1ccff5c6f117409fea0c861aa44b8e62.json
vendored
@ -1 +1 @@
|
|||||||
{"timestamp": 1733363478.77673, "result": 1.14}
|
{"timestamp": 1733367305.82001, "result": 1.18}
|
2
cache/29409a8a40dd2d547a7a44b8f6758f54.json
vendored
2
cache/29409a8a40dd2d547a7a44b8f6758f54.json
vendored
@ -1 +1 @@
|
|||||||
{"timestamp": 1733363478.8822353, "result": 4.16}
|
{"timestamp": 1733367305.1029575, "result": 4.18}
|
2
cache/4104ed0427efe63d4ca0dead970a4391.json
vendored
2
cache/4104ed0427efe63d4ca0dead970a4391.json
vendored
@ -1 +1 @@
|
|||||||
{"timestamp": 1733363358.2618918, "result": 240.01}
|
{"timestamp": 1733367303.484976, "result": 248.08}
|
2
cache/598f5dbf97fb0d45cbc6e1a5b0a3b575.json
vendored
2
cache/598f5dbf97fb0d45cbc6e1a5b0a3b575.json
vendored
@ -1 +1 @@
|
|||||||
{"timestamp": 1733363355.9661899, "result": 226.21}
|
{"timestamp": 1733367300.1864865, "result": 231.97}
|
2
cache/6eec370e2713cfc84c84e1080b8a191a.json
vendored
2
cache/6eec370e2713cfc84c84e1080b8a191a.json
vendored
@ -1 +1 @@
|
|||||||
{"timestamp": 1733363361.094433, "result": 120.0}
|
{"timestamp": 1733367298.2447531, "result": 120.0}
|
2
cache/94ac30c93587c50252ac382a8d02257f.json
vendored
2
cache/94ac30c93587c50252ac382a8d02257f.json
vendored
@ -1 +1 @@
|
|||||||
{"timestamp": 1733363355.9663823, "result": 1.47422459502}
|
{"timestamp": 1733367300.1870453, "result": 1.5117628721399998}
|
2
cache/a071d7bdda25c22e42ad7840f17c4b0e.json
vendored
2
cache/a071d7bdda25c22e42ad7840f17c4b0e.json
vendored
@ -1 +1 @@
|
|||||||
{"timestamp": 1733363357.525003, "result": 0.999445}
|
{"timestamp": 1733367301.6211267, "result": 1.001}
|
2
cache/a0ee60913ba556f39d128e7d7249e788.json
vendored
2
cache/a0ee60913ba556f39d128e7d7249e788.json
vendored
@ -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"}]}
|
2
cache/b81351778df9f812bbd75ee85a7a073e.json
vendored
2
cache/b81351778df9f812bbd75ee85a7a073e.json
vendored
@ -1 +1 @@
|
|||||||
{"timestamp": 1733363478.7767982, "result": 115.43342152993999}
|
{"timestamp": 1733367305.8206198, "result": 119.1529266475}
|
2
cache/ccf2a009e56f1b05d471a55d9c9ea8ea.json
vendored
2
cache/ccf2a009e56f1b05d471a55d9c9ea8ea.json
vendored
@ -1 +1 @@
|
|||||||
{"timestamp": 1733363419.3752136, "result": 82.815227}
|
{"timestamp": 1733367305.512328, "result": 82.815227}
|
2
main.py
2
main.py
@ -39,8 +39,6 @@ if __name__ == '__main__':
|
|||||||
'threads': threads,
|
'threads': threads,
|
||||||
}
|
}
|
||||||
|
|
||||||
threading.Thread(target=server.update_data).start()
|
|
||||||
|
|
||||||
gunicorn_app = GunicornApp(server.app, options)
|
gunicorn_app = GunicornApp(server.app, options)
|
||||||
print(f'Starting server with {workers} workers and {threads} threads', flush=True)
|
print(f'Starting server with {workers} workers and {threads} threads', flush=True)
|
||||||
gunicorn_app.run()
|
gunicorn_app.run()
|
||||||
|
21
server.py
21
server.py
@ -317,8 +317,8 @@ def api_token():
|
|||||||
# Get balance of vault
|
# Get balance of vault
|
||||||
token["SOL"] = {
|
token["SOL"] = {
|
||||||
"name": "Solana",
|
"name": "Solana",
|
||||||
"amount": getSolBalance() / supply,
|
"amount": round(getSolBalance() / supply,4),
|
||||||
"value": getSolValue() / supply
|
"value": round(getSolValue() / supply,2)
|
||||||
}
|
}
|
||||||
if token["SOL"]["value"] < 0.01:
|
if token["SOL"]["value"] < 0.01:
|
||||||
token["SOL"]["amount"] = 0
|
token["SOL"]["amount"] = 0
|
||||||
@ -377,23 +377,6 @@ def not_found(e):
|
|||||||
|
|
||||||
# endregion
|
# 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__":
|
if __name__ == "__main__":
|
||||||
threading.Thread(target=update_data).start()
|
|
||||||
app.run(debug=True, port=5000, host="0.0.0.0")
|
app.run(debug=True, port=5000, host="0.0.0.0")
|
||||||
|
Loading…
Reference in New Issue
Block a user