️ support cache tenant token

This commit is contained in:
StarHeartHunt 2021-07-09 09:22:56 +08:00
parent 730186cfd4
commit 70e424b58f
3 changed files with 31 additions and 2 deletions

View File

@ -1,6 +1,8 @@
import json
import httpx
from aiocache import cached, Cache
from aiocache.serializers import PickleSerializer
from typing import Any, Dict, Tuple, Union, Optional, TYPE_CHECKING
from nonebot.log import logger
@ -195,7 +197,10 @@ class Bot(BaseBot):
def _construct_url(self, path: str) -> str:
return self.api_root + path
#TODO:实现token缓存与ttl
@cached(ttl=60 * 60,
cache=Cache.MEMORY,
key="_feishu_tenant_access_token",
serializer=PickleSerializer())
async def _fetch_tenant_access_token(self) -> str:
try:
async with httpx.AsyncClient() as client:

View File

@ -1,3 +1,22 @@
[[package]]
name = "aiocache"
version = "0.11.1"
description = "multi backend asyncio cache"
category = "main"
optional = false
python-versions = "*"
[package.extras]
dev = ["asynctest (>=0.11.0)", "codecov", "coverage", "flake8", "ipdb", "marshmallow", "pystache", "pytest", "pytest-asyncio", "pytest-mock", "sphinx", "sphinx-autobuild", "sphinx-rtd-theme", "black"]
memcached = ["aiomcache (>=0.5.2)"]
msgpack = ["msgpack (>=0.5.5)"]
redis = ["aioredis (>=0.3.3)", "aioredis (>=1.0.0)"]
[package.source]
type = "legacy"
url = "https://mirrors.aliyun.com/pypi/simple"
reference = "aliyun"
[[package]]
name = "certifi"
version = "2021.5.30"
@ -432,9 +451,13 @@ reference = "aliyun"
[metadata]
lock-version = "1.1"
python-versions = "^3.7.3"
content-hash = "fbc5c896e785a9b9c948ec9f20d5d795684bc0f839bd31b10812c9dc52adb75b"
content-hash = "8cb6803a739721f108b5d43f0633275f26a97c6486eb67cdb9346f369a5182ee"
[metadata.files]
aiocache = [
{file = "aiocache-0.11.1-py2.py3-none-any.whl", hash = "sha256:e55c7caaa5753794fd301c3a2e592737fa1d036db9f8d04ae154facdfb48a157"},
{file = "aiocache-0.11.1.tar.gz", hash = "sha256:f2ebe0b05cec45782e7b5ea0bb74640f157dd4bb1028b4565364dda9fe33be7f"},
]
certifi = [
{file = "certifi-2021.5.30-py2.py3-none-any.whl", hash = "sha256:50b1e4f8446b06f41be7dd6338db18e0990601dce795c2b1686458aa7e8fa7d8"},
{file = "certifi-2021.5.30.tar.gz", hash = "sha256:2bbf76fd432960138b3ef6dda3dde0544f27cbf8546c458e60baf371917ba9ee"},

View File

@ -27,6 +27,7 @@ python = "^3.7.3"
httpx = "^0.17.0"
nonebot2 = "^2.0.0-alpha.13"
pycryptodome = "^3.10.1"
aiocache = "^0.11.1"
[tool.poetry.dev-dependencies]
nonebot2 = { path = "../../", develop = true }