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 {
// contains filtered or unexported fields
}
Storage stores arbitrary values in memory for use in tests and benchmarks.
func New ¶
func New() *Storage
New constructs an in-memory Storage initialized with a background GC loop.
func (*Storage) Get ¶
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 ¶
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).