The smartcache realizes smart in memory cache generation to minimize process blocks by using soft expire limit
var (
expire = 5*time.Minute
softExpire = 1*time.Minute
)
ca := smartcache.New(expire, softExpire, func(ctx context.Context) (interface{}, error) {
val, err := genCache(ctx)
return val, err
})
val, err := ca.Get(context.Background())
The smartcache is an in-memory cache library with avoiding the following problems.
- thundering herd
- block processing when regenerating
- etc.
To avoid the above problems, you can set a soft expire limit to Cache. The soft expired cached value is internally pre-warmed by a single goroutine and the value is replaced seamlessly.
% go get github.com/Songmu/smartcache