Skip to content

Commit 6dad129

Browse files
committed
feat: make the cache size of static files configurable
1 parent 502ff29 commit 6dad129

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ env CGO_ENABLED=0 go install -trimpath -ldflags="-s -w" github.com/RoyXiang/plex
3333
* Set it if you run an instance of [Plaxt](https://github.com/XanderStrike/goplaxt)
3434
* Or, you can set it to [the official one](https://plaxt.astandke.com/)
3535
- `PLEX_TOKEN` (Optional, if you need it, see [here](https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/))
36+
- `STATIC_CACHE_SIZE` (Optional, the cache size of static files, e.g. CSS files, images, default: `1000`)
3637
- `REDIRECT_WEB_APP` (Optional, default: `true`)
3738
- `DISABLE_TRANSCODE` (Optional, default: `true`)
3839
- `NO_REQUEST_LOGS` (Optional, default: `false`)

handler/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func init() {
2121
BaseUrl: os.Getenv("PLEX_BASEURL"),
2222
Token: os.Getenv("PLEX_TOKEN"),
2323
PlaxtUrl: os.Getenv("PLAXT_URL"),
24+
StaticCacheSize: os.Getenv("STATIC_CACHE_SIZE"),
2425
RedirectWebApp: os.Getenv("REDIRECT_WEB_APP"),
2526
DisableTranscode: os.Getenv("DISABLE_TRANSCODE"),
2627
NoRequestLogs: os.Getenv("NO_REQUEST_LOGS"),

handler/middleware.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ func cacheMiddleware(next http.Handler) http.Handler {
193193
params := r.URL.Query()
194194
switch info.Prefix {
195195
case cachePrefixStatic:
196+
if plexClient.staticCache == nil {
197+
return
198+
}
196199
cache = plexClient.staticCache
197200
case cachePrefixDynamic:
198201
if user := r.Context().Value(userCtxKey); user != nil {

handler/plex.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ type PlexConfig struct {
2424
BaseUrl string
2525
Token string
2626
PlaxtUrl string
27-
StaticCacheTtl string
28-
DynamicCacheTtl string
27+
StaticCacheSize string
2928
RedirectWebApp string
3029
DisableTranscode string
3130
NoRequestLogs string
@@ -78,7 +77,12 @@ func NewPlexClient(config PlexConfig) *PlexClient {
7877
plaxtUrl = u.String()
7978
}
8079

81-
staticCache := gcache.New(1000).LFU().Build()
80+
var staticCache gcache.Cache
81+
if config.StaticCacheSize == "" {
82+
staticCache = gcache.New(1000).LFU().Build()
83+
} else if staticCacheSize, err := strconv.Atoi(config.StaticCacheSize); err == nil && staticCacheSize > 0 {
84+
staticCache = gcache.New(staticCacheSize).LFU().Build()
85+
}
8286
dynamicCache := gcache.New(100).LRU().Expiration(time.Second).Build()
8387

8488
var redirectWebApp, disableTranscode, noRequestLogs bool

0 commit comments

Comments
 (0)