From e995df2e052c9c92a7852d1a5c011a0b32d67bb1 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Thu, 5 Dec 2024 14:32:26 +1100 Subject: [PATCH] feat: Update cache function --- cache.py | 14 +++++++++----- cache/10118a51009b13b2592c87579e15e61e.json | 2 +- cache/1ccff5c6f117409fea0c861aa44b8e62.json | 2 +- cache/29409a8a40dd2d547a7a44b8f6758f54.json | 2 +- cache/4104ed0427efe63d4ca0dead970a4391.json | 2 +- cache/598f5dbf97fb0d45cbc6e1a5b0a3b575.json | 2 +- cache/6eec370e2713cfc84c84e1080b8a191a.json | 2 +- cache/94ac30c93587c50252ac382a8d02257f.json | 2 +- cache/a071d7bdda25c22e42ad7840f17c4b0e.json | 2 +- cache/a0ee60913ba556f39d128e7d7249e788.json | 2 +- cache/b81351778df9f812bbd75ee85a7a073e.json | 2 +- cache/ccf2a009e56f1b05d471a55d9c9ea8ea.json | 2 +- main.py | 2 -- server.py | 21 ++------------------- 14 files changed, 22 insertions(+), 37 deletions(-) diff --git a/cache.py b/cache.py index ac93397..8321d39 100644 --- a/cache.py +++ b/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_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 diff --git a/cache/10118a51009b13b2592c87579e15e61e.json b/cache/10118a51009b13b2592c87579e15e61e.json index 8a30cdc..2d5b3ba 100644 --- a/cache/10118a51009b13b2592c87579e15e61e.json +++ b/cache/10118a51009b13b2592c87579e15e61e.json @@ -1 +1 @@ -{"timestamp": 1733363360.6520803, "result": "120"} \ No newline at end of file +{"timestamp": 1733367496.996954, "result": "120"} \ No newline at end of file diff --git a/cache/1ccff5c6f117409fea0c861aa44b8e62.json b/cache/1ccff5c6f117409fea0c861aa44b8e62.json index 0feb8e4..0f03aad 100644 --- a/cache/1ccff5c6f117409fea0c861aa44b8e62.json +++ b/cache/1ccff5c6f117409fea0c861aa44b8e62.json @@ -1 +1 @@ -{"timestamp": 1733363478.77673, "result": 1.14} \ No newline at end of file +{"timestamp": 1733367305.82001, "result": 1.18} \ No newline at end of file diff --git a/cache/29409a8a40dd2d547a7a44b8f6758f54.json b/cache/29409a8a40dd2d547a7a44b8f6758f54.json index 33ae4ad..2be2c0d 100644 --- a/cache/29409a8a40dd2d547a7a44b8f6758f54.json +++ b/cache/29409a8a40dd2d547a7a44b8f6758f54.json @@ -1 +1 @@ -{"timestamp": 1733363478.8822353, "result": 4.16} \ No newline at end of file +{"timestamp": 1733367305.1029575, "result": 4.18} \ No newline at end of file diff --git a/cache/4104ed0427efe63d4ca0dead970a4391.json b/cache/4104ed0427efe63d4ca0dead970a4391.json index c9943a5..00ef5cc 100644 --- a/cache/4104ed0427efe63d4ca0dead970a4391.json +++ b/cache/4104ed0427efe63d4ca0dead970a4391.json @@ -1 +1 @@ -{"timestamp": 1733363358.2618918, "result": 240.01} \ No newline at end of file +{"timestamp": 1733367303.484976, "result": 248.08} \ No newline at end of file diff --git a/cache/598f5dbf97fb0d45cbc6e1a5b0a3b575.json b/cache/598f5dbf97fb0d45cbc6e1a5b0a3b575.json index be7dff0..ffbe00e 100644 --- a/cache/598f5dbf97fb0d45cbc6e1a5b0a3b575.json +++ b/cache/598f5dbf97fb0d45cbc6e1a5b0a3b575.json @@ -1 +1 @@ -{"timestamp": 1733363355.9661899, "result": 226.21} \ No newline at end of file +{"timestamp": 1733367300.1864865, "result": 231.97} \ No newline at end of file diff --git a/cache/6eec370e2713cfc84c84e1080b8a191a.json b/cache/6eec370e2713cfc84c84e1080b8a191a.json index 70d9e98..130faec 100644 --- a/cache/6eec370e2713cfc84c84e1080b8a191a.json +++ b/cache/6eec370e2713cfc84c84e1080b8a191a.json @@ -1 +1 @@ -{"timestamp": 1733363361.094433, "result": 120.0} \ No newline at end of file +{"timestamp": 1733367298.2447531, "result": 120.0} \ No newline at end of file diff --git a/cache/94ac30c93587c50252ac382a8d02257f.json b/cache/94ac30c93587c50252ac382a8d02257f.json index bb4cefe..ae276d0 100644 --- a/cache/94ac30c93587c50252ac382a8d02257f.json +++ b/cache/94ac30c93587c50252ac382a8d02257f.json @@ -1 +1 @@ -{"timestamp": 1733363355.9663823, "result": 1.47422459502} \ No newline at end of file +{"timestamp": 1733367300.1870453, "result": 1.5117628721399998} \ No newline at end of file diff --git a/cache/a071d7bdda25c22e42ad7840f17c4b0e.json b/cache/a071d7bdda25c22e42ad7840f17c4b0e.json index 944c498..8b8a8fc 100644 --- a/cache/a071d7bdda25c22e42ad7840f17c4b0e.json +++ b/cache/a071d7bdda25c22e42ad7840f17c4b0e.json @@ -1 +1 @@ -{"timestamp": 1733363357.525003, "result": 0.999445} \ No newline at end of file +{"timestamp": 1733367301.6211267, "result": 1.001} \ No newline at end of file diff --git a/cache/a0ee60913ba556f39d128e7d7249e788.json b/cache/a0ee60913ba556f39d128e7d7249e788.json index 69713d0..9cf1b9f 100644 --- a/cache/a0ee60913ba556f39d128e7d7249e788.json +++ b/cache/a0ee60913ba556f39d128e7d7249e788.json @@ -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"}]} \ No newline at end of file +{"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"}]} \ No newline at end of file diff --git a/cache/b81351778df9f812bbd75ee85a7a073e.json b/cache/b81351778df9f812bbd75ee85a7a073e.json index cde6576..1bba464 100644 --- a/cache/b81351778df9f812bbd75ee85a7a073e.json +++ b/cache/b81351778df9f812bbd75ee85a7a073e.json @@ -1 +1 @@ -{"timestamp": 1733363478.7767982, "result": 115.43342152993999} \ No newline at end of file +{"timestamp": 1733367305.8206198, "result": 119.1529266475} \ No newline at end of file diff --git a/cache/ccf2a009e56f1b05d471a55d9c9ea8ea.json b/cache/ccf2a009e56f1b05d471a55d9c9ea8ea.json index ad0363e..7097bd8 100644 --- a/cache/ccf2a009e56f1b05d471a55d9c9ea8ea.json +++ b/cache/ccf2a009e56f1b05d471a55d9c9ea8ea.json @@ -1 +1 @@ -{"timestamp": 1733363419.3752136, "result": 82.815227} \ No newline at end of file +{"timestamp": 1733367305.512328, "result": 82.815227} \ No newline at end of file diff --git a/main.py b/main.py index 9bcefc3..9197959 100644 --- a/main.py +++ b/main.py @@ -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() diff --git a/server.py b/server.py index efcd659..3f150fe 100644 --- a/server.py +++ b/server.py @@ -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")