A lightweight, thread-safe in-memory cache implementation in Go with TTL support.
- Simple key-value storage
- Thread-safe operations with mutex locks
- TTL (Time-To-Live) support for cached items
- Basic operations: Set, Get, Delete
- Count functionality for cached items
- TCP server implementation for network access
git clone https://github.com/yourusername/go-cache.git
cd go-cache
go buildimport "your-module/db"
// Create a new database instance
cache := db.NewDB()
// Set a value with TTL
cache.Set("key", "value", 3600) // TTL in seconds
// Get a value
value, err := cache.Get("key")
if err != nil {
// Handle error (not found or expired)
}
// Delete a value
cache.Delete("key")
// Get count of active items
count := cache.Count()The project includes a TCP server implementation that listens on port 3169.
To run the server:
go run main.go.
├── main.go # TCP server implementation
├── db
│ └── db.go # Core cache implementation
└── README.md
Set(key, value string, ttlSeconds int64): Stores a value with an optional TTLGet(key string) (string, error): Retrieves a value, returns error if not found or expiredDelete(key string): Removes a value from the cacheCount() int: Returns the count of non-expired items in the cache
The implementation uses sync.RWMutex to ensure thread-safe operations on the cache.