Skip to content

Commit

Permalink
Fix cache issue for dynamically added games
Browse files Browse the repository at this point in the history
Replaced lru_cache with a custom caching system to handle games added post server start. Ensured new entries are correctly cached and old entries are evicted when the cache size limit is reached.
  • Loading branch information
Lenochxd committed Sep 7, 2024
1 parent ab2d41a commit c90c58f
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,47 @@ async def dispatch(self, request: Request, call_next):
app.add_middleware(RateLimitMiddleware)


@lru_cache(maxsize=128)
# Custom caching system
cache = {}
cache_max_size = 128

def find_id_type(tid: str):
tid = tid.upper()

# Check if result is in cache
if tid in cache:
return cache[tid]

base_path = os.path.join(config['database-path'], 'base', f'{tid}.json')
dlc_path = os.path.join(config['database-path'], 'dlc', f'{tid}.json')
update_path = os.path.join(config['database-path'], 'update', f'{tid}.json')

if os.path.exists(base_path):
return 'nx', 'base', base_path
result = ('nx', 'base', base_path)
elif os.path.exists(dlc_path):
return 'nx', 'dlc', dlc_path
result = ('nx', 'dlc', dlc_path)
elif os.path.exists(update_path):
return 'nx', 'update', update_path
result = ('nx', 'update', update_path)
else:
retro_path = os.path.join(config['database-path'], 'retro')
if os.path.exists(retro_path):
for console in os.listdir(retro_path):
console_path = os.path.join(retro_path, console, f'{tid}.json')
if os.path.exists(console_path):
return console, 'retro', console_path

return None, None, None
result = (console, 'retro', console_path)
break
else:
result = (None, None, None)
else:
result = (None, None, None)

# Add result to cache
if len(cache) >= cache_max_size:
cache.pop(next(iter(cache))) # Remove oldest item
if result != (None, None, None):
cache[tid] = result

return result

@lru_cache(maxsize=128)
def get_game_screenshot(tid: str, screen_id: 1):
Expand Down

0 comments on commit c90c58f

Please sign in to comment.