mirror of
https://github.com/Faridik/Carolyn.git
synced 2026-07-08 05:11:52 +00:00
lru cache,
This commit is contained in:
parent
326fd07123
commit
c940a862e3
@ -2,6 +2,8 @@ from collections import defaultdict
|
||||
import os.path
|
||||
import pathlib
|
||||
from sys import path
|
||||
from functools import lru_cache, wraps
|
||||
from datetime import datetime, timedelta
|
||||
from googleapiclient.discovery import _MEDIA_SIZE_BIT_SHIFTS, build
|
||||
from google_auth_oauthlib.flow import InstalledAppFlow
|
||||
from google.auth.transport.requests import Request
|
||||
@ -26,6 +28,25 @@ TOKEN_FILE = pathlib.Path() / ".secrets" / "token.json"
|
||||
CLIENT_SECRET_FILE = pathlib.Path() / ".secrets" / "client_secret.json"
|
||||
|
||||
|
||||
def timed_lru_cache(seconds: int, maxsize: int = 128):
|
||||
def wrapper_cache(func):
|
||||
func = lru_cache(maxsize=maxsize)(func)
|
||||
func.lifetime = timedelta(seconds=seconds)
|
||||
func.expiration = datetime.utcnow() + func.lifetime
|
||||
|
||||
@wraps(func)
|
||||
def wrapped_func(*args, **kwargs):
|
||||
if datetime.utcnow() >= func.expiration:
|
||||
func.cache_clear()
|
||||
func.expiration = datetime.utcnow() + func.lifetime
|
||||
|
||||
return func(*args, **kwargs)
|
||||
|
||||
return wrapped_func
|
||||
|
||||
return wrapper_cache
|
||||
|
||||
|
||||
class StudentAlreadyAuthed(Exception):
|
||||
def __init__(self, message):
|
||||
self.message = message
|
||||
@ -76,6 +97,7 @@ class Manager:
|
||||
service = build("sheets", "v4", credentials=creds)
|
||||
self.sheet = service.spreadsheets()
|
||||
|
||||
@timed_lru_cache(seconds=150)
|
||||
def get_values(
|
||||
self, range_name: str = "StudentList"
|
||||
) -> list: # TODO: change subject
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user