52 lines
879 B
Markdown
52 lines
879 B
Markdown
|
# go/cache
|
||
|
|
||
|
Cache provides an in-memory key-value store used to cache arbitrary data.
|
||
|
|
||
|
It supports entry expiration.
|
||
|
|
||
|
## Quickstart
|
||
|
|
||
|
You can add `cache` to your project using `go get`:
|
||
|
|
||
|
go get code.bcarlin.xyz/go/cache
|
||
|
|
||
|
You can then import it in your code:
|
||
|
|
||
|
import "code.bcarlin.xyz/go/cache"
|
||
|
|
||
|
## Tests
|
||
|
|
||
|
To run the tests, run the command:
|
||
|
|
||
|
go test
|
||
|
|
||
|
## Usage
|
||
|
|
||
|
The API is very light and straight forward:
|
||
|
|
||
|
```go
|
||
|
// Initialize a cache
|
||
|
c := cache.New()
|
||
|
|
||
|
// Store some values. Keys must be strings.
|
||
|
c.Put("foo", "my data")
|
||
|
c.Put("the-answer", 42)
|
||
|
|
||
|
// Store some more data with an ewpiration date
|
||
|
c.PutTTL("not-the-answer", 144, 1*time.Hour)
|
||
|
|
||
|
// Retrieve some data
|
||
|
val, ok := c.Get("the-answer")
|
||
|
if !ok {
|
||
|
fmt.Println("cache missed")
|
||
|
}
|
||
|
|
||
|
// Delete entries
|
||
|
c.Del("foo")
|
||
|
c.Del("does-not-exist") // does not fail
|
||
|
```
|
||
|
|
||
|
## License
|
||
|
|
||
|
[MIT](https://choosealicense.com/licenses/mit/)
|