copilot_share/cache/cache_token.py
2024-02-20 11:38:06 +08:00

30 lines
678 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@File : cache_token.py
@Time : 2023/09/27 20:04:45
@Author : lvguanjun
@Desc : cache_token.py
"""
import random
import time
tokens = {}
err_tokens = {}
def set_token_to_cache(github_token, copilot_token):
tokens[github_token] = copilot_token
def get_token_from_cache(github_token):
# 当前过期时间30分钟15-25分钟内随机值刷新
# [(30 - 25) * 60, (30 - 15) * 60]
extra_time = random.randint(300, 900)
if copilot_token := tokens.get(github_token):
if copilot_token["expires_at"] > int(time.time()) + extra_time:
return copilot_token
return None