基于api进行操作的缓存系统,支持 Vercel 和 Cloudflare Workers 部署
可以使用Vercel一键部署。
转到你的Vercel,在此页面中创建一个KV Database. vercel/stores
尽量选择离你更近的服务地区,名称随意。如果切换了服务地区,你可稍后在下方的部署按钮部署结束后,在项目设置中更改你的项目serverless地区为服务器所在地区以提高性能。
转到你的数据库页面,切换至.env.local,如图。记录如下两项的值:
npm installnpx wrangler login浏览器会打开授权页面,登录你的 Cloudflare 账户并授权。
npx wrangler kv:namespace create "KV_CACHE"你会看到类似输出:
✨ Success!
Add the following to your configuration file:
{ binding = "KV_CACHE", id = "abc123def456..." }
复制 id 的值。
编辑项目根目录的 wrangler.toml 文件,将第 10 行的 your_kv_namespace_id_here 替换为上一步获得的 ID:
[[kv_namespaces]]
binding = "KV_CACHE"
id = "your_actual_kv_namespace_id" # 替换这里npm run deploy:cloudflare部署成功后会显示你的 Worker URL:
Published kv-cache (1.23 sec)
https://kv-cache.your-subdomain.workers.dev
现在你可以通过这个 URL 访问你的缓存 API 了!
npm run dev:cloudflare本地开发服务器会在 http://localhost:8787 启动。
更多部署选项和配置,请参考:
如果你已经在 Vercel 上有数据,想要迁移到 Cloudflare,可以使用内置的迁移脚本。
复制环境变量模板:
cp .env.example .env编辑 .env 文件,填写以下信息:
# Vercel KV 配置(从 Vercel Dashboard > Storage > KV > Settings 获取)
KV_REST_API_URL=https://your-kv-name.kv.vercel-storage.com
KV_REST_API_TOKEN=your_vercel_kv_token
# Cloudflare 配置
CF_ACCOUNT_ID=your_account_id # Cloudflare Dashboard 右侧可见
CF_NAMESPACE_ID=your_namespace_id # 运行 wrangler kv:namespace create 后获取
CF_API_TOKEN=your_api_token # 创建 API Token(需要 Workers KV Storage 编辑权限)- 访问 Cloudflare API Tokens 页面
- 点击 Create Token
- 选择 Custom Token
- 配置权限:Account → Workers KV Storage → Edit
- 创建并复制 Token
npm run migrate迁移脚本会:
- 扫描 Vercel KV 中的所有数据
- 自动保留剩余过期时间
- 批量迁移到 Cloudflare KV
- 生成详细的迁移报告
迁移完成后会生成 migration-report.json 文件,包含:
- 总数据量
- 成功迁移数量
- 跳过/失败数量
- 错误详情(如有)
完整的迁移指南请参考:MIGRATION.md
此API提供了数据的读写和删除功能。请求方法包括POST和GET。
- 请求方法:POST
- 请求路径:
/api?mode=set或/set - 请求参数:
- data:要存储的数据(必选)
- password:访问数据时的密码(可选)
- safeIP:允许访问数据的IP地址范围(可选,示例:1.2-3.*.4)
- expiredTime:数据过期时间(可选,默认为7天)
- uuid:数据的唯一标识(可选,如果不提供则自动生成,输入已存在的uuid可覆盖原数据)
请求数据有两种方法,可以以POST的方式请求,返回json格式的数据,或者以GET的方式请求,返回文本格式的数据
-
请求方法:POST
-
请求路径:
/api?mode=get或/get -
请求参数:
- uuid:要读取的数据的唯一标识(必选)
- password:访问数据时的密码(可选)
- shouldDelete:是否在读取数据后删除数据(可选,默认为false)
-
请求方法:GET
-
请求路径:
/api或/ -
查询参数:
- uuid:要读取的数据的唯一标识(必选)
- password:访问数据时的密码(可选)
- shouldDelete:是否在读取数据后删除数据(可选,默认为false)
-
例子:
https://cache.ravelloh.top/?uuid=xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx&password=123456&shouldDelete=true
- 请求方法:POST
- 请求路径:
/api?mode=del或/del - 请求参数:
- uuid:要删除的数据的唯一标识(必选)
- 请求方法:GET
- 请求路径:
/api - 响应参数:
- code:状态码(200表示正常)
- message:状态信息
- version:API版本号
- active:当前活动的数据数量
写入数据:
fetch('https://cache.ravelloh.top/api?mode=set', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
data: 'Hello, World!',
password: '123456',
safeIP: '*.*.*.*',
expiredTime: 24 * 60 * 60 * 1000,
uuid: 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
})
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));读取数据:
fetch('https://cache.ravelloh.top/api?mode=get', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
uuid: 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx',
password: '123456',
shouldDelete: false
})
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));删除数据:
fetch('https://cache.ravelloh.top/api?mode=del', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
uuid: 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
})
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));- 数据大小超过1MB的限制会返回错误。
- 密码长度超过128的限制会返回错误。
- IP规则不正确会返回错误,正确示例为:1.2-3.*.4。
- UUID格式错误会自动生成。
- 读取数据时,如果访问IP与存储数据的IP不匹配,会返回无权限错误。
- 读取数据时,如果提供的密码与存储数据的密码不匹配,会返回无效密码错误。
- 删除数据时,如果提供的UUID不存在,会返回删除失败错误。
