Documentation ¶
Index ¶
- Constants
- type Store
- func (s *Store[T]) Get(key string) T
- func (s *Store[T]) GetAll() map[string]T
- func (s *Store[T]) GetOk(key string) (T, bool)
- func (s *Store[T]) GetOrSet(key string, setFunc func() T) T
- func (s *Store[T]) Has(key string) bool
- func (s *Store[T]) Length() int
- func (s *Store[T]) MarshalJSON() ([]byte, error)
- func (s *Store[T]) Remove(key string)
- func (s *Store[T]) RemoveAll()
- func (s *Store[T]) Reset(newData map[string]T)
- func (s *Store[T]) Set(key string, value T)
- func (s *Store[T]) SetIfLessThanLimit(key string, value T, maxAllowedElements int) bool
- func (s *Store[T]) UnmarshalJSON(data []byte) error
- func (s *Store[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[T any] struct { // contains filtered or unexported fields }
Store defines a concurrent safe in memory key-value data store.
func (*Store[T]) Get ¶
Get returns a single element value from the store.
If key is not set, the zero T value is returned.
func (*Store[T]) GetOk ¶ added in v0.23.0
GetOk is similar to Get but returns also a boolean indicating whether the key exists or not.
func (*Store[T]) GetOrSet ¶ added in v0.23.0
GetOrSet retrieves a single existing value for the provided key or stores a new one if it doesn't exist.
func (*Store[T]) Length ¶ added in v0.12.0
Length returns the current number of elements in the store.
func (*Store[T]) MarshalJSON ¶ added in v0.23.0
MarshalJSON implements json.Marshaler and export the current store data into valid JSON.
func (*Store[T]) Remove ¶
Remove removes a single entry from the store.
Remove does nothing if key doesn't exist in the store.
func (*Store[T]) RemoveAll ¶ added in v0.2.6
func (s *Store[T]) RemoveAll()
RemoveAll removes all the existing store entries.
func (*Store[T]) Reset ¶ added in v0.12.0
Reset clears the store and replaces the store data with a shallow copy of the provided newData.
func (*Store[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[T]) UnmarshalJSON ¶ added in v0.23.0
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.