cache-db

module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 22, 2025 License: MIT

README ΒΆ

πŸ—„οΈ 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
Persisted[K,V] β†’ On-disk format (with version & defaultTTL)

🀝 Contributing

Feel free to contribute by opening pull requests to help improve and extend this project.
Your contributions are always welcome!

License

MIT License

Directories ΒΆ

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL