master
jinql 2025-09-11 14:41:01 +08:00
parent a845b1053e
commit 96a975b9ad
3 changed files with 20 additions and 7 deletions

View File

@ -24,12 +24,10 @@ forwarded_allow_ips = "*"
with open(Path(__file__).with_name("logging.yaml"), "r", encoding="utf-8") as f:
logconfig_dict = yaml.safe_load(f)
# 日志级别debug、info、warning、error、critical);以 YAML 配置优先
# 日志级别debug、info、warning、error以 YAML 配置优先
loglevel = "info"
# 访问日志文件("-" 表示输出到 stdout以 YAML 配置优先
accesslog = "logs/access.log"
# 错误日志文件;以 YAML 配置优先
errorlog = "-"

View File

@ -24,12 +24,10 @@ forwarded_allow_ips = "*"
with open(Path(__file__).with_name("logging.yaml"), "r", encoding="utf-8") as f:
logconfig_dict = yaml.safe_load(f)
# 日志级别debug、info、warning、error、critical);以 YAML 配置优先
# 日志级别debug、info、warning、error);以 YAML 配置优先
loglevel = "info"
# 访问日志文件("-" 表示输出到 stdout以 YAML 配置优先
accesslog = "logs/access.log"
# 错误日志文件;以 YAML 配置优先
errorlog = "-"

View File

@ -29,6 +29,10 @@ warnings.filterwarnings("ignore", category=XMLParsedAsHTMLWarning)
# 获取当前所在目录的绝对路径
_current_dir = os.path.dirname(os.path.abspath(__file__))
_url_count = 0
_icon_count = 0
_cache_count = 0
# 预编译正则表达式,提高性能
pattern_icon = re.compile(r'(icon|shortcut icon|alternate icon|apple-touch-icon)+', re.I)
pattern_link = re.compile(r'(<link[^>]+rel=.(icon|shortcut icon|alternate icon|apple-touch-icon)[^>]+>)', re.I)
@ -235,6 +239,7 @@ async def get_favicon_handler(request: Request,
url: Optional[str] = None,
refresh: Optional[str] = None) -> dict[str, str] | Response:
"""异步处理获取图标的请求"""
global _url_count, _icon_count, _cache_count
# 验证URL参数
if not url:
@ -243,7 +248,8 @@ async def get_favicon_handler(request: Request,
try:
entity = Favicon(url)
logger.info(f"-> failed url size: {len(favicon.failed_urls)}")
# 统计URL数量
_url_count += 1
# 验证域名
if not entity.domain:
@ -257,6 +263,11 @@ async def get_favicon_handler(request: Request,
else:
del favicon.failed_urls[entity.domain]
logger.info(
f"-> count (failed/cached/icon/url): "
f"{len(favicon.failed_urls)}/{_cache_count}/{_icon_count}/{_url_count}"
)
# 检查缓存
_cached, cached_icon = _get_cache_icon(entity.domain_md5, refresh=refresh in ['true', '1'])
@ -276,6 +287,8 @@ async def get_favicon_handler(request: Request,
logger.info(f"缓存已过期,加入后台队列刷新(异步): {entity.domain}")
bg_tasks.add_task(get_icon_async, entity, _cached)
# 缓存计数
_cache_count += 1
return Response(content=icon_content,
media_type=content_type if content_type else "image/x-icon",
headers=_get_header(content_type, cache_time))
@ -303,6 +316,7 @@ async def get_favicon_handler(request: Request,
async def get_icon_async(entity: Favicon, _cached: bytes = None) -> Optional[bytes]:
"""异步获取图标"""
global _icon_count
icon_content = None
try:
@ -355,6 +369,9 @@ async def get_icon_async(entity: Favicon, _cached: bytes = None) -> Optional[byt
# 写入缓存文件注意文件IO操作仍然是同步的
FileUtil.write_file(cache_path, icon_content, mode='wb')
FileUtil.write_file(md5_path, entity.domain, mode='w')
# 任务计数
_icon_count += 1
except Exception as e:
logger.error(f"异步写入缓存文件失败: {e}")