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

MockStorage

Release Discord Test Security Linter

Fiber 的一个模拟存储实现。此存储不是持久性的,仅用于测试目的。

注意:需要 Go 1.21 及以上版本

目录

签名

结构体

type Storage struct {
// contains filtered or unexported fields
}

type Entry struct {
Value []byte
Exp time.Time
}

type Config struct {
CustomFuncs *CustomFuncs
}

type CustomFuncs struct {
GetFunc func(key string) ([]byte, error)
SetFunc func(key string, val []byte, exp time.Duration) error
DeleteFunc func(key string) error
ResetFunc func() error
CloseFunc func() error
ConnFunc func() map[string]Entry
KeysFunc func() ([][]byte, error)
}

函数

// New creates a new Storage instance. You can optionally pass a Config.
func New(config ...Config) *Storage

// Get retrieves the value associated with the given key.
func (s *Storage) Get(key string) ([]byte, error)

// Set sets the value for the given key, with an optional expiration duration.
func (s *Storage) Set(key string, val []byte, exp time.Duration) error

// Delete removes the value associated with the given key.
func (s *Storage) Delete(key string) error

// Reset clears all values from the storage.
func (s *Storage) Reset() error

// Close performs any necessary cleanup when the storage is no longer needed.
func (s *Storage) Close() error

// Conn returns a copy of the current state of the storage.
func (s *Storage) Conn() map[string]Entry

// Keys returns a list of all keys in the storage.
func (s *Storage) Keys() ([][]byte, error)

// SetCustomFuncs allows you to set custom functions for the storage operations.
func (s *Storage) SetCustomFuncs(custom *CustomFuncs)

安装

MockStorage 在支持模块的最新两个 Go 版本上进行了测试。因此,如果您尚未初始化,请确保先进行初始化。

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

然后安装 mockstorage 实现

go get github.com/gofiber/storage/mockstorage

示例

导入存储包。

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

您可以使用以下方法创建存储

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

// Set a value in the storage.
err := store.Set("key1", []byte("value1"), 0)
if err != nil {
// handle error
}

// Get a value from the storage.
val, err := store.Get("key1")
if err != nil {
// handle error
}
fmt.Println(string(val)) // prints "value1"

// Delete a value from the storage.
err = store.Delete("key1")
if err != nil {
// handle error
}

// Mocking storage operations in tests:
func TestMyFunction(t *testing.T) {
// Create a new instance of MockStorage
store := mockstorage.New()

// Mock the Set function
store.SetCustomFuncs(&mockstorage.CustomFuncs{
Set: func(key string, val []byte, exp time.Duration) error {
if key == "expectedKey" && string(val) == "expectedValue" {
return nil
}
return errors.New("unexpected key or value")
},
})

// Call the function you want to test, which should call store.Set
err := MyFunction(store)

// Check that the function behaved as expected
if err != nil {
t.Errorf("MyFunction returned an error: %v", err)
}
}

注意:mockstorage 包中,数据过期不会在后台自动处理。数据仅在您尝试在过期时间之后 Get() 它时才被标记为过期并删除。如果您使用自定义的 Get() 函数或直接使用 Conn() 函数访问数据,过期的 数据将不会被移除。在编写测试时请记住这一点。

配置

type Config struct {
CustomFuncs *CustomFuncs
}

默认配置

var ConfigDefault = Config{
CustomFuncs: &CustomFuncs{
GetFunc: nil,
SetFunc: nil,
DeleteFunc: nil,
ResetFunc: nil,
CloseFunc: nil,
ConnFunc: nil,
KeysFunc: nil,
},
}