gcache

package module
v0.0.0-...-80a70c4 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2024 License: MIT Imports: 2 Imported by: 0

README

Generic Cache for Golang

This is a simple cache implementation in Golang. It is a generic cache and can be used to store any type of data.

package gcache

import (
	"testing"

	"github.com/carlmjohnson/be"
)

type X struct {
	Time int64
}

func TestCache(t *testing.T) {

	key := "thekey"
	x, err := Cache(key, func() (ret X, err error) {
		return getSomething(10)
	})
	if err != nil {
		t.Errorf("Error: %v", err)
	}

	y, err := Cache(key, func() (ret X, err error) {
		return getSomething(20)
	})

	if err != nil {
		t.Errorf("Error: %v", err)
	}

	be.Equal(t, x.Time, y.Time)

}

func getSomething(x int64) (resp X, err error) {
	resp.Time = x
	return
}

As you can see, x and y have equal values despite having different parameters. This is because they share the same key.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cache

func Cache[T any](key string, function func() (ret T, err error)) (ret T, err error)

func SetDefaultCache

func SetDefaultCache(duration time.Duration)

Types

This section is empty.

Jump to

Keyboard shortcuts

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