Skip to content

Commit

Permalink
feat(cache): make TTL configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyXiang committed Nov 16, 2022
1 parent 73711ac commit 614a3fc
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ env CGO_ENABLED=0 go install -trimpath -ldflags="-s -w" github.com/RoyXiang/plex
* Set it if you run an instance of [Plaxt](https://github.com/XanderStrike/goplaxt)
* Or, you can set it to [the official one](https://plaxt.astandke.com/)
- `PLEX_TOKEN` (Optional, if you need it, see [here](https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/))
- `STATIC_CACHE_TTL` (Optional, default: `1h`, which controls the cache TTL of static files)
- `DYNAMIC_CACHE_TTL` (Optional, default: `5s`, which controls the cache TTL of dynamic requests)
- `REDIRECT_WEB_APP` (Optional, default: `true`)
- `DISABLE_TRANSCODE` (Optional, default: `true`)
- `NO_REQUEST_LOGS` (Optional, default: `false`)
Expand Down
7 changes: 0 additions & 7 deletions handler/const.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package handler

import (
"time"
)

const (
headerPlexPrefix = "X-Plex-"
headerCacheStatus = "X-Plex-Cache-Status"
Expand All @@ -24,9 +20,6 @@ const (
cachePrefixStatic = "static"
cachePrefixPlex = "plex"

cacheTtlDynamic = time.Second * 5
cacheTtlStatic = time.Hour

contentTypeAny = "*/*"
contentTypeXml = "xml"

Expand Down
2 changes: 2 additions & 0 deletions handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func init() {
BaseUrl: os.Getenv("PLEX_BASEURL"),
Token: os.Getenv("PLEX_TOKEN"),
PlaxtUrl: os.Getenv("PLAXT_URL"),
StaticCacheTtl: os.Getenv("STATIC_CACHE_TTL"),
DynamicCacheTtl: os.Getenv("DYNAMIC_CACHE_TTL"),
RedirectWebApp: os.Getenv("REDIRECT_WEB_APP"),
DisableTranscode: os.Getenv("DISABLE_TRANSCODE"),
NoRequestLogs: os.Getenv("NO_REQUEST_LOGS"),
Expand Down
6 changes: 3 additions & 3 deletions handler/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func staticMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), cacheInfoCtxKey, &cacheInfo{
Prefix: cachePrefixStatic,
Ttl: cacheTtlStatic,
Ttl: plexClient.staticCacheTtl,
})
r = r.WithContext(ctx)
cacheMiddleware(next).ServeHTTP(w, r)
Expand All @@ -125,7 +125,7 @@ func dynamicMiddleware(next http.Handler) http.Handler {
case ".css", ".ico", ".jpeg", ".jpg", ".webp":
ctx = context.WithValue(r.Context(), cacheInfoCtxKey, &cacheInfo{
Prefix: cachePrefixStatic,
Ttl: cacheTtlStatic,
Ttl: plexClient.staticCacheTtl,
})
case ".m3u8", ".ts":
ctx = r.Context()
Expand All @@ -136,7 +136,7 @@ func dynamicMiddleware(next http.Handler) http.Handler {
}
ctx = context.WithValue(r.Context(), cacheInfoCtxKey, &cacheInfo{
Prefix: cachePrefixDynamic,
Ttl: cacheTtlDynamic,
Ttl: plexClient.dynamicCacheTtl,
})
}
cacheMiddleware(next).ServeHTTP(w, r.WithContext(ctx))
Expand Down
17 changes: 17 additions & 0 deletions handler/plex.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net/url"
"strconv"
"strings"
"time"

"github.com/RoyXiang/plexproxy/common"
"github.com/jrudio/go-plex-client"
Expand All @@ -23,6 +24,8 @@ type PlexConfig struct {
BaseUrl string
Token string
PlaxtUrl string
StaticCacheTtl string
DynamicCacheTtl string
RedirectWebApp string
DisableTranscode string
NoRequestLogs string
Expand All @@ -32,6 +35,9 @@ type PlexClient struct {
proxy *httputil.ReverseProxy
client *plex.Plex

staticCacheTtl time.Duration
dynamicCacheTtl time.Duration

plaxtUrl string
redirectWebApp bool
disableTranscode bool
Expand Down Expand Up @@ -72,6 +78,15 @@ func NewPlexClient(config PlexConfig) *PlexClient {
plaxtUrl = u.String()
}

staticCacheTtl, err := time.ParseDuration(config.StaticCacheTtl)
if err != nil {
staticCacheTtl = time.Hour
}
dynamicCacheTtl, err := time.ParseDuration(config.DynamicCacheTtl)
if err != nil {
dynamicCacheTtl = time.Second * 5
}

var redirectWebApp, disableTranscode, noRequestLogs bool
if b, err := strconv.ParseBool(config.RedirectWebApp); err == nil {
redirectWebApp = b
Expand All @@ -93,6 +108,8 @@ func NewPlexClient(config PlexConfig) *PlexClient {
proxy: proxy,
client: client,
plaxtUrl: plaxtUrl,
staticCacheTtl: staticCacheTtl,
dynamicCacheTtl: dynamicCacheTtl,
redirectWebApp: redirectWebApp,
disableTranscode: disableTranscode,
NoRequestLogs: noRequestLogs,
Expand Down

0 comments on commit 614a3fc

Please sign in to comment.