Help us shape the future of Go! Take the 2026 Go Developer Survey

kvstore

package
v3.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 7 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Filename specifies the path of the on-disk file associated to the key-value store.
	Filename string

	// AutoSave specifies whether the store
	// must be written to disk automatically after every modification.
	// When AutoSave is false, stores are only saved to disk upon shutdown
	// or when the [Service.Save] method is called manually.
	AutoSave bool
}

type KVStoreService

type KVStoreService struct {
	// contains filtered or unexported fields
}

func New

func New() *KVStoreService

New initialises an in-memory key-value store. See NewWithConfig for details.

func NewWithConfig

func NewWithConfig(config *Config) *KVStoreService

NewWithConfig initialises a key-value store with the given configuration:

  • if config is nil, the new store is in-memory, i.e. not associated with a file;
  • if config is non-nil, the associated file is not loaded until [Service.Load] is called.

If the store is registered with the application as a service, [Service.Load] will be called automatically at startup.

func (*KVStoreService) Clear

func (kvs *KVStoreService) Clear() error

Clear deletes all keys from the store. If AutoSave is true, the store is saved to disk.

func (*KVStoreService) Configure

func (kvs *KVStoreService) Configure(config *Config)

Configure changes the store's configuration. The contents of the store at call time are preserved and marked unsaved. Consumers will need to call [Service.Load] manually after Configure in order to load a new file.

If the store is unsaved upon calling Configure, no attempt is made at saving it. Consumers will need to call [Service.Save] manually beforehand.

See NewWithConfig for details on configuration.

func (*KVStoreService) Delete

func (kvs *KVStoreService) Delete(key string) error

Delete deletes the given key from the store. If AutoSave is true, the store is saved to disk.

func (*KVStoreService) Get

func (kvs *KVStoreService) Get(key string) any

Get returns the value for the given key. If key is empty, a shallow snapshot of the entire store is returned. Nested values are not copied.

func (*KVStoreService) Load

func (kvs *KVStoreService) Load() error

Load loads the store from disk. If the store is in-memory, i.e. not associated with a file, Load has no effect. If the operation fails, a non-nil error is returned and the store's content and state at call time are preserved.

func (*KVStoreService) Save

func (kvs *KVStoreService) Save() error

Save saves the store to disk. If the store is in-memory, i.e. not associated with a file, Save has no effect.

func (*KVStoreService) ServiceName

func (kvs *KVStoreService) ServiceName() string

ServiceName returns the name of the plugin.

func (*KVStoreService) ServiceShutdown

func (kvs *KVStoreService) ServiceShutdown() error

ServiceShutdown saves the store to disk if it is associated with a file. It returns a non-nil error in case of failure.

func (*KVStoreService) ServiceStartup

func (kvs *KVStoreService) ServiceStartup(ctx context.Context, options application.ServiceOptions) error

ServiceStartup loads the store from disk if it is associated with a file. It returns a non-nil error in case of failure.

func (*KVStoreService) Set

func (kvs *KVStoreService) Set(key string, value any) error

Set sets the value for the given key. If AutoSave is true, the store is saved to disk.

Jump to

Keyboard shortcuts

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