Common utilities for FastAPI applications
Project description
FastAPI Toolbox
这是一个Python库,提供FastAPI开发时的常用工具和功能。
安装
pip install fastapi-toolbox
功能特性
StaticFilesCache
StaticFilesCache 是 FastAPI StaticFiles 的增强版本,为静态文件提供可配置的缓存控制功能。
基本用法
from fastapi import FastAPI
from fastapi_toolbox import StaticFilesCache
import os
app = FastAPI()
# 示例1:使用默认缓存策略(禁用缓存)
front_folder = os.path.join(os.path.dirname(__file__), "frontend/dist")
app.mount("/", StaticFilesCache(directory=front_folder), name="static")
# 示例2:使用自定义缓存策略
app.mount("/static", StaticFilesCache(
directory="static_files",
cachecontrol="max-age=3600" # 缓存1小时
), name="static")
主要特性
- 自动缓存控制:自动为
.html和.txt文件添加 Cache-Control 响应头 - 可配置策略:通过
cachecontrol参数自定义缓存行为 - 完全兼容:继承自 FastAPI StaticFiles,保留所有原有功能
参数说明
| 参数 | 说明 | 默认值 |
|---|---|---|
directory |
静态文件目录路径 | 必填 |
cachecontrol |
Cache-Control 头的值 | "no-cache, no-store, must-revalidate" |
| 其他参数 | 与标准 StaticFiles 相同 | - |
常用缓存策略
# 禁用缓存(适合开发环境)
cachecontrol="no-cache, no-store, must-revalidate"
# 短期缓存(适合经常更新的资源)
cachecontrol="max-age=3600" # 1小时
# 长期缓存(适合不常变动的资源)
cachecontrol="public, max-age=86400" # 1天
# 私有缓存,必须重新验证
cachecontrol="private, must-revalidate"
实际应用场景
适用于需要为前端SPA应用提供静态文件服务的场景:
# 访问 http://127.0.0.1:8000/index.html 即可访问前端页面
front_folder = os.path.join(os.path.dirname(__file__), "frontend/dist")
app.mount("/", StaticFilesCache(directory=front_folder), name="static")
这样配置后,HTML文件将不会被浏览器缓存,确保用户总是获取最新版本的前端应用。
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
fastapi_toolbox-0.3.1.tar.gz
(3.6 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_toolbox-0.3.1.tar.gz.
File metadata
- Download URL: fastapi_toolbox-0.3.1.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f06a719c376a75f8b05fb456439e427a74f3b9281007958eafe90224b6800c76
|
|
| MD5 |
03952991c5cec4056f6bc95890bb370d
|
|
| BLAKE2b-256 |
2b6dee3b2e770dad8a0f44031f4beeb363b090087d63d60a8c0264f854bfa41c
|
File details
Details for the file fastapi_toolbox-0.3.1-py3-none-any.whl.
File metadata
- Download URL: fastapi_toolbox-0.3.1-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d74057264674c3477a36f5b02e0b757a1755c3a6899210d55ebd5987ead0a054
|
|
| MD5 |
3b1ec26b34c661ad2124986dc11d482d
|
|
| BLAKE2b-256 |
46db4c94eb4ec36810d68479bd6b6a81bf783f6511173d15d117940e743aa928
|