sfcache

package module
v0.0.0-...-25464de Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2024 License: MIT Imports: 3 Imported by: 0

README

sfcache

singleflight + in memory cache

Example Usage

import (
	"github.com/abcang/sfcache"
)

type Hoge struct {
	ID   int64
	Name string
}

var itemSize = 100
var ttl = 30 * time.Second
var hogeSfc = sfcache.New[*Hoge](itemSize, ttl)

func getHogeWithCache(id int64) (*Hoge, error) {
	key := strconv.Itoa(int(id))
	return hogeSfc.Do(key, func() (*Hoge, error) {
		return getHogeCore(id)
	})
}

func getHogeCore(id int64) (*Hoge, error) {
	return &Hoge{
		ID:   id,
		Name: "hoge",
	}, nil
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Sfcache

type Sfcache[T any] struct {
	// contains filtered or unexported fields
}

func New

func New[T any](itemSize int, ttl time.Duration) *Sfcache[T]

func (Sfcache[T]) Clear

func (sfc Sfcache[T]) Clear()

func (Sfcache[T]) Delete

func (sfc Sfcache[T]) Delete(key string)

func (Sfcache[T]) Do

func (sfc Sfcache[T]) Do(key string, fn func() (T, error)) (T, error)

Jump to

Keyboard shortcuts

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