ποΈ cache-db
A lightweight in-memory key-value store for Go with:
- Generic key & value types
- Per-key TTL (expiration)
- Thread-safe access (RWMutex)
- Persistence with
gob (atomic writes with temp + rename)
- Simple file management (
Load, Persist, DeleteFile)
π Installation
go get github.com/Serajian/cache-db.git
π¦ Usage
Define Database
package main
import (
"fmt"
"time"
"github.com/Serajian/cache-db.git/database"
)
func main() {
// Create a DB with string keys and string values.
// Default TTL = 2 seconds, data persisted under ./data directory.
db := database.NewDatabase[string, string](2*time.Second, "./data")
// Set a key with default TTL
db.Set("foo", "bar")
// Persist to disk
if err := db.Persist("test.gob"); err != nil {
panic(err)
}
// Load into a fresh DB
db2 := database.NewDatabase[string, string](0, "./data")
if err := db2.Load("test.gob"); err != nil {
panic(err)
}
// Retrieve
if v, ok := db2.Get("foo"); ok {
fmt.Println("Loaded value:", v)
}
// TTL expiry check
time.Sleep(3*time.Second)
if _, ok := db2.Get("foo"); !ok {
fmt.Println("foo expired as expected")
}
// Delete file
if err := db2.DeleteFile("test.gob"); err != nil {
panic(err)
}
}
π Features
Set / Get / Delete / Clear
-TTL support: Expiration per-key or default for DB
-CleanExpired: Manually purge expired entries
Persistence:
-Persist(filename) β Save DB atomically to disk
-Load(filename) β Load DB state from disk
-DeleteFile(filename) β Remove persisted file safely
-Thread-safe with sync.RWMutex
π Model Types
Entry[V] β Wraps value with ExpiresAt time.Time
π€ Contributing
Feel free to contribute by opening pull requests to help improve and extend this project.
Your contributions are always welcome!
License
MIT License