No description
Find a file
2021-11-20 10:37:11 +01:00
.gitlab-ci.yml Add CI configuration 2021-11-20 10:37:02 +01:00
.golangci.yml initial commit 2021-11-19 13:22:01 +01:00
cache.go initial commit 2021-11-19 13:22:01 +01:00
cache_test.go initial commit 2021-11-19 13:22:01 +01:00
CHANGELOG.md Add changelog, license and readme 2021-11-20 10:37:11 +01:00
go.mod initial commit 2021-11-19 13:22:01 +01:00
LICENSE Add changelog, license and readme 2021-11-20 10:37:11 +01:00
README.md Add changelog, license and readme 2021-11-20 10:37:11 +01:00

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:

// 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