Files
storage/mockstorage
2024-06-03 15:22:45 -03:00
..
2024-06-03 15:22:45 -03:00
2024-06-03 15:22:45 -03:00
2024-06-03 15:22:45 -03:00
2024-06-03 15:22:45 -03:00
2024-06-03 15:22:45 -03:00

id, title
id title
memory Memory

Release Discord Test Security Linter

A mock storage implementation for Fiber. This storage is not persistent and is only used for testing purposes.

Note: Requires Go 1.21 and above

Table of Contents

Signatures

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() map[string]entry
func (s *Storage) Keys() ([][]byte, error)

func SetCustomFuncs(custom *CustomFuncs)

Installation

Mockstorage is tested on the 2 last Go versions with support for modules. So make sure to initialize one first if you didn't do that yet:

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

And then install the mockstorage implementation:

go get github.com/gofiber/storage/mockstorage

Examples

Import the storage package.

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

You can use the following possibilities to create a storage:

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

// Initialize custom config
store := mockstorage.New(mockstorage.Config{
	CustomFuncs: &mockstorage.CustomFuncs{
		&CustomFuncs{
			GetFunc: func(key string) ([]byte, error) {
				if key == "customKey" {
					return []byte("customValue"), nil
				}
				return nil, errors.New("custom key not found")
			},
			SetFunc: func(key string, val []byte, exp time.Duration) error {
				if key == "readonly" {
					return errors.New("cannot set readonly key")
				}
				return nil
			},
			DeleteFunc: func(key string) error {
				if key == "protectedKey" {
					return errors.New("cannot delete protected key")
				}
				return nil
			},
			// ...
		},
	},
})

// Set custom functions after initialization
store.SetCustomFuncs(&mockstorage.CustomFuncs{
	GetFunc: func(key string) ([]byte, error) {
		if key == "customKey" {
			return []byte("customValue"), nil
		}
		return nil, errors.New("custom key not found")
	},
	SetFunc: func(key string, val []byte, exp time.Duration) error {
		if key == "readonly" {
			return errors.New("cannot set readonly key")
		}
		return nil
	},
	DeleteFunc: func(key string) error {
		if key == "protectedKey" {
			return errors.New("cannot delete protected key")
		}
		return nil
	},
	// ...
})

Config

type Config struct {
	CustomFuncs *CustomFuncs
}

Default Config

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