Skip to content

Commit

Permalink
make cache optional
Browse files Browse the repository at this point in the history
  • Loading branch information
vintikzzz committed Dec 29, 2023
1 parent 7039a3d commit 2085d47
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 44 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/webtor-io/rest-api

go 1.18
go 1.21

require (
code.cloudfoundry.org/bytefmt v0.0.0-20211005130812-5bb3c17173e5 // indirect
Expand Down
134 changes: 91 additions & 43 deletions services/url_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,32 @@ import (
"github.com/urfave/cli"
)

const (
usePieceCache = "use-piece-cache"
useTranscodeCache = "use-transcode-cache"
useTranscodeMultibitrateCache = "use-transcode-multibitrate-cache"
)

func RegisterUrlBuilderFlags(f []cli.Flag) []cli.Flag {
return append(f,
cli.BoolFlag{
Name: usePieceCache,
Usage: "use piece cache",
EnvVar: "USE_PIECE_CACHE",
},
cli.BoolFlag{
Name: useTranscodeCache,
Usage: "use transcode cache",
EnvVar: "USE_TRANSCODE_CACHE",
},
cli.BoolFlag{
Name: useTranscodeMultibitrateCache,
Usage: "use transcode multibitrate cache",
EnvVar: "USE_TRANSCODE_MULTIBITRATE_CACHE",
},
)
}

type MyURL struct {
url.URL
cached bool
Expand All @@ -25,32 +51,41 @@ func (s *MyURL) BuildExportMeta() *ExportMeta {
}

type URLBuilder struct {
cpm *CompletedPiecesMap
tdm *TranscodeDoneMap
sd *Subdomains
domain string
ssl bool
cpm *CompletedPiecesMap
tdm *TranscodeDoneMap
sd *Subdomains
domain string
ssl bool
usePieceCache bool
useTranscodeCache bool
useTranscodeMultibitrateCache bool
}

func NewURLBuilder(c *cli.Context, cpm *CompletedPiecesMap, tdm *TranscodeDoneMap, sd *Subdomains) *URLBuilder {
return &URLBuilder{
cpm: cpm,
tdm: tdm,
sd: sd,
domain: c.String(exportDomainFlag),
ssl: c.BoolT(exportSSLFlag),
cpm: cpm,
tdm: tdm,
sd: sd,
domain: c.String(exportDomainFlag),
ssl: c.BoolT(exportSSLFlag),
usePieceCache: c.Bool(usePieceCache),
useTranscodeCache: c.Bool(useTranscodeCache),
useTranscodeMultibitrateCache: c.Bool(useTranscodeMultibitrateCache),
}
}

func (s *URLBuilder) Build(r *Resource, i *ListItem, g ParamGetter, et ExportType) (*MyURL, error) {
bubc := BaseURLBuilder{
cpm: s.cpm,
sd: s.sd,
r: r,
i: i,
g: g,
domain: s.domain,
ssl: s.ssl,
cpm: s.cpm,
sd: s.sd,
r: r,
i: i,
g: g,
domain: s.domain,
ssl: s.ssl,
usePieceCache: s.usePieceCache,
useTranscodeCache: s.useTranscodeCache,
useTranscodeMultibitrateCache: s.useTranscodeMultibitrateCache,
}
switch et {
case ExportTypeDownload:
Expand Down Expand Up @@ -87,13 +122,16 @@ func (s *URLBuilder) Build(r *Resource, i *ListItem, g ParamGetter, et ExportTyp
}

type BaseURLBuilder struct {
cpm *CompletedPiecesMap
sd *Subdomains
r *Resource
i *ListItem
g ParamGetter
domain string
ssl bool
cpm *CompletedPiecesMap
sd *Subdomains
r *Resource
i *ListItem
g ParamGetter
domain string
ssl bool
usePieceCache bool
useTranscodeCache bool
useTranscodeMultibitrateCache bool
}

type DownloadURLBuilder struct {
Expand Down Expand Up @@ -140,17 +178,19 @@ func (s *BaseURLBuilder) BuildBaseURL(i *MyURL) (u *MyURL, err error) {
} else {
u.Path = "/" + s.r.ID + "/" + strings.Trim(s.i.PathStr, "/")
}
pieces, err := s.cpm.Get(s.r.ID)
if err != nil {
return nil, err
}

cached := false
if len(pieces) > 0 {
for _, f := range s.r.Files {
if pathBeginsWith(f.Path, s.i.Path) && pieces.HasAny(f.Pieces) {
cached = true
break
if s.usePieceCache {
pieces, err := s.cpm.Get(s.r.ID)
if err != nil {
return nil, err
}

if len(pieces) > 0 {
for _, f := range s.r.Files {
if pathBeginsWith(f.Path, s.i.Path) && pieces.HasAny(f.Pieces) {
cached = true
break
}
}
}
}
Expand Down Expand Up @@ -313,13 +353,18 @@ func (s *StreamURLBuilder) BuildVODURL(i *MyURL, suffix string) (u *MyURL) {

func (s *StreamURLBuilder) BuildVideoStreamURL(i *MyURL, suffix string) (u *MyURL, err error) {
u = i
u, ok, err := s.BuildTranscodeCacheURL(u, true, suffix)
if err != nil || ok {
return
ok := false
if s.useTranscodeMultibitrateCache {
u, ok, err = s.BuildTranscodeCacheURL(u, true, suffix)
if err != nil || ok {
return
}
}
u, ok, err = s.BuildTranscodeCacheURL(u, false, suffix)
if err != nil || ok {
return
if s.useTranscodeCache {
u, ok, err = s.BuildTranscodeCacheURL(u, false, suffix)
if err != nil || ok {
return
}
}
if shouldTranscode(s.i.Ext) {
u = s.BuildTranscodeURL(i, suffix)
Expand All @@ -332,9 +377,12 @@ func (s *StreamURLBuilder) BuildVideoStreamURL(i *MyURL, suffix string) (u *MyUR

func (s *StreamURLBuilder) BuildAudioStreamURL(i *MyURL, suffix string) (u *MyURL, err error) {
u = i
u, ok, err := s.BuildTranscodeCacheURL(u, false, suffix)
if err != nil || ok {
return
ok := false
if s.useTranscodeCache {
u, ok, err = s.BuildTranscodeCacheURL(u, false, suffix)
if err != nil || ok {
return
}
}
if shouldTranscode(s.i.Ext) {
u = s.BuildTranscodeURL(i, suffix)
Expand Down

0 comments on commit 2085d47

Please sign in to comment.