memory

package
v2.52.11 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

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.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Storage

type Storage struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func New

func New() *Storage

func (*Storage) Delete

func (s *Storage) Delete(key string)

Delete removes key and its associated value from the storage.

func (*Storage) Get

func (s *Storage) Get(key string) interface{}

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 (*Storage) Reset

func (s *Storage) Reset()

Reset clears the storage by dropping every stored key.

func (*Storage) Set

func (s *Storage) Set(key string, val interface{}, ttl time.Duration)

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).

Jump to

Keyboard shortcuts

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