kv
import "github.com/tangoui/tango/service/kv"本页由
site/tools/apigen从源码注释生成(按 darwin 构建标签选文件),请勿手改。
Package kv is a small thread-safe key-value store for app settings and state: kept in memory and, optionally, persisted to a JSON file.
Values are anything encoding/json can encode. Once persisted and loaded again they come back as JSON decodes them into an any (numbers as float64, objects as map[string]any), so use the store for simple values.
A *Store can be bound to the page with bind.Register or service.Bind; its exported methods are Get, Has, Set, Delete, Keys and Clear.
函数
func WriteFileAtomic
func WriteFileAtomic(path string, data []byte, perm fs.FileMode) (err error)WriteFileAtomic writes data to path through a temporary file in the same directory that is synced and then renamed over path. Missing parent directories are created.
类型
Store
type Store struct {
// contains filtered or unexported fields
}Store is a key-value store. Its methods are safe for concurrent use.
func New
func New(path string) (*Store, error)New returns a store persisted to the JSON file at path, loading it if it exists. Every change rewrites the file atomically (a temporary file in the same directory renamed over it), so a crash leaves either the old or the new contents. An empty path returns a memory-only store.
func (*Store) Clear
func (s *Store) Clear() errorClear removes every key.
func (*Store) Delete
func (s *Store) Delete(key string) errorDelete removes key. Deleting a missing key is not an error.
func (*Store) Get
func (s *Store) Get(key string) anyGet returns the value stored under key, or nil if there is none.
func (*Store) Has
func (s *Store) Has(key string) boolHas reports whether a value is stored under key.
func (*Store) Keys
func (s *Store) Keys() []stringKeys returns the stored keys in sorted order.
func (*Store) Set
func (s *Store) Set(key string, value any) errorSet stores value under key. If the store is persisted and the file cannot be written, the store is left unchanged.