Skip to content

gildas/go-cache

Repository files navigation

go-cache

GoVersion GoDoc License Report

master Test codecov

dev Test codecov

a disk & memory Cache for stuff

Installation

go get github.com/gildas/go-cache

Usage

In the simplest form, you can use the cache like this:

cache := cache.New[User]("mycache")
err := cache.Set(user)
err := cache.Set(user, "mykey1", "mykey2", "mykey3")
...
value, err := cache.Get("key")

On top of the provided keys in the Set method, the cache will check if the User struct implements the following interfaces to use as keys (all that apply):

Note: The keys are case-sensitive.

If the User is not found in the cache, the Get method will return an error of type errors.NotFound.

You can also set the cache-wide expiration time:

cache := cache.New[User("mycache").WithExpiration(10 * time.Minute)

Or set the expiration time for a specific key:

cache := cache.New[User]("mycache")
err := cache.SetWithExpiration(user, 10 * time.Minute)

If the User is expired, the Get method will return an error of type errors.NotFound.

The cache can be persisted to disk:

cache := cache.New[User]("mycache", cache.CacheOptionPersistent)

The cache files are stored in the os.UserCacheDir directory, in a subdirectory named after the cache name.

The cache can be encrypted:

cache := cache.New[User]("mycache", cache.CacheOptionPersistent).WithEncryption("mysecret")
cache := cache.New[User]("mycache").WithEncryption("mysecret")

Setting the encryption key turns on the persistent option automatically.

The encryption key must follow the crypto/aes requirements, otherwise the cache will return an error when trying to read or write data.

About

a disk & memory Cache for stuff

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages