Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rubiojr/kv
Simple key-value store on top of SQLite or MySQL
https://github.com/rubiojr/kv
database key-value mysql sqlite
Last synced: about 1 month ago
JSON representation
Simple key-value store on top of SQLite or MySQL
- Host: GitHub
- URL: https://github.com/rubiojr/kv
- Owner: rubiojr
- License: mit
- Created: 2022-03-10T23:11:58.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-05-14T13:20:39.000Z (8 months ago)
- Last Synced: 2024-11-18T04:03:53.734Z (about 2 months ago)
- Topics: database, key-value, mysql, sqlite
- Language: Go
- Homepage:
- Size: 144 KB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# KV
A simple key/value store on top of SQLite or MySQL (Go port of [GitHub's KV](https://github.com/github/github-ds/blob/master/lib/github/kv.rb)).
Aims compatible with the original implementation by default, offering a few extra backend drivers and some extra configuration knobs.
## Status
Work in progress.
* Drivers are almost feature complete.
* No optmization work has happened yet.
* Both drivers could use a few extra tests.
* API changes not expected at this point.
* Not SQL injection freeMissing functionality:
- [ ] Custom driver options for MySQL and SQLite
- [ ] setnx
- [ ] increment
- [ ] ttl
- [ ] mttl
- [ ] Configurable key/value max length
- [ ] Enforce default key and value max length## Initialization
Import the module first:
```Go
import "github.com/rubiojr/kv"
```### MySQL
```Go
db, err := kv.New("mysql", "root:toor@tcp(127.0.0.1:3306)/gokv")
```Note that the database `gokv` and the table name used by default `key_values` will need to be created before using this driver.
```sql
USE gokv
CREATE TABLE IF NOT EXISTS key_values (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`key` varchar(255) NOT NULL,
`value` blob NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
`expires_at` datetime DEFAULT NULL,
PRIMARY KEY (id),
UNIQUE KEY index_key_values_on_key (`key`),
KEY index_key_values_on_expires_at (expires_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
```### SQLite
```Go
db, err := kv.New("sqlite", "my.db")
```Creates a `key_values` table in `my.db` file database to store key/values.
## Getting and setting keys
Single keys:
```Go
// set a couple of keys
err = db.Set("foo", []byte("bar"), nil)
if err != nil {
panic(err)
}
err = db.Set("stuff", []byte("staff"), nil)
if err != nil {
panic(err)
}// Get one key
v, err := db.Get("foo")
if err != nil {
panic(err)
}
fmt.Println(string(v)) // prints bar// Deleting a single key
err := db.Del("foo")
if err != nil {
panic(err)
}
```Multiple keys:
```Go
// Get multiple keys
values, err := db.MGet("foo", "staff")
if err != nil {
panic(err)
}
// iterate the results
for _, v := range values {
fmt.Println(string(v))
}// Set multiple keys
values := types.KeyValues{}
values["mset1"] = "msetv1"
values["mset2"] = "msetv2"
err = db.MSet(values, nil)// Deleting multiple keys
err := db.MDel("mset1", "mset2")
if err != nil {
panic(err)
}```
### Storing binary values
An example using [vmihailenco/msgpack](https://github.com/vmihailenco/msgpack) to serialize data.
```Go
// store a string as a binary blob
b, err := msgpack.Marshal("blob")
if err != nil {
panic(err)
}err = db.Set("bin", b, nil)
if err != nil {
panic(err)
}v, err = db.Get("bin")
if err != nil {
fmt.Println(err)
}var blobStr string
err = msgpack.Unmarshal(b, &blobStr)
if err != nil {
panic(err)
}
fmt.Println(blobStr)
```