Extremely simple Key/Value store for Go (golang).
Currently comes with only one backend implementation - MySQL. gkv
doesn't support transactions or other fancy features.
The only methods it supports are Set(key, value)
, Get(key)
and Delete(key)
, where both key
and value
are strings.
CREATE TABLE `key_value_store`
`key` VARBINARY(32) PRIMARY KEY,
`value` VARBINARY(128) NOT NULL
)
import "github.com/petrkotek/gkv"
db, err := sql.Open("mysql", "user:pwd@tcp(localhost:3306)/test_db")
if err != nil {
// TODO: handle error
}
kv, err := gkv.NewMySQLKeyValueStore(db, &gkv.SQLKeyValueStoreConfig{
TableName: "key_value_store",
KeyColumn: "key",
ValueColumn: "value",
MaxKeyLen: 32,
MaxValueLen: 128,
})
if err != nil {
// TODO: handle error
}
// set a value for the key
err = kv.Set("foo", "bar")
if err != nil {
// TODO: handle error
}
// retrieve the value
value, err := kv.Get("foo")
if err != nil {
// TODO: handle error
}
fmt.Print(value)
// delete the key
err = kv.Del("foo")
if err != nil {
// TODO: handle error
}
kv.Close()
This project is useful only for limited number of use cases (such as when you already have MySQL database/cluster running and don't want to use proper Key-Value database).
In many cases, safer choice will be using for instance one of these packages, which purely use Go and file-backed storage: