FlashKV is a simple and fast key-value database written in Go. It loads the entire dataset into memory for quick access, making it ideal for applications requiring high-speed data retrieval and storage.
- In-memory Storage: All data is stored in memory, ensuring high-speed access.
- Thread-safe Operations: Uses sync.Mutex for thread safety.
- Efficient Data Loading and Saving: Loads and saves data in batches for performance optimization.
- Simple API: Easy-to-use functions for adding, retrieving, deleting, and iterating over key-value pairs.
To install FlashKV, you need to have Go installed. You can then use go get to install the package:
go get github.com/goupdate/flashkvBelow is a basic example of how to use FlashKV:
package main
import (
"fmt"
"log"
"github.com/goupdate/flashkv"
)
func main() {
db := flashkv.New()
// Load data from a file
err := db.Load("data.db")
if err != nil {
log.Fatalf("failed to load database: %v", err)
}
// Add a new key-value pair
db.Add("key1", []byte("value1"))
// Retrieve a value
val, found := db.Get("key1")
if found {
fmt.Printf("Key: key1, Value: %s\n", val)
} else {
fmt.Println("Key not found")
}
// Save the database
err = db.Save()
if err != nil {
log.Fatalf("failed to save database: %v", err)
}
// Iterate over all key-value pairs
db.Iterate(func(key string, val []byte) bool {
fmt.Printf("Key: %s, Value: %s\n", key, val)
return true
})
}func New() *DBCreates a new database instance.
func (d *DB) Load(name string) errorLoads data from the specified file into the database.
func (d *DB) Save() errorSaves the database to the file it was loaded from.
func (d *DB) SaveAs(name string) errorSaves the database to the specified file.
func (d *DB) Add(key string, val []byte)Adds a key-value pair to the database.
func (d *DB) Get(key string) ([]byte, bool)Retrieves the value for the specified key. Returns the value and a boolean indicating if the key was found.
func (d *DB) Iterate(fn func(key string, val []byte) bool)Iterates over all key-value pairs in the database, calling the provided function for each pair. If the function returns false, the iteration stops.
func (d *DB) Exist(key string) boolChecks if a key exists in the database.
func (d *DB) Delete(key string)Deletes a key-value pair from the database.
func (d *DB) Count() intReturns the number of key-value pairs in the database.