⛷️ In-memory key/value cache server over HTTP with no dependencies.
MIT License
My blog post: Cloning Memcached with Go
An in-memory key/value cache server over HTTP with no dependencies.
Keys and values are strings. Integer math can be applied in some situations (like Memcached does).
The caching method is Least Recently Used (LRU).
go get healeycodes/in-memory-cache-over-http
production
to turn off logging.# Linux/macOS
export PORT=8000
export APP_ENV=production
# Command Prompt
set PORT=8000
set APP_ENV=production
# PowerShell
$env:PORT = "8000"
$env:APP_ENV = "production"
go run .\main.go
go build
Adding an expire parameter is always optional. Not setting it, or setting it to zero means that the key will not expire. It uses Unix time.
Example usage.
Set name
to be Andrew
with an expire time of 01/01/2030 @ 12:00am (UTC)
GET localhost:8000/set?key=name&value=Andrew&expire=1893456000
(204 status code)
Retrieve the value located at name
.
GET localhost:8000/get?key=name
(200 status code, body: Andrew
)
/set
Set a key/value. Existing will be overwritten.
/get
Get a value from a key.
/delete
Delete a key.
/checkandset
Set a key/value if the current value at that key matches the compare.
If no existing key, set the key/value.
/increment
Increment a value. Both the existing value and the new value amount should be integers.
If no existing key, set the key/value.
/decrement
Decrement a value. Both the existing value and the new value amount should be integers.
If no existing key, set the key/value.
/append
Concatenates the new value onto the existing.
If no existing key, set the key/value.
/prepend
Concatenates the existing value onto the new value.
If no existing key, set the key/value.
/flush
Clear the cache. Delete all keys and values.
/stats
Return statistics about the cache.
Example.
{
"keyCount": 1,
"maxSize": 0
}
The majority of tests are integration tests that test API routes while checking the underlying cache.
There are some unit tests for the cache.
Run tests recursively.
go test ./...
Example output.
? healeycodes/in-memory-cache-over-http [no test files]
ok healeycodes/in-memory-cache-over-http/api 0.527s
ok healeycodes/in-memory-cache-over-http/cache 0.340s
Feel free to raise any issues and pull requests
There is no road map for this project. My main motivations were to learn more about Go!