Skip to content

n-r-w/sfcache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Reference Go Coverage CI Status Stability Go Report

sfcache

Singleflight group with internal LRU cache

Usage

Singleflight is a concurrency method to prevent duplicate work from being executed due to multiple calls for the same resource. The internal LRU cache is used to store the result of the function call and bypass the singleflight call if the result is already in the cache.

package main

import (
    "context"
    "log"
    "time"

    "github.com/n-r-w/sfcache"
)

func main() {
    const (
    // The number of elements in the cache
    cacheSize = 100
    // TTL for the cache
    cacheTTL = 10 * time.Minute
    useCache = true
    )

    // evictCallback can be nil if you don't want to do anything when an item is evicted
    cache := sfcache.New[int, string](cacheSize, evictCallback, cacheTTL)

    // make come call
    key := 1
    val, shared, err := cache.Do(context.Background(), key, useCache, func(ctx context.Context) (string, error) {
        return worker(ctx, key)
    })
    if err != nil {
        panic(err)
    }
    log.Println("Value:", val, "Shared:", shared)

    // make some other call with the same key. This time it should be cached and worker should not be called.
    val, shared, err = cache.Do(context.Background(), key, useCache, func(ctx context.Context) (string, error) {
        return worker(ctx, key)
    })
    if err != nil {
        panic(err)
    }
    log.Println("Value:", val, "Shared:", shared)
}

func evictCallback(key int, value string) {
    log.Println("Evicting", key, value)
}

func worker(_ context.Context, key int) (string, error) {
    log.Println("Doing some work for", key)
    return "value", nil
}