From cec9ca0ff1c052f9e36a033aec7c96e090c6d351 Mon Sep 17 00:00:00 2001 From: perennial Date: Wed, 16 Oct 2024 15:01:55 +1100 Subject: [PATCH] docs: update caching section --- config/config.go | 2 +- core/requests.go | 2 +- doc/hosting/environment-variables.md | 21 +++++++++++++-------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/config/config.go b/config/config.go index 51b285c..4325eb7 100644 --- a/config/config.go +++ b/config/config.go @@ -139,7 +139,7 @@ func (s *ServerConfig) LoadConfig() error { s.APIMaxBackoffTime = 8000 * time.Millisecond s.CacheSize = 100 - s.CacheTTL = 5 * time.Minute + s.CacheTTL = 60 * time.Minute s.CacheShortTTL = 10 * time.Second s.ResponseSaveLocation = "/tmp/pixivfe/responses" diff --git a/core/requests.go b/core/requests.go index a5c3d7e..b704949 100644 --- a/core/requests.go +++ b/core/requests.go @@ -166,7 +166,7 @@ func retryRequest( // Check for X-Handled-Redirected in the incoming request if redirectedHeader := incomingHeaders.Get("X-Handled-Redirected"); redirectedHeader != "" { // fmt.Printf("retryRequest: X-Handled-Redirected header found: %s\n", redirectedHeader) - if strings.Contains(strings.ToLower(redirectedHeader), "no-cache") { + if strings.Contains(strings.ToLower(redirectedHeader), "true") { // fmt.Println("retryRequest: X-Handled-Redirected found, removing existing cache entry if present") // Remove existing cache entry if present cacheLock.Lock() diff --git a/doc/hosting/environment-variables.md b/doc/hosting/environment-variables.md index a53ad93..9683a40 100644 --- a/doc/hosting/environment-variables.md +++ b/doc/hosting/environment-variables.md @@ -199,12 +199,11 @@ Maximum backoff time for token management. ## Caching configuration -PixivFE caches responses from the Pixiv API to improve performance. The following environment variables can be used to configure the caching behavior. +PixivFE implements a caching system for API responses to improve performance. The cache uses a [Least Recently Used (LRU) eviction policy](https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_Recently_Used_(LRU)). -!!! note - While caching improves performance, it also means that changes on Pixiv might not be immediately reflected in PixivFE. +Each cache entry is stored with an expiration time. When a cached item is accessed, its expiration time is checked. If the item has expired, it is treated as a cache miss, and a new request is made to the Pixiv API. - If you want content updates that are closer to real-time, consider reducing the cache TTLs. +To ensure that responses are properly isolated between different users, the cache key for each item is generated based on both the URL of the request and the user's `pixivfe-Token` cookie. ### `PIXIVFE_CACHE_SIZE` @@ -212,15 +211,15 @@ PixivFE caches responses from the Pixiv API to improve performance. The followin **Default:** `100` -Specifies the maximum number of items that can be stored in the cache. This limits the memory usage of the cache. +Specifies the maximum number of items that can be stored in the LRU cache. This limits the memory usage of the cache. When the cache reaches this size, the least recently used items will be evicted to make room for new entries. ### `PIXIVFE_CACHE_TTL` **Required**: No -**Default:** `5m` +**Default:** `60m` -Specifies the Time To Live (TTL) for cached items. This is the duration for which an item remains valid in the cache before it's considered stale and needs to be fetched again. +Specifies the default Time To Live (TTL) for cached items. This is the duration for which an item remains valid in the cache before it's considered stale and needs to be fetched again from the Pixiv API. The TTL is applied to most API responses and can safely be set to a high value. ### `PIXIVFE_CACHE_SHORT_TTL` @@ -228,7 +227,13 @@ Specifies the Time To Live (TTL) for cached items. This is the duration for whic **Default:** `10s` -Specifies the Time To Live (TTL) for cached items when matching one of the `shortTTLPaths`. +Specifies a shorter Time To Live (TTL) for specific API endpoints that change frequently. This shorter TTL is applied to the following paths: + +- `/ajax/discovery/artworks` (used for the `/discovery` page) +- `/ajax/discovery/novels` (used for the `/discovery/novels` page) +- `/ajax/illust/new` (used for the `/newest` page) + +These paths are considered more dynamic, and the shorter TTL ensures more up-to-date content. ## Network proxy configuration