Chore: update refresh cache

pull/43/head
zijiren233 2 years ago
parent df703e4664
commit b3e498d365

@ -31,7 +31,7 @@ require (
github.com/synctv-org/vendors v0.1.1-0.20231212054308-d2740988c951
github.com/ulule/limiter/v3 v3.11.2
github.com/zencoder/go-dash/v3 v3.0.3
github.com/zijiren233/gencontainer v0.0.0-20231212054144-37b16bf11399
github.com/zijiren233/gencontainer v0.0.0-20231213075414-f7f4c8261dca
github.com/zijiren233/go-colorable v0.0.0-20230930131441-997304c961cb
github.com/zijiren233/livelib v0.2.3-0.20231103145812-58de2ae7f423
github.com/zijiren233/stream v0.5.1

@ -372,6 +372,8 @@ github.com/zijiren233/gencontainer v0.0.0-20231211155903-104d264ea2a6 h1:JHXq5Uh
github.com/zijiren233/gencontainer v0.0.0-20231211155903-104d264ea2a6/go.mod h1:V5oL7PrZxgisuLCblFWd89Jg99O8vM1n58llcxZ2hDY=
github.com/zijiren233/gencontainer v0.0.0-20231212054144-37b16bf11399 h1:3yWNkkPUIbSGXNvbzMu8eSS9apGGBsPvnVQVIIFUEsc=
github.com/zijiren233/gencontainer v0.0.0-20231212054144-37b16bf11399/go.mod h1:V5oL7PrZxgisuLCblFWd89Jg99O8vM1n58llcxZ2hDY=
github.com/zijiren233/gencontainer v0.0.0-20231213075414-f7f4c8261dca h1:mYxvu0DgfUFviM0GRqRIwC+nXBGZNV/WLnndTYGSwPA=
github.com/zijiren233/gencontainer v0.0.0-20231213075414-f7f4c8261dca/go.mod h1:V5oL7PrZxgisuLCblFWd89Jg99O8vM1n58llcxZ2hDY=
github.com/zijiren233/go-colorable v0.0.0-20230930131441-997304c961cb h1:0DyOxf/TbbGodHhOVHNoPk+7v/YBJACs22gKpKlatWw=
github.com/zijiren233/go-colorable v0.0.0-20230930131441-997304c961cb/go.mod h1:6TCzjDiQ8+5gWZiwsC3pnA5M0vUy2jV2Y7ciHJh729g=
github.com/zijiren233/livelib v0.2.3-0.20231103145812-58de2ae7f423 h1:6febr/evRs52lo2lHSpcc6e7+yVPI04ba9eEl26tl+Y=

@ -12,7 +12,7 @@ import (
"github.com/zijiren233/gencontainer/refreshcache"
)
type AlistUserCache = refreshcache.RefreshCache[*AlistUserCacheData]
type AlistUserCache = refreshcache.RefreshCache[*AlistUserCacheData, string]
type AlistUserCacheData struct {
Host string
@ -20,21 +20,17 @@ type AlistUserCacheData struct {
}
func NewAlistCache(userID string) *AlistUserCache {
return refreshcache.NewRefreshCache[*AlistUserCacheData](func(ctx context.Context, args ...any) (*AlistUserCacheData, error) {
return refreshcache.NewRefreshCache[*AlistUserCacheData](func(ctx context.Context, args ...string) (*AlistUserCacheData, error) {
var backend string
if len(args) == 1 {
var ok bool
backend, ok = args[0].(string)
if !ok {
panic("args[0] is not string")
}
backend = args[0]
}
return AlistAuthorizationCacheWithUserIDInitFunc(userID, backend)(ctx)
}, time.Hour*24)
}
func AlistAuthorizationCacheWithConfigInitFunc(host, username, password, backend string) func(ctx context.Context, args ...any) (*AlistUserCacheData, error) {
return func(ctx context.Context, args ...any) (*AlistUserCacheData, error) {
func AlistAuthorizationCacheWithConfigInitFunc(host, username, password, backend string) func(ctx context.Context, args ...string) (*AlistUserCacheData, error) {
return func(ctx context.Context, args ...string) (*AlistUserCacheData, error) {
cli := vendor.AlistClient(backend)
if username == "" {
_, err := cli.Me(ctx, &alist.MeReq{
@ -71,18 +67,18 @@ func AlistAuthorizationCacheWithUserIDInitFunc(userID string, backend string) fu
}
}
type AlistMovieCache = refreshcache.RefreshCache[*AlistMovieCacheData]
type AlistMovieCache = refreshcache.RefreshCache[*AlistMovieCacheData, string]
func NewAlistMovieCache(user *AlistUserCache, movie *model.Movie) *AlistMovieCache {
return refreshcache.NewRefreshCache[*AlistMovieCacheData](NewAlistMovieCacheInitFunc(user, movie), time.Hour)
return refreshcache.NewRefreshCache[*AlistMovieCacheData, string](NewAlistMovieCacheInitFunc(user, movie), time.Hour)
}
type AlistMovieCacheData struct {
URL string
}
func NewAlistMovieCacheInitFunc(user *AlistUserCache, movie *model.Movie) func(ctx context.Context, args ...any) (*AlistMovieCacheData, error) {
return func(ctx context.Context, args ...any) (*AlistMovieCacheData, error) {
func NewAlistMovieCacheInitFunc(user *AlistUserCache, movie *model.Movie) func(ctx context.Context, args ...string) (*AlistMovieCacheData, error) {
return func(ctx context.Context, args ...string) (*AlistMovieCacheData, error) {
aucd, err := user.Get(ctx)
if err != nil {
return nil, err

@ -27,16 +27,16 @@ type BilibiliMpdCache struct {
type BilibiliSubtitleCache map[string]*struct {
Url string
Srt *refreshcache.RefreshCache[[]byte]
Srt *refreshcache.RefreshCache[[]byte, struct{}]
}
func NewBilibiliSharedMpdCacheInitFunc(movie *model.Movie) func(ctx context.Context, args ...any) (*BilibiliMpdCache, error) {
return func(ctx context.Context, args ...any) (*BilibiliMpdCache, error) {
func NewBilibiliSharedMpdCacheInitFunc(movie *model.Movie) func(ctx context.Context, args ...struct{}) (*BilibiliMpdCache, error) {
return func(ctx context.Context, args ...struct{}) (*BilibiliMpdCache, error) {
return BilibiliSharedMpdCacheInitFunc(ctx, movie, args...)
}
}
func BilibiliSharedMpdCacheInitFunc(ctx context.Context, movie *model.Movie, args ...any) (*BilibiliMpdCache, error) {
func BilibiliSharedMpdCacheInitFunc(ctx context.Context, movie *model.Movie, args ...struct{}) (*BilibiliMpdCache, error) {
var cookies []*http.Cookie
vendorInfo, err := db.GetBilibiliVendor(movie.CreatorID)
if err != nil {
@ -127,13 +127,13 @@ func BilibiliSharedMpdCacheInitFunc(ctx context.Context, movie *model.Movie, arg
}, nil
}
func NewBilibiliNoSharedMovieCacheInitFunc(movie *model.Movie) func(ctx context.Context, id string, args ...any) (string, error) {
return func(ctx context.Context, id string, args ...any) (string, error) {
func NewBilibiliNoSharedMovieCacheInitFunc(movie *model.Movie) func(ctx context.Context, id string, args ...struct{}) (string, error) {
return func(ctx context.Context, id string, args ...struct{}) (string, error) {
return BilibiliNoSharedMovieCacheInitFunc(ctx, id, movie, args...)
}
}
func BilibiliNoSharedMovieCacheInitFunc(ctx context.Context, id string, movie *model.Movie, args ...any) (string, error) {
func BilibiliNoSharedMovieCacheInitFunc(ctx context.Context, id string, movie *model.Movie, args ...struct{}) (string, error) {
var cookies []*http.Cookie
vendorInfo, err := db.GetBilibiliVendor(id)
if err != nil {
@ -194,17 +194,17 @@ type bilibiliSubtitleResp struct {
} `json:"body"`
}
func NewBilibiliSubtitleCacheInitFunc(movie *model.Movie) func(ctx context.Context, args ...any) (BilibiliSubtitleCache, error) {
return func(ctx context.Context, args ...any) (BilibiliSubtitleCache, error) {
func NewBilibiliSubtitleCacheInitFunc(movie *model.Movie) func(ctx context.Context, args ...struct{}) (BilibiliSubtitleCache, error) {
return func(ctx context.Context, args ...struct{}) (BilibiliSubtitleCache, error) {
return BilibiliSubtitleCacheInitFunc(ctx, movie)
}
}
func BilibiliSubtitleCacheInitFunc(ctx context.Context, movie *model.Movie, args ...any) (BilibiliSubtitleCache, error) {
func BilibiliSubtitleCacheInitFunc(ctx context.Context, movie *model.Movie, args ...struct{}) (BilibiliSubtitleCache, error) {
return initBilibiliSubtitleCache(ctx, movie, args...)
}
func initBilibiliSubtitleCache(ctx context.Context, movie *model.Movie, args ...any) (BilibiliSubtitleCache, error) {
func initBilibiliSubtitleCache(ctx context.Context, movie *model.Movie, args ...struct{}) (BilibiliSubtitleCache, error) {
biliInfo := movie.Base.VendorInfo.Bilibili
if biliInfo.Bvid == "" || biliInfo.Cid == 0 {
return nil, errors.New("bvid or cid is empty")
@ -232,10 +232,10 @@ func initBilibiliSubtitleCache(ctx context.Context, movie *model.Movie, args ...
for k, v := range resp.Subtitles {
subtitleCache[k] = &struct {
Url string
Srt *refreshcache.RefreshCache[[]byte]
Srt *refreshcache.RefreshCache[[]byte, struct{}]
}{
Url: v,
Srt: refreshcache.NewRefreshCache[[]byte](func(ctx context.Context, args ...any) ([]byte, error) {
Srt: refreshcache.NewRefreshCache[[]byte](func(ctx context.Context, args ...struct{}) ([]byte, error) {
return translateBilibiliSubtitleToSrt(ctx, v)
}, 0),
}
@ -289,15 +289,15 @@ func translateBilibiliSubtitleToSrt(ctx context.Context, url string) ([]byte, er
}
type BilibiliMovieCache struct {
NoSharedMovie *MapCache[string]
SharedMpd *refreshcache.RefreshCache[*BilibiliMpdCache]
Subtitle *refreshcache.RefreshCache[BilibiliSubtitleCache]
NoSharedMovie *MapCache[string, struct{}]
SharedMpd *refreshcache.RefreshCache[*BilibiliMpdCache, struct{}]
Subtitle *refreshcache.RefreshCache[BilibiliSubtitleCache, struct{}]
}
func NewBilibiliMovieCache(movie *model.Movie) *BilibiliMovieCache {
return &BilibiliMovieCache{
NoSharedMovie: newMapCache[string](NewBilibiliNoSharedMovieCacheInitFunc(movie), time.Minute*115),
SharedMpd: refreshcache.NewRefreshCache[*BilibiliMpdCache](NewBilibiliSharedMpdCacheInitFunc(movie), time.Minute*115),
Subtitle: refreshcache.NewRefreshCache[BilibiliSubtitleCache](NewBilibiliSubtitleCacheInitFunc(movie), time.Minute*60),
NoSharedMovie: newMapCache[string, struct{}](NewBilibiliNoSharedMovieCacheInitFunc(movie), time.Minute*115),
SharedMpd: refreshcache.NewRefreshCache[*BilibiliMpdCache, struct{}](NewBilibiliSharedMpdCacheInitFunc(movie), time.Minute*115),
Subtitle: refreshcache.NewRefreshCache[BilibiliSubtitleCache, struct{}](NewBilibiliSubtitleCacheInitFunc(movie), time.Minute*60),
}
}

@ -9,34 +9,34 @@ import (
"golang.org/x/exp/maps"
)
type MapRefreshFunc[T any] func(ctx context.Context, id string, args ...any) (T, error)
type MapRefreshFunc[T any, A any] func(ctx context.Context, id string, args ...A) (T, error)
type MapCache[T any] struct {
type MapCache[T any, A any] struct {
lock sync.RWMutex
cache map[string]*refreshcache.RefreshCache[T]
refreshFunc MapRefreshFunc[T]
cache map[string]*refreshcache.RefreshCache[T, A]
refreshFunc MapRefreshFunc[T, A]
maxAge time.Duration
}
func newMapCache[T any](refreshFunc MapRefreshFunc[T], maxAge time.Duration) *MapCache[T] {
return &MapCache[T]{
cache: make(map[string]*refreshcache.RefreshCache[T]),
func newMapCache[T any, A any](refreshFunc MapRefreshFunc[T, A], maxAge time.Duration) *MapCache[T, A] {
return &MapCache[T, A]{
cache: make(map[string]*refreshcache.RefreshCache[T, A]),
refreshFunc: refreshFunc,
maxAge: maxAge,
}
}
func (b *MapCache[T]) Clear() {
func (b *MapCache[T, A]) Clear() {
b.lock.Lock()
defer b.lock.Unlock()
b.clear()
}
func (b *MapCache[T]) clear() {
func (b *MapCache[T, A]) clear() {
maps.Clear(b.cache)
}
func (b *MapCache[T]) LoadOrStore(ctx context.Context, id string) (T, error) {
func (b *MapCache[T, A]) LoadOrStore(ctx context.Context, id string) (T, error) {
b.lock.RLock()
c, loaded := b.cache[id]
if loaded {
@ -50,7 +50,7 @@ func (b *MapCache[T]) LoadOrStore(ctx context.Context, id string) (T, error) {
b.lock.Unlock()
return c.Get(ctx)
}
c = refreshcache.NewRefreshCache[T](func(ctx context.Context, args ...any) (T, error) {
c = refreshcache.NewRefreshCache[T, A](func(ctx context.Context, args ...A) (T, error) {
return b.refreshFunc(ctx, id, args...)
}, b.maxAge)
b.cache[id] = c
@ -58,7 +58,7 @@ func (b *MapCache[T]) LoadOrStore(ctx context.Context, id string) (T, error) {
return c.Get(ctx)
}
func (b *MapCache[T]) StoreOrRefresh(ctx context.Context, id string) (T, error) {
func (b *MapCache[T, A]) StoreOrRefresh(ctx context.Context, id string) (T, error) {
b.lock.RLock()
c, ok := b.cache[id]
if ok {
@ -72,7 +72,7 @@ func (b *MapCache[T]) StoreOrRefresh(ctx context.Context, id string) (T, error)
b.lock.Unlock()
return c.Refresh(ctx)
}
c = refreshcache.NewRefreshCache[T](func(ctx context.Context, args ...any) (T, error) {
c = refreshcache.NewRefreshCache[T, A](func(ctx context.Context, args ...A) (T, error) {
return b.refreshFunc(ctx, id, args...)
}, b.maxAge)
b.cache[id] = c
@ -80,12 +80,12 @@ func (b *MapCache[T]) StoreOrRefresh(ctx context.Context, id string) (T, error)
return c.Refresh(ctx)
}
func (b *MapCache[T]) LoadOrStoreWithDynamicFunc(ctx context.Context, id string, refreshFunc MapRefreshFunc[T]) (T, error) {
func (b *MapCache[T, A]) LoadOrStoreWithDynamicFunc(ctx context.Context, id string, refreshFunc MapRefreshFunc[T, A]) (T, error) {
b.lock.RLock()
c, loaded := b.cache[id]
if loaded {
b.lock.RUnlock()
return c.Data().Get(ctx, func(ctx context.Context, args ...any) (T, error) {
return c.Data().Get(ctx, func(ctx context.Context, args ...A) (T, error) {
return refreshFunc(ctx, id, args...)
})
}
@ -94,26 +94,26 @@ func (b *MapCache[T]) LoadOrStoreWithDynamicFunc(ctx context.Context, id string,
c, loaded = b.cache[id]
if loaded {
b.lock.Unlock()
return c.Data().Get(ctx, func(ctx context.Context, args ...any) (T, error) {
return c.Data().Get(ctx, func(ctx context.Context, args ...A) (T, error) {
return refreshFunc(ctx, id, args...)
})
}
c = refreshcache.NewRefreshCache[T](func(ctx context.Context, args ...any) (T, error) {
c = refreshcache.NewRefreshCache[T, A](func(ctx context.Context, args ...A) (T, error) {
return b.refreshFunc(ctx, id, args...)
}, b.maxAge)
b.cache[id] = c
b.lock.Unlock()
return c.Data().Get(ctx, func(ctx context.Context, args ...any) (T, error) {
return c.Data().Get(ctx, func(ctx context.Context, args ...A) (T, error) {
return refreshFunc(ctx, id, args...)
})
}
func (b *MapCache[T]) StoreOrRefreshWithDynamicFunc(ctx context.Context, id string, refreshFunc MapRefreshFunc[T]) (T, error) {
func (b *MapCache[T, A]) StoreOrRefreshWithDynamicFunc(ctx context.Context, id string, refreshFunc MapRefreshFunc[T, A]) (T, error) {
b.lock.RLock()
c, ok := b.cache[id]
if ok {
b.lock.RUnlock()
return c.Data().Refresh(ctx, func(ctx context.Context, args ...any) (T, error) {
return c.Data().Refresh(ctx, func(ctx context.Context, args ...A) (T, error) {
return refreshFunc(ctx, id, args...)
})
}
@ -122,16 +122,16 @@ func (b *MapCache[T]) StoreOrRefreshWithDynamicFunc(ctx context.Context, id stri
c, ok = b.cache[id]
if ok {
b.lock.Unlock()
return c.Data().Refresh(ctx, func(ctx context.Context, args ...any) (T, error) {
return c.Data().Refresh(ctx, func(ctx context.Context, args ...A) (T, error) {
return refreshFunc(ctx, id, args...)
})
}
c = refreshcache.NewRefreshCache[T](func(ctx context.Context, args ...any) (T, error) {
c = refreshcache.NewRefreshCache[T, A](func(ctx context.Context, args ...A) (T, error) {
return b.refreshFunc(ctx, id, args...)
}, b.maxAge)
b.cache[id] = c
b.lock.Unlock()
return c.Data().Refresh(ctx, func(ctx context.Context, args ...any) (T, error) {
return c.Data().Refresh(ctx, func(ctx context.Context, args ...A) (T, error) {
return refreshFunc(ctx, id, args...)
})
}

@ -52,7 +52,7 @@ func Login(ctx *gin.Context) {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
return
}
_, err = user.AlistCache().Data().Refresh(ctx, func(ctx context.Context, args ...any) (*cache.AlistUserCacheData, error) {
_, err = user.AlistCache().Data().Refresh(ctx, func(ctx context.Context, args ...string) (*cache.AlistUserCacheData, error) {
return &cache.AlistUserCacheData{
Host: req.Host,
}, nil
@ -72,7 +72,7 @@ func Login(ctx *gin.Context) {
return
}
_, err = user.AlistCache().Data().Refresh(ctx, func(ctx context.Context, args ...any) (*cache.AlistUserCacheData, error) {
_, err = user.AlistCache().Data().Refresh(ctx, func(ctx context.Context, args ...string) (*cache.AlistUserCacheData, error) {
return &cache.AlistUserCacheData{
Host: req.Host,
Token: resp.Token,

Loading…
Cancel
Save