Internal

internal/memory

import "github.com/gofiber/fiber/v3/internal/memory"

Package memory provides a high-performance in-memory storage that can store any type without encoding overhead. Unlike the standard storage interface, this storage works directly with Go types for maximum speed.

Safety Considerations

This storage automatically performs defensive copying for:

  • String keys: Copied to prevent corruption from pooled buffers
  • []byte values: Copied on both Set and Get to prevent external mutation

For other types (structs, ints, etc.), Go's value semantics provide natural protection. However, if storing pointers or slices of non-byte types, callers are responsible for not mutating the underlying data.

This storage is primarily used internally by middleware for performance- critical operations where the stored data types are known and controlled.

Functions

func New() *Storage

Source: internal/memory/memory.go:41

New constructs an in-memory Storage initialized with a background GC loop.

Types

type Storage

Source: internal/memory/memory.go:29

Storage stores arbitrary values in memory for use in tests and benchmarks. Storage is safe for concurrent use.

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

func (s *Storage) Delete(key string)

Source: internal/memory/memory.go:98

Delete removes key and its associated value from the storage.

func (s *Storage) DeleteIf(key string, matches func(any) bool) bool

Source: internal/memory/memory.go:106

DeleteIf removes key only when matches accepts its current value. The predicate runs under the storage lock and must not call back into Storage.

func (s *Storage) Get(key string) any

Source: internal/memory/memory.go:55

Get retrieves the value stored under key, returning nil when the entry does not exist or has expired.

For []byte values, this returns a defensive copy to prevent callers from mutating the stored data. Other types are returned as-is.

func (s *Storage) Reset()

Source: internal/memory/memory.go:120

Reset clears the storage by dropping every stored key.

func (s *Storage) Set(key string, val any, ttl time.Duration)

Source: internal/memory/memory.go:77

Set stores val under key and applies the optional ttl before expiring the entry. A non-positive ttl keeps the item forever.

String keys are defensively copied to prevent corruption from pooled buffers. []byte values are also copied to prevent external mutation of stored data. Other types are stored as-is (structs are copied by value automatically).