Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
cache stats (#179)
Browse files Browse the repository at this point in the history
- fixes #175
  • Loading branch information
casperdcl authored Nov 3, 2023
1 parent 347f3b1 commit 385ecf0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/core/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ def system_prune():
client.networks.prune()


@utils.cache_ttl(seconds=10)
def get_free_total_memory():
if utils.is_gpu_available():
total_memory = 0
Expand All @@ -322,6 +323,7 @@ def get_free_total_memory():
return free_memory, values["memory_limit"]


@utils.cache_ttl(seconds=10)
def get_free_storage():
values = get_system_stats_all()
free_storage = values["storage_limit"] - values["storage_usage"]
Expand Down
21 changes: 21 additions & 0 deletions app/core/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import errno
import functools
import logging
import os
import pty
import signal
import subprocess
import time
from http import HTTPStatus

import docker
Expand Down Expand Up @@ -293,3 +295,22 @@ def get_deployment_ip():
except Exception as e:
logger.error(f"An error occurred: {e}")
return None


def cache_ttl(seconds: int = 10, **lru_cache_kwargs):
"""`functools.lru_cache` but with time-based expiry"""

def inner(func):
"""based on https://stackoverflow.com/q/31771286"""

@functools.lru_cache(**lru_cache_kwargs)
def with_timeout(__ttl: float, *args, **kwargs):
return func(*args, **kwargs)

@functools.wraps(func)
def wrapper(*args, **kwargs):
return with_timeout(time.time() // seconds, *args, **kwargs)

return wrapper

return inner

0 comments on commit 385ecf0

Please sign in to comment.