modulepackage
0.0.0-20241128085945-6642ccdb4227
Repository: https://github.com/chriswrendev/go-kvstore.git
Documentation: pkg.go.dev
# README
Key-Value Store Project
This repository contains a simple key-value store implementation written in Go. It includes a generic, thread-safe storage system with basic CRUD operations and exposes HTTP endpoints for interacting with the store.
Project Structure
- Main Package:
- Contains the
main
entry point, initializing the server and starting the HTTP service.
- Contains the
- Storer Interface:
- Defines the contract for the key-value store with methods for
Put
,Get
,Update
, andDelete
.
- Defines the contract for the key-value store with methods for
- KVStore:
- A thread-safe, generic implementation of the
Storer
interface using Go's generics and synchronization primitives.
- A thread-safe, generic implementation of the
- Server:
- Implements HTTP handlers using the Echo framework for
Put
andGet
operations, backed by theKVStore
.
- Implements HTTP handlers using the Echo framework for
Features
-
Generic Key-Value Store:
KVStore
supports any comparable type for keys and any type for values using Go's generics.- Thread-safe operations using
sync.RWMutex
to ensure safe concurrent access.
-
CRUD Operations:
Put
: Add or update a key-value pair.Get
: Retrieve a value by its key.Update
: Modify an existing key-value pair.Delete
: Remove a key-value pair from the store.
-
HTTP API:
GET /put/:key/:value
: Adds a key-value pair to the store.GET /get/:key
: Retrieves the value for a given key.
Project Architecture
Design Patterns
-
Generic Repository:
- The
Storer
interface abstracts the implementation of the key-value store, making it easy to swap or extend the storage backend. KVStore
implements this interface with a map for simplicity.
- The
-
Thread-Safe Singleton:
- The
KVStore
ensures thread safety withsync.RWMutex
. - Read-heavy operations (
Get
) use a read lock, while write operations (Put
,Delete
) use a write lock.
- The
-
RESTful API Design:
- The
Server
exposes HTTP routes to interact with the store, adhering to REST principles for easy client integration.
- The
Installation and Setup
Prerequisites
- Go 1.18 or later
github.com/labstack/echo/v4
for the HTTP server
Steps
-
Clone the repository:
git clone https://github.com/your-repo/kv-store.git cd kv-store
-
Install dependencies:
go mod tidy
-
Run the server:
go run main.go
The server will start on http://localhost:3000.
Future Enhancements
- Add more HTTP endpoints for Update and Delete operations.
- Implement persistent storage to save the data beyond the application's lifecycle.
- Introduce authentication and authorization for API endpoints.
- Enhance error handling and logging mechanisms.
- Extend support for additional data types for values (e.g., JSON objects).
# Interfaces
No description provided by the author