Skip to content

Commit 614a3fc

Browse files
committed
feat(cache): make TTL configurable
1 parent 73711ac commit 614a3fc

5 files changed

Lines changed: 24 additions & 10 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ env CGO_ENABLED=0 go install -trimpath -ldflags="-s -w" github.com/RoyXiang/plex
3535
* Set it if you run an instance of [Plaxt](https://github.com/XanderStrike/goplaxt)
3636
* Or, you can set it to [the official one](https://plaxt.astandke.com/)
3737
- `PLEX_TOKEN` (Optional, if you need it, see [here](https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/))
38+
- `STATIC_CACHE_TTL` (Optional, default: `1h`, which controls the cache TTL of static files)
39+
- `DYNAMIC_CACHE_TTL` (Optional, default: `5s`, which controls the cache TTL of dynamic requests)
3840
- `REDIRECT_WEB_APP` (Optional, default: `true`)
3941
- `DISABLE_TRANSCODE` (Optional, default: `true`)
4042
- `NO_REQUEST_LOGS` (Optional, default: `false`)

handler/const.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package handler
22

3-
import (
4-
"time"
5-
)
6-
73
const (
84
headerPlexPrefix = "X-Plex-"
95
headerCacheStatus = "X-Plex-Cache-Status"
@@ -24,9 +20,6 @@ const (
2420
cachePrefixStatic = "static"
2521
cachePrefixPlex = "plex"
2622

27-
cacheTtlDynamic = time.Second * 5
28-
cacheTtlStatic = time.Hour
29-
3023
contentTypeAny = "*/*"
3124
contentTypeXml = "xml"
3225

handler/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ func init() {
2323
BaseUrl: os.Getenv("PLEX_BASEURL"),
2424
Token: os.Getenv("PLEX_TOKEN"),
2525
PlaxtUrl: os.Getenv("PLAXT_URL"),
26+
StaticCacheTtl: os.Getenv("STATIC_CACHE_TTL"),
27+
DynamicCacheTtl: os.Getenv("DYNAMIC_CACHE_TTL"),
2628
RedirectWebApp: os.Getenv("REDIRECT_WEB_APP"),
2729
DisableTranscode: os.Getenv("DISABLE_TRANSCODE"),
2830
NoRequestLogs: os.Getenv("NO_REQUEST_LOGS"),

handler/middleware.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func staticMiddleware(next http.Handler) http.Handler {
111111
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
112112
ctx := context.WithValue(r.Context(), cacheInfoCtxKey, &cacheInfo{
113113
Prefix: cachePrefixStatic,
114-
Ttl: cacheTtlStatic,
114+
Ttl: plexClient.staticCacheTtl,
115115
})
116116
r = r.WithContext(ctx)
117117
cacheMiddleware(next).ServeHTTP(w, r)
@@ -125,7 +125,7 @@ func dynamicMiddleware(next http.Handler) http.Handler {
125125
case ".css", ".ico", ".jpeg", ".jpg", ".webp":
126126
ctx = context.WithValue(r.Context(), cacheInfoCtxKey, &cacheInfo{
127127
Prefix: cachePrefixStatic,
128-
Ttl: cacheTtlStatic,
128+
Ttl: plexClient.staticCacheTtl,
129129
})
130130
case ".m3u8", ".ts":
131131
ctx = r.Context()
@@ -136,7 +136,7 @@ func dynamicMiddleware(next http.Handler) http.Handler {
136136
}
137137
ctx = context.WithValue(r.Context(), cacheInfoCtxKey, &cacheInfo{
138138
Prefix: cachePrefixDynamic,
139-
Ttl: cacheTtlDynamic,
139+
Ttl: plexClient.dynamicCacheTtl,
140140
})
141141
}
142142
cacheMiddleware(next).ServeHTTP(w, r.WithContext(ctx))

handler/plex.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"net/url"
1414
"strconv"
1515
"strings"
16+
"time"
1617

1718
"github.com/RoyXiang/plexproxy/common"
1819
"github.com/jrudio/go-plex-client"
@@ -23,6 +24,8 @@ type PlexConfig struct {
2324
BaseUrl string
2425
Token string
2526
PlaxtUrl string
27+
StaticCacheTtl string
28+
DynamicCacheTtl string
2629
RedirectWebApp string
2730
DisableTranscode string
2831
NoRequestLogs string
@@ -32,6 +35,9 @@ type PlexClient struct {
3235
proxy *httputil.ReverseProxy
3336
client *plex.Plex
3437

38+
staticCacheTtl time.Duration
39+
dynamicCacheTtl time.Duration
40+
3541
plaxtUrl string
3642
redirectWebApp bool
3743
disableTranscode bool
@@ -72,6 +78,15 @@ func NewPlexClient(config PlexConfig) *PlexClient {
7278
plaxtUrl = u.String()
7379
}
7480

81+
staticCacheTtl, err := time.ParseDuration(config.StaticCacheTtl)
82+
if err != nil {
83+
staticCacheTtl = time.Hour
84+
}
85+
dynamicCacheTtl, err := time.ParseDuration(config.DynamicCacheTtl)
86+
if err != nil {
87+
dynamicCacheTtl = time.Second * 5
88+
}
89+
7590
var redirectWebApp, disableTranscode, noRequestLogs bool
7691
if b, err := strconv.ParseBool(config.RedirectWebApp); err == nil {
7792
redirectWebApp = b
@@ -93,6 +108,8 @@ func NewPlexClient(config PlexConfig) *PlexClient {
93108
proxy: proxy,
94109
client: client,
95110
plaxtUrl: plaxtUrl,
111+
staticCacheTtl: staticCacheTtl,
112+
dynamicCacheTtl: dynamicCacheTtl,
96113
redirectWebApp: redirectWebApp,
97114
disableTranscode: disableTranscode,
98115
NoRequestLogs: noRequestLogs,

0 commit comments

Comments
 (0)