1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-12 02:30:53 +09:00
anytype-heart/util/linkpreview/cache.go
AnastasiaShemyakinskaya 1b385fc288
GO-3031: fix comment
Signed-off-by: AnastasiaShemyakinskaya <shem98a@mail.ru>
2024-05-31 16:08:43 +02:00

45 lines
830 B
Go

package linkpreview
import (
"context"
"github.com/anyproto/any-sync/app"
"github.com/golang/groupcache/lru"
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
)
const (
maxCacheEntries = 100
)
func NewWithCache() LinkPreview {
return &cache{}
}
type cache struct {
lp LinkPreview
cache *lru.Cache
}
func (c *cache) Init(_ *app.App) (err error) {
c.lp = New()
c.cache = lru.New(maxCacheEntries)
return
}
func (c *cache) Name() string {
return CName
}
func (c *cache) Fetch(ctx context.Context, url string) (linkPreview model.LinkPreview, responseBody []byte, isFile bool, err error) {
if res, ok := c.cache.Get(url); ok {
return res.(model.LinkPreview), nil, false, nil
}
linkPreview, responseBody, _, err = c.lp.Fetch(ctx, url)
if err != nil {
return
}
c.cache.Add(url, linkPreview)
return
}