forked from EzioTheDeadPoet/yaml-tags-discord-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
enhancementNew feature or requestNew feature or request
Description
for better performance and reduced server requests to prevent (reduce) chances for the bot timing out.
Notes to self on how to do it:
import requests
from cachetools import cached, TTLCache
# ttl is in seconds (300 seconds = 5 minutes)
# maxsize is the maximum number of items to keep in cache
cache = TTLCache(maxsize=100, ttl=300)
@cached(cache)
def get_api_data(url):
print(f"Fetching data from API: {url}")
response = requests.get(url)
return response.json()
# First call: Fetches from API
data1 = get_api_data("https://api.example.com/data")
# Second call (within 5 mins): Returns from RAM cache
data2 = get_api_data("https://api.example.com/data")Adding TTL information of the cached response to the discord message or as a separate command:
import time
from cachetools import TTLCache
# We set the TTL in the cache to act as a hard limit
TTL_LIMIT = 60
cache = TTLCache(maxsize=10, ttl=TTL_LIMIT)
def set_with_timestamp(key, value):
# Store the time it was added along with the value
cache[key] = (time.time(), value)
def get_info(key):
if key not in cache:
return None, 0
start_time, value = cache[key]
remaining = TTL_LIMIT - (time.time() - start_time)
return value, max(0, remaining)
# Usage
set_with_timestamp('user_1', {'name': 'Alice'})
data, timeLeft = get_info('user_1')
print(f"Data: {data}, Time remaining: {timeLeft:.2f}s")Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request