Redis is a well-known in-memory database that persists data on disk and operates on a key-value data model. To better understand its internals, we can explore building a similar solution from scratch. This repository provides such an implementation, incorporating some variations.
We will start with a simple Redis-based interface where users can make simple queries to this redis.go database via a built-in redis.go client and go from there.
To run locally:
- Start the app in a terminal:
go run main.go - Once this message appears:
[INFO] Server started on localhost:6379you may need to allow run permissions - A built-in client automatically connects to the server above:
[INFO] Connecting to redis.go server... - Once the client prompt appears:
redis.go>you may execute using Redis commands as shown below
Optional: create a .env file with REDIS_GO_SERVER_PORT=<YOUR PORT> to adjust the port
Commands are based on the official Redis commands. We will note some base commands below and progressively add more functionality.
Get the value of key. If the key does not exist the special value nil is returned.
GET keySet key to hold the string value. If key already holds a value, it is overwritten. Any previous time to live associated with the key is discarded on successful SET operation.
SET key value [NX | XX] [GET] [EX seconds]Where:
NX: set only if key does NOT existXX: set only if key ALREADY existsGET: retrieve last value before update
Removes the specified keys. A key is ignored if it does not exist.
DEL key [key ...]# Run all tests in all subpackages and create a coverage profile
go test ./... -coverprofile=coverage.out
# View a simple coverage summary in the terminal
go tool cover -func=coverage.out
# Open an HTML coverage report in your browser
go tool cover -html=coverage.out