No description
Find a file
2025-01-18 01:08:59 +01:00
.gitlab-ci.yml Add code navigation to CI 2021-11-20 10:39:16 +01:00
.golangci.yml feat: use generics 2025-01-18 01:08:59 +01:00
cache.go feat: use generics 2025-01-18 01:08:59 +01:00
cache_test.go feat: use generics 2025-01-18 01:08:59 +01:00
CHANGELOG.md Add changelog, license and readme 2021-11-20 10:37:11 +01:00
go.mod feat: use generics 2025-01-18 01:08:59 +01:00
go.sum feat: use generics 2025-01-18 01:08:59 +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