Documentation ¶
Overview ¶
Example (Memory) ¶
package main import ( "context" "fmt" "github.com/brexhq/substation/v2/config" "github.com/brexhq/substation/v2/internal/kv" ) func main() { ctx := context.TODO() // create KV config cfg := config.Config{ Type: "memory", Settings: map[string]interface{}{ "capacity": 3, }, } // get KV store using factory method kvStore, err := kv.Get(cfg) if err != nil { panic(err) } // setup and defer closing KV store if err := kvStore.Setup(ctx); err != nil { panic(err) } defer kvStore.Close() // set a series of values in the store if err := kvStore.Set(ctx, "foo", "bar"); err != nil { panic(err) } if err := kvStore.Set(ctx, "baz", "qux"); err != nil { panic(err) } if err := kvStore.Set(ctx, "quux", "corge"); err != nil { panic(err) } // retrieve a value from the store item, err := kvStore.Get(ctx, "foo") if err != nil { panic(err) } fmt.Println(item) }
Output: bar
Example (MemoryWithTTL) ¶
package main import ( "context" "fmt" "time" "github.com/brexhq/substation/v2/config" "github.com/brexhq/substation/v2/internal/kv" ) func main() { ctx := context.TODO() // create KV config cfg := config.Config{ Type: "memory", Settings: map[string]interface{}{ "capacity": 1, }, } // get KV store using factory method kvStore, err := kv.Get(cfg) if err != nil { panic(err) } // setup and defer closing KV store if err := kvStore.Setup(ctx); err != nil { panic(err) } defer kvStore.Close() // set a value with time-to-live enabled in the KV store ttl := time.Now().Add(1 * time.Microsecond).Unix() if err := kvStore.SetWithTTL(ctx, "foo", "bar", ttl); err != nil { panic(err) } time.Sleep(15 * time.Microsecond) // retrieve a value from the store // if the time-to-live has passed, then the item has expired and is // no longer available item, err := kvStore.Get(ctx, "foo") if err != nil { panic(err) } fmt.Println(item) }
Output: <nil>
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrNoLock is returned when a lock cannot be acquired. ErrNoLock = fmt.Errorf("unable to acquire lock") )
Functions ¶
This section is empty.
Types ¶
type Locker ¶
type Locker interface { Lock(context.Context, string, int64) error Unlock(context.Context, string) error Setup(context.Context) error IsEnabled() bool }
type Storer ¶
type Storer interface { Get(context.Context, string) (interface{}, error) Set(context.Context, string, interface{}) error SetWithTTL(context.Context, string, interface{}, int64) error SetAddWithTTL(context.Context, string, interface{}, int64) error Setup(context.Context) error Close() error IsEnabled() bool }
Storer provides tools for getting values from and putting values into key-value stores.
Click to show internal directories.
Click to hide internal directories.