跳到主要内容
版本: valkey_v0.x.x

Cloudflare KV

Release Discord Test Security Linter

使用 cloudflare/cloudflare-go 的 Cloudflare KV 存储驱动。

注意: 需要 Go 1.21 及更高版本

目录

签名

func New(config ...Config) Storage
func (s *Storage) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error
func (s *Storage) Delete(key string) error
func (s *Storage) Reset() error
func (s *Storage) Close() error
func (s *Storage) Conn() *cloudflare.API

安装

go mod init github.com/<user>/<repo>

然后安装 Cloudflare KV 实现

go get github.com/gofiber/storage/cloudflarekv

示例

导入 storage 包。

import "github.com/gofiber/storage/cloudflarekv"

你可以使用以下方法创建存储。Key 必须是至少具有 Account.Workers KV Storage 权限生成的 API Token。查看 创建 API Token 文档以生成一个。

// Initialize default config
store := cloudflarekv.New()

store := cloudflarekv.New(cloudflarekv.Config{
Key: "",
Email: "",
AccountID: "fiber",
NamespaceID: "fiber",
Reset: false,
})

配置

type Config struct {

// Cloudflare Auth Token
//
// Optional. Default is ""
Key string

// Cloudflare Email
//
// Optional. Default is ""
Email string

// Account id
//
// Optional. Default is "fiber"
AccountID string

// Namespace id
//
// Optional. Default is "fiber"
NamespaceID string

// Reset clears any existing keys in existing Table
//
// Optional. Default is false
Reset bool
}

默认配置

var ConfigDefault = Config{
Key: "",
Email: "",
AccountID: "fiber",
NamespaceID: "fiber",
Reset: false,
}