store

package module
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2022 License: BSD-3-Clause Imports: 2 Imported by: 0

README

Store

Provides a faster and more memory-efficient alternative to the sync.Map that takes advantage of Go 1.18 Generics.

Table of Contents

Quick Start

Simply do the typical go get as always when consuming Go libraries.

go get github.com/Gophigure/store@latest

NOTE If you want examples, check out store_test.go.

NOTE Typical usage is no different to sync.Map, but it's just faster and more memory-efficient with added type-safety.

Why?

Using sync.Map can be a challenge, as well as frustrating due to requiring lots of type-casting as it does not use generics.

Other than that, there are no major problems with sync.Map's implementation. It works, and it's not incredibly slow given you adhere to the warning that it best suits workloads that require lots of reading but not much writing.

Features

  • Type-safety, the Store type uses generics, enforcing all passed and received data conforms to the appropriate types.

  • Memory-efficient, as per my own testing, Store uses less memory on average than sync.Map.

    You may want to validate this with your own workload and hardware, this was done on my own machine with a very simple workload.

  • Faster, as per my own testing, Store is typically faster than sync.Map.

    You may want to validate this with your own workload and hardware, this was done on my own machine with a very simple workload.

  • Multiple functions for manipulating or working through the stored data.

    Function Description
    .Get Retrieves a value from the Store using the given key.
    .Set Sets a value in the Store using the given key, if a value already exists under said key it will be overwritten.
    .GetOrSet Either gets or sets the given key to the given value in the Store, getting takes precedence and will not replace the key if found.
    .Delete Removes a key from the Store.
    .Pluck Removes and returns the value of the given key from the Store.
    .ForEach Iterates over every value in the Store, passing each key and value to the given function.

Licensing

Store is licensed under the BSD 3-Clause LICENSE.

Documentation

Overview

Package store provides Store, which is an alternative to the sync.Map type and supports both type-safety using 1.18 generics and avoids the unsafe package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Store

type Store[T comparable, K any] struct {
	// contains filtered or unexported fields
}

Store is a go-routine safe way to store an arbitrary map. Its zero-value is safe to use. It must not be copied after first use.

func (*Store[T, K]) Delete

func (s *Store[T, K]) Delete(key T) (ok bool)

Delete removes a key from the Store, returns true if successful.

func (*Store[T, K]) ForEach

func (s *Store[T, K]) ForEach(f func(key T, value K))

ForEach calls the provided function for each value stored.

func (*Store[T, K]) Get

func (s *Store[T, K]) Get(key T) (value K, ok bool)

Get returns a value from the Store, if it exists.

func (*Store[T, K]) GetOrSet

func (s *Store[T, K]) GetOrSet(key T, val K) (value K, set bool)

GetOrSet only stores value under key if no value already exists under key, returns true if successful.

func (*Store[T, K]) Pluck

func (s *Store[T, K]) Pluck(key T) (value K, ok bool)

Pluck both retrieves and removes a key from the Store.

func (*Store[T, K]) Reset added in v1.1.1

func (s *Store[T, K]) Reset()

Reset removes all stored data from the Store.

func (*Store[T, K]) Set

func (s *Store[T, K]) Set(key T, val K)

Set allows you to store value under key.

Jump to

Keyboard shortcuts

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