Skip to content
/ memkey Public

Simple type-safe in memory key-value store

License

Notifications You must be signed in to change notification settings

mymmrac/memkey

Repository files navigation

📦 MemKey

Go Reference Go Version

CI Status Race Testing Quality Gate Status Go Report
Coverage Code Smells Lines of Code

Very simple type-safe, thread-safe in memory key-value store with zero dependencies.

⚡ Getting Started

How to get the library:

go get -u github.com/mymmrac/memkey

Create new store:

s := &memkey.Store[comparable]{}

As a key for store you can use any comparable type, values can be of any type.

🪁 Features

All functions are type-safe and thread-safe, there is also type-unsafe variant of all functions (just use same methods of Store struct).

Method Description
Get Get value
MustGet Get value or zero value
Set Set value
Type Get type name of value
MustType Get type name or empty string
Has Check if value exists
Delete Delete value
Len Number of elements stored
Keys Get keys
Values Get values
Entries Get key-value pairs
ForEach Iterate over key-value pairs

🧩 Usage

s := &memkey.Store[int]{}

memkey.Set(s, 1, "mem")
memkey.Set(s, 2, "key")
memkey.Set(s, 3, 42.0)

m, ok := memkey.Get[string](s, 1)
k, ok := memkey.Get[string](s, 2)
// Here `m` & `k` will be of type string and have zero value if not found, 
// `ok` will indicate if value was found

n, ok := memkey.Get[float64](s, 3)
// Here `n` will be float64

found := memkey.Has[uint](s, 2)
// Here `found` will be `false`, since value with key `2` is `string` and not an `uint`

keys := s.Keys()
// Here `keys` will be a slice of `1`, `2` and `3` in non-deterministic order, 
// Methods called on `Store` will ignore the type of values

🔐 License

MemKey is distributed under MIT.