https://github.com/bay0/kvs
kvs is a simple key-value store library for Go.
https://github.com/bay0/kvs
go key-value-store kvs memory module package sharding
Last synced: 2 months ago
JSON representation
kvs is a simple key-value store library for Go.
- Host: GitHub
- URL: https://github.com/bay0/kvs
- Owner: bay0
- License: mit
- Created: 2023-02-17T15:24:52.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-02-19T17:28:24.000Z (over 2 years ago)
- Last Synced: 2025-02-01T02:43:33.198Z (4 months ago)
- Topics: go, key-value-store, kvs, memory, module, package, sharding
- Language: Go
- Homepage: https://pkg.go.dev/github.com/bay0/kvs
- Size: 28.3 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# kvs
kvs is a simple key-value store library for Go.
It offers the following functionality:
* Get: retrieve a value associated with a given key from the store
* Set: add or update a key-value pair in the store
* Delete: remove a key-value pair associated with a given key from the store
* Keys: retrieve a slice of all the keys in the storeThis library defines two interfaces:
`Value` which defines the methods a value in the key-value store must implement
`Store` which defines the methods that a key-value store must implement
`ErrCode` defines an enumeration that represents the error codes that can be returned by the store.
The error codes are:
* `ErrUnknown`: represents an unknown error
* `ErrNotFound`: represents an error that occurs when the key is not found in the store
* `ErrDuplicate`: represents an error that occurs when the key already exists in the store## Installation
Use `go get` to install kvs.
```bash
go get github.com/bay0/kvs
```## Usage
To use the library, import the kvs package and create a new instance of the KeyValueStore using NewKeyValueStore().
```go
package mainimport (
"fmt""github.com/bay0/kvs"
)type Person struct {
Name string
Age int
}func (p *Person) Clone() kvs.Value {
return &Person{
Name: p.Name,
Age: p.Age,
}
}func main() {
// Create a new key-value store with sharding enabled
store := kvs.NewKeyValueStore(2)// Create a new person value
person := &Person{
Name: "John",
Age: 20,
}// Set the person value in the store
err := store.Set("person", person)
if err != nil {
// Handle the error
}// Get the person value from the store
val, err := store.Get("person")
if err != nil {
// Handle the error
}// Cast the value to a person object
personVal, ok := val.(*Person)
if !ok {
// Handle the error
}// Print the person's name and age
fmt.Printf("%s is %d years old.\n", personVal.Name, personVal.Age)// Delete the person value from the store
err = store.Delete("person")
if err != nil {
// Handle the error
}// Get all keys from the store
keys := store.Keys()
fmt.Println("Keys in the store:", keys)
}```