You've already forked favicon-api-v3
25.09.08
This commit is contained in:
@@ -7,6 +7,7 @@ from typing import Tuple, Optional
|
||||
import aiohttp
|
||||
|
||||
import setting
|
||||
from favicon_app.asyncs import redis_pool
|
||||
from favicon_app.models import favicon
|
||||
from favicon_app.utils import header
|
||||
from favicon_app.utils.filetype import helpers, filetype
|
||||
@@ -135,6 +136,7 @@ class FaviconAsync(favicon.Favicon):
|
||||
content = await resp.read()
|
||||
return content, ct_type
|
||||
else:
|
||||
await redis_pool.set_cache(domain, setting.time_of_7_days, setting.time_of_7_days)
|
||||
favicon.failed_url_cache(domain, setting.time_of_7_days)
|
||||
logger.error('异步请求失败: %d, URL: %s', resp.status, url)
|
||||
break
|
||||
@@ -146,6 +148,7 @@ class FaviconAsync(favicon.Favicon):
|
||||
logger.warning('异步请求超时,正在重试(%d/%d): %s', retry_count, retries, url)
|
||||
continue
|
||||
except Exception as e:
|
||||
await redis_pool.set_cache(domain, setting.time_of_7_days, setting.time_of_7_days)
|
||||
favicon.failed_url_cache(domain, setting.time_of_7_days)
|
||||
logger.error('异步请求异常: %s, URL: %s', str(e), url)
|
||||
break
|
||||
|
||||
@@ -60,7 +60,7 @@ class FaviconServiceAsync(favicon_service.FaviconService):
|
||||
|
||||
strategy_url, strategy_name = strategy()
|
||||
if strategy_url is not None:
|
||||
logger.info(f"-> 异步尝试从 {strategy_name} 获取图标")
|
||||
logger.debug(f"-> 异步尝试从 {strategy_name} 获取图标")
|
||||
icon_content, icon_type = await entity.async_get_icon_file(strategy_url, strategy_url == '')
|
||||
|
||||
# 图标获取失败,或图标不是支持的图片格式,写入默认图标
|
||||
|
||||
38
favicon_app/asyncs/redis_pool.py
Normal file
38
favicon_app/asyncs/redis_pool.py
Normal file
@@ -0,0 +1,38 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import AsyncGenerator
|
||||
|
||||
from fastapi import Depends
|
||||
from redis.asyncio import ConnectionPool, Redis
|
||||
|
||||
import setting
|
||||
|
||||
REDIS_URL = setting.REDIS_URL
|
||||
|
||||
pool = ConnectionPool.from_url(
|
||||
REDIS_URL,
|
||||
max_connections=200,
|
||||
decode_responses=True,
|
||||
)
|
||||
|
||||
|
||||
async def get_redis() -> AsyncGenerator[Redis, None]:
|
||||
async with Redis(connection_pool=pool) as conn:
|
||||
yield conn
|
||||
|
||||
|
||||
async def set_cache(
|
||||
key: str,
|
||||
value: str,
|
||||
ttl: int | None = None,
|
||||
r: Redis = Depends(get_redis),
|
||||
):
|
||||
await r.set(key, value, ex=ttl)
|
||||
|
||||
|
||||
async def get_cache(key: str, r: Redis = Depends(get_redis)):
|
||||
return await r.get(key)
|
||||
|
||||
|
||||
async def del_cache(key: str, r: Redis = Depends(get_redis)):
|
||||
await r.delete(key)
|
||||
Reference in New Issue
Block a user