store
import store "ganigeorgiev.com/pocketbase/pocketbase/tools/store"
Source: tools/store.
Constants
const ShrinkThreshold = 200 // the number is arbitrary chosen
@todo remove after https://github.com/golang/go/issues/20135
Types
Store
type Store[K comparable, T any] struct {
// contains filtered or unexported fields
}
Store defines a concurrent safe in memory key-value data store.
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).
Store.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.
Store.GetAll
func (s *Store[K, T]) GetAll() map[K]T
GetAll returns a shallow copy of the current store data.
Store.GetOk
func (s *Store[K, T]) GetOk(key K) (T, bool)
GetOk is similar to Get but returns also a boolean indicating whether the key exists or not.
Store.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.
Store.Has
func (s *Store[K, T]) Has(key K) bool
Has checks if element with the specified key exist or not.
Store.Keys
func (s *Store[K, T]) Keys() []K
Keys returns a slice with all of the store keys.
Store.Length
func (s *Store[K, T]) Length() int
Length returns the current number of elements in the store.
Store.MarshalJSON
func (s *Store[K, T]) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler and export the current store data into valid JSON.
Store.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.
Store.RemoveAll
func (s *Store[K, T]) RemoveAll()
RemoveAll removes all the existing store entries.
Store.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.
Store.Set
func (s *Store[K, T]) Set(key K, value T)
Set sets (or overwrite if already exists) a new value for key.
Store.SetFunc
func (s *Store[K, T]) SetFunc(key K, fn func(old T) T)
SetFunc sets (or overwrite if already exists) a new value resolved from the function callback for the provided key.
The function callback receives as argument the old store element value (if exists). If there is no old store element, the argument will be the T zero value.
Example:
s := store.New[string, int](nil)
s.SetFunc("count", func(old int) int {
return old + 1
})
Store.SetIfLessThanLimit
func (s *Store[K, T]) SetIfLessThanLimit(key K, value T, maxAllowedElements int) bool
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.
Store.UnmarshalJSON
func (s *Store[K, T]) UnmarshalJSON(data []byte) error
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.
Store.Values
func (s *Store[K, T]) Values() []T
Values returns a slice with all of the store values.
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.