cache

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 29, 2024 License: MIT Imports: 12 Imported by: 1

README

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.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var VERSION = "0.2.0" + commit

VERSION is the version of this library

Functions

This section is empty.

Types

type Cache

type Cache[T interface{}] struct {
	Name       string
	Items      sync.Map
	Expiration time.Duration
	// contains filtered or unexported fields
}

Cache is a cache

func New

func New[T any](name string, option ...CacheOption) *Cache[T]

New creates a new Cache

func (*Cache[T]) Clear

func (cache *Cache[T]) Clear() error

Clear clears the cache

func (*Cache[T]) Get

func (cache *Cache[T]) Get(key string) (*T, error)

Get gets an item from the cache

func (*Cache[T]) Set

func (cache *Cache[T]) Set(item T, key ...string) (err error)

Set sets an item in the cache

func (*Cache[T]) SetWithExpiration

func (cache *Cache[T]) SetWithExpiration(item T, expiration time.Duration, key ...string) (err error)

SetWithExpiration sets an item in the cache with a custom expiration

func (*Cache[T]) WithEncryptionKey

func (cache *Cache[T]) WithEncryptionKey(key []byte) *Cache[T]

WithEncryptionKey sets the encryption key for the cache

func (*Cache[T]) WithExpiration

func (cache *Cache[T]) WithExpiration(expiration time.Duration) *Cache[T]

WithExpiration sets the expiration time for the cache

type CacheOption

type CacheOption int
const (
	// CacheOptionNone is the default option
	CacheOptionNone CacheOption = iota
	// CacheOptionPersistent tells the cache to persist the data
	CacheOptionPersistent
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL