Skip to content

Commit

Permalink
feat: set a default TTL for the cache of static files
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyXiang committed Jan 25, 2024
1 parent f1a68eb commit 0cd6d01
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ env CGO_ENABLED=0 go install -trimpath -ldflags="-s -w" github.com/RoyXiang/plex
* 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_SIZE` (Optional, the cache size of static files, e.g. CSS files, images, default: `1000`)
- `STATIC_CACHE_TTL` (Optional, the cache TTL of static files, default: `72h`)
- `REDIRECT_WEB_APP` (Optional, default: `true`)
- `DISABLE_TRANSCODE` (Optional, default: `true`)
- `NO_REQUEST_LOGS` (Optional, default: `false`)
Expand Down
1 change: 1 addition & 0 deletions handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func init() {
Token: os.Getenv("PLEX_TOKEN"),
PlaxtUrl: os.Getenv("PLAXT_URL"),
StaticCacheSize: os.Getenv("STATIC_CACHE_SIZE"),
StaticCacheTtl: os.Getenv("STATIC_CACHE_TTL"),
RedirectWebApp: os.Getenv("REDIRECT_WEB_APP"),
DisableTranscode: os.Getenv("DISABLE_TRANSCODE"),
NoRequestLogs: os.Getenv("NO_REQUEST_LOGS"),
Expand Down
16 changes: 11 additions & 5 deletions handler/plex.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type PlexConfig struct {
Token string
PlaxtUrl string
StaticCacheSize string
StaticCacheTtl string
RedirectWebApp string
DisableTranscode string
NoRequestLogs string
Expand Down Expand Up @@ -77,12 +78,17 @@ func NewPlexClient(config PlexConfig) *PlexClient {
plaxtUrl = u.String()
}

var staticCache gcache.Cache
if config.StaticCacheSize == "" {
staticCache = gcache.New(1000).LFU().Build()
} else if staticCacheSize, err := strconv.Atoi(config.StaticCacheSize); err == nil && staticCacheSize > 0 {
staticCache = gcache.New(staticCacheSize).LFU().Build()
var (
staticCacheSize int
staticCacheTtl time.Duration
)
if staticCacheSize, err = strconv.Atoi(config.StaticCacheSize); err != nil || staticCacheSize <= 0 {
staticCacheSize = 1000
}
if staticCacheTtl, err = time.ParseDuration(config.StaticCacheTtl); err != nil {
staticCacheTtl = time.Hour * 24 * 3
}
staticCache := gcache.New(staticCacheSize).LFU().Expiration(staticCacheTtl).Build()
dynamicCache := gcache.New(100).LRU().Expiration(time.Second).Build()

var redirectWebApp, disableTranscode, noRequestLogs bool
Expand Down

0 comments on commit 0cd6d01

Please sign in to comment.