1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-10 01:51:07 +09:00
anytype-heart/util/linkpreview/cache.go
Sergey Cherepanov e558b8439a
bookmark block
2020-02-14 11:48:45 +03:00

37 lines
621 B
Go

package linkpreview
import (
"context"
"github.com/anytypeio/go-anytype-library/pb/model"
"github.com/hashicorp/golang-lru"
)
const (
maxCacheEntries = 100
)
func NewWithCache() LinkPreview {
lruCache, _ := lru.New(maxCacheEntries)
return &cache{
lp: New(),
cache: lruCache,
}
}
type cache struct {
lp LinkPreview
cache *lru.Cache
}
func (c *cache) Fetch(ctx context.Context, url string) (lp model.LinkPreview, err error) {
if res, ok := c.cache.Get(url); ok {
return res.(model.LinkPreview), nil
}
lp, err = c.lp.Fetch(ctx, url)
if err != nil {
return
}
c.cache.Add(url, lp)
return
}