storage

package module
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 20, 2023 License: MIT Imports: 6 Imported by: 5

README

Fiber Storage Drivers

Storage drivers that implement a common Storage interface, designed to be used with Fiber. These ARE NOT directly compatible with the standard storage drivers for Fiber, but only require minimal code tweaks for them to work with code that uses those.

These differ from the standard Fiber versions in that they allow any data type to be entered and retrieved, and allow them to be recalled either as an interface{} or as their original type.

// Storage interface for communicating with different database/key-value providers
type Storage interface {
	// Get the value for the given key.
	// A Result struct is returned allowing various extraction options.
	Get(key string) *Result

	// Set the value for the given key along with an optional expiration value, 0 means no expiration.
	// An empty key will flag an error, but empty values are allowed.
	Set(key string, val any, expiry ...time.Duration) error

	// Deletes the values for the given keys.
	// It returns no error if the storage does not contain the keys.
	Delete(keys ...string) error

	// Removes all keys for the specified namespace.
	Reset() error

	// Close the storage, stop any running garbage collectors and closes open connections.
	Close() error
}

Usage

package main

import (
	"fmt"
	"time"
	"github.com/paul-norman/go-fiber-storage/memory"
)

func main() {
	store := memory.New()
	defer store.Close()

	// Error handling omitted for brevity
	err := store.Set("my_key", "lives forever")
	err  = store.Set("one_second", "lives for 1 second", 1 * time.Second)
	err  = store.Set("complex_type", map[string]int64{ "test": 123 })

	// Fetch the Result struct, check that we found a value and that there wasn't an error
	result := store.Get("my_key")
	if !result.Miss() && result.Err() == nil {
		fmt.Println("Value: " + result.String()) // Convert the interface{} to a string
	}

	// Fetch parsed information (value, error, miss) separately
	str, err, miss := store.Get("one_second").String()
	if !miss && err == nil {
		fmt.Println("Value: " + str) // If we are here the value is a string
	}

	// Sleep for 2 seconds
	time.Sleep(2 * time.Second)

	// This will result in a miss
	str, err, miss = store.Get("one_second").String()
	if miss {
		fmt.Println("The value has gone...") // str and err will be nil
	}

	// Complex types are also simple
	item, err, miss := store.Get("complex_type").Interface()
	if !miss && err == nil {
		fmt.Println(item.(map[string]int64)) // Convert the interface to the desired type
	}

	// Remove the keys - doesn't matter that one has already expired
	err = store.Delete("my_key", "one_second", "complex_type")

	// Or
	err = store.Reset()
}

Storage Implementations

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Result added in v0.0.4

type Result struct {
	Value  any
	Error  error
	Missed bool
}

Result struct for returning data back to its original types

func (*Result) Bool added in v0.0.4

func (r *Result) Bool() (bool, error, bool)

Return the result back as a boolean

func (*Result) BoolSlice added in v0.0.4

func (r *Result) BoolSlice() ([]bool, error, bool)

Return the result back as a bool slice

func (*Result) ByteSlice added in v0.0.4

func (r *Result) ByteSlice() ([]byte, error, bool)

Return the result back as a byte slice

func (*Result) Bytes added in v0.0.4

func (r *Result) Bytes() ([]byte, error, bool)

Return the result back as a byte slice

func (*Result) Err added in v0.0.4

func (r *Result) Err() error

Return the result error

func (*Result) Float added in v0.0.4

func (r *Result) Float() (float64, error, bool)

Return the result back as a 64-bit float

func (*Result) Float32 added in v0.0.4

func (r *Result) Float32() (float32, error, bool)

Return the result back as a 32-bit float

func (*Result) Float32Slice added in v0.0.4

func (r *Result) Float32Slice() ([]float32, error, bool)

Return the result back as a float32 slice

func (*Result) Float64 added in v0.0.4

func (r *Result) Float64() (float64, error, bool)

Return the result back as a 64-bit float

func (*Result) Float64Slice added in v0.0.4

func (r *Result) Float64Slice() ([]float64, error, bool)

Return the result back as a float64 slice

func (*Result) Hit added in v0.0.4

func (r *Result) Hit() bool

Return whether the result was present in the cache

func (*Result) Int added in v0.0.4

func (r *Result) Int() (int, error, bool)

Return the result back as an integer

func (*Result) Int64 added in v0.0.4

func (r *Result) Int64() (int64, error, bool)

Return the result back as a 64-bit integer

func (*Result) Int64Slice added in v0.0.4

func (r *Result) Int64Slice() ([]int64, error, bool)

Return the result back as an int64 slice

func (*Result) IntSlice added in v0.0.4

func (r *Result) IntSlice() ([]int, error, bool)

Return the result back as an int slice

func (*Result) Interface added in v0.0.4

func (r *Result) Interface() (any, error, bool)

Return the result back as an interface{}

func (*Result) Miss added in v0.0.4

func (r *Result) Miss() bool

Return whether the result was not present in the cache

func (*Result) Result added in v0.0.4

func (r *Result) Result() (any, error, bool)

Return the result back as an interface{} (Redis naming)

func (*Result) String added in v0.0.4

func (r *Result) String() (string, error, bool)

Return the result back as a string

func (*Result) StringSlice added in v0.0.4

func (r *Result) StringSlice() ([]string, error, bool)

Return the result back as a string slice

func (*Result) Text added in v0.0.4

func (r *Result) Text() (string, error, bool)

Return the result back as a string (Redis naming)

func (*Result) TextSlice added in v0.0.4

func (r *Result) TextSlice() ([]string, error, bool)

Return the result back as a string slice (Redis naming)

func (*Result) UUID added in v0.0.4

func (r *Result) UUID() (uuid.UUID, error, bool)

Return the result back as a UUID

func (*Result) Uint64 added in v0.0.4

func (r *Result) Uint64() (uint64, error, bool)

Return the result back as an unsigned 64-bit integer

func (*Result) Uint64Slice added in v0.0.4

func (r *Result) Uint64Slice() ([]uint64, error, bool)

Return the result back as a uint64 slice

func (*Result) Val added in v0.0.4

func (r *Result) Val() any

Return the result value back as an interface{}

type Storage

type Storage interface {
	// Get the value for the given key.
	// A Result struct is returned allowing various extraction options.
	Get(key string) *Result

	// Set the value for the given key along with an optional expiration value, 0 means no expiration.
	// An empty key will flag an error, but empty values are allowed.
	Set(key string, val any, expiry ...time.Duration) error

	// Deletes the values for the given keys.
	// It returns no error if the storage does not contain the keys.
	Delete(key ...string) error

	// Removes all keys for the specified namespace.
	Reset() error

	// Close the storage, stop any running garbage collectors and closes open connections.
	Close() error
}

Storage interface for communicating with different database/key-value providers

Directories

Path Synopsis
memory module
mysql module
postgres module
redis module
sqlite3 module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL