Documentation
¶
Index ¶
- Constants
- type Store
- func (s *Store[K, T]) Get(key K) T
- func (s *Store[K, T]) GetAll() map[K]T
- func (s *Store[K, T]) GetOk(key K) (T, bool)
- func (s *Store[K, T]) GetOrSet(key K, setFunc func() T) T
- func (s *Store[K, T]) Has(key K) bool
- func (s *Store[K, T]) Length() int
- func (s *Store[K, T]) MarshalJSON() ([]byte, error)
- func (s *Store[K, T]) Remove(key K)
- func (s *Store[K, T]) RemoveAll()
- func (s *Store[K, T]) Reset(newData map[K]T)
- func (s *Store[K, T]) Set(key K, value T)
- func (s *Store[K, T]) SetIfLessThanLimit(key K, value T, maxAllowedElements int) bool
- func (s *Store[K, T]) UnmarshalJSON(data []byte) error
- func (s *Store[K, T]) Values() []T
Constants ¶
const ShrinkThreshold = 200 // the number is arbitrary chosen
@todo remove after https://github.com/golang/go/issues/20135
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Store ¶
type Store[K comparable, T any] struct { // contains filtered or unexported fields }
Store defines a concurrent safe in memory key-value data store.
func New ¶
func New[K comparable, T any](data map[K]T) *Store[K, T]
New creates a new Store[T] instance with a shallow copy of the provided data (if any).
func (*Store[K, T]) Get ¶
func (s *Store[K, T]) Get(key K) T
Get returns a single element value from the store.
If key is not set, the zero T value is returned.
func (*Store[K, T]) GetAll ¶
func (s *Store[K, T]) GetAll() map[K]T
GetAll returns a shallow copy of the current store data.
func (*Store[K, T]) GetOk ¶
GetOk is similar to Get but returns also a boolean indicating whether the key exists or not.
func (*Store[K, T]) GetOrSet ¶
func (s *Store[K, T]) GetOrSet(key K, setFunc func() T) T
GetOrSet retrieves a single existing value for the provided key or stores a new one if it doesn't exist.
func (*Store[K, T]) MarshalJSON ¶
MarshalJSON implements json.Marshaler and export the current store data into valid JSON.
func (*Store[K, T]) Remove ¶
func (s *Store[K, T]) Remove(key K)
Remove removes a single entry from the store.
Remove does nothing if key doesn't exist in the store.
func (*Store[K, T]) RemoveAll ¶
func (s *Store[K, T]) RemoveAll()
RemoveAll removes all the existing store entries.
func (*Store[K, T]) Reset ¶
func (s *Store[K, T]) Reset(newData map[K]T)
Reset clears the store and replaces the store data with a shallow copy of the provided newData.
func (*Store[K, T]) Set ¶
func (s *Store[K, T]) Set(key K, value T)
Set sets (or overwrite if already exist) a new value for key.
func (*Store[K, T]) SetIfLessThanLimit ¶
SetIfLessThanLimit sets (or overwrite if already exist) a new value for key.
This method is similar to Set() but **it will skip adding new elements** to the store if the store length has reached the specified limit. false is returned if maxAllowedElements limit is reached.
func (*Store[K, T]) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler and imports the provided JSON data into the store.
The store entries that match with the ones from the data will be overwritten with the new value.