LOADING...
基于api进行操作的缓存系统
基于api进行操作的缓存系统,支持 Vercel 和 Cloudflare Workers 部署
可以使用Vercel一键部署。
转到你的Vercel,在此页面中创建一个KV Database. vercel/stores
尽量选择离你更近的服务地区,名称随意。如果切换了服务地区,你可稍后在下方的部署按钮部署结束后,在项目设置中更改你的项目serverless地区为服务器所在地区以提高性能。
转到你的数据库页面,切换至.env.local,如图。记录如下两项的值:
KV_REST_API_TOKENKV_REST_API_URL之后,单击下方部署按钮,填入上方值即可一键部署。
Deploy with Vercel
浏览器会打开授权页面,登录你的 Cloudflare 账户并授权。
你会看到类似输出:
复制 id 的值。
编辑项目根目录的 wrangler.toml 文件,将第 10 行的 your_kv_namespace_id_here 替换为上一步获得的 ID:
部署成功后会显示你的 Worker URL:
现在你可以通过这个 URL 访问你的缓存 API 了!
本地开发服务器会在 http://localhost:8787 启动。
更多部署选项和配置,请参考:
如果你已经在 Vercel 上有数据,想要迁移到 Cloudflare,可以使用内置的迁移脚本。
复制环境变量模板:
编辑 .env 文件,填写以下信息:
迁移脚本会:
迁移完成后会生成 文件,包含:
完整的迁移指南请参考:MIGRATION.md
此API提供了数据的读写和删除功能。请求方法包括POST和GET。
请求数据有两种方法,可以以POST的方式请求,返回json格式的数据,或者以GET的方式请求,返回文本格式的数据
请求方法:POST
请求路径:或
请求参数:
请求方法:GET
请求路径: 或
查询参数:
例子:
写入数据:
读取数据:
删除数据:
migration-report.json/api?mode=set/set/api?mode=get/get/api/https://cache.ravelloh.top/?uuid=xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx&password=123456&shouldDelete=true/api?mode=del/del/api✨ Success!
Add the following to your configuration file:
{ binding = "KV_CACHE", id = "abc123def456..." }Published kv-cache (1.23 sec)
https://kv-cache.your-subdomain.workers.devnpm installnpx wrangler loginnpx wrangler kv:namespace create "KV_CACHE"npm run deploy:cloudflarenpm run dev:cloudflarecp .env.example .env[[kv_namespaces]]
binding = "KV_CACHE"
id = "your_actual_kv_namespace_id" # 替换这里# 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 编辑权限)npm run migratefetch('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));