skaji / go-cache

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cache

This is a golang in-meomry cache library which prevents cache stampedes.

Usage

import "github.com/skaji/go-cache"

c := cache.New[string, int]()

// we can make sure database.Query with the same key must be called at most once
count, err := c.Compute(context.Background(), "key", func(ctx context.Context, key string) (int, error) {
    rows, err := database.Query(ctx, key)
    if err != nil {
        return 0, err
    }
    return len(rows), nil
})

About

License:MIT License


Languages

Language:Go 100.0%