This is a simple implementation of key-value store in Go. It serves as simple caching server that can store data for specified duration with sub 1ms read and write speed.
Detailed description of the code and design decisions I took when writing it is in this Medium article.
Clone the repository and run:
go run .
You can create optimized build by running:
go build .
After building and running the application, the API will be exposed on port 3000, the storage has 3 methods, PUT, GET, DELETE
- PUT - creates new write, accepts json with key-value pairs, value can be any data compatible with json:
PUT http://127.0.0.1:3000/
{
"foo": "bar",
"complexVal": {"foo": 0, "bar": 3.14}
}
- GET - reads data from storage by key
GET http://127.0.0.1:3000/
{
"key": "complexVal"
}
returns:
{
"complexVal": {
"bar": 3.14,
"foo": 0
}
}
- DELETE - deletes write:
DELETE http://127.0.0.1:3000/
{
"key": "complexVal"
}
Main usage of the store is intended to be caching, therefore the data will expire after specified time (default 3min).
During operation the app can write logs into a .txt file which then can be read to reconstruct the database if it were to go offline.
The applications includes number of variables that can be set by the user. The variables are:
- use_logs - boolean - ff true, logs will be written to logs_filename
- reconstruct_from_logs - boolean - reconstruct database from existing log file
- logs_filename - string - name of the log file, has to be .txt
- expiration - time.Duration - how long to store key-value pairs for
- port - int - port at which to run the server
Example run:
go run . --use_logs=false --reconstruct_from_logs=false --logs_filename=logs.txt --expiration=1m30s --port=3000