Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ func NewApiServer(config config.Config) *ApiServer {
panic(err)
}

sitemapPageCache, err := otter.MustBuilder[string, []byte](256).
WithTTL(sitemapPageCacheTTL).
CollectStats().
Build()
if err != nil {
panic(err)
}

privateKey, err := crypto.HexToECDSA(config.DelegatePrivateKey)
if err != nil {
panic(err)
Expand Down Expand Up @@ -268,6 +276,7 @@ func NewApiServer(config config.Config) *ApiServer {
oauthTokenCache: &oauthTokenCache,
qualifiedPlaylistsCache: &qualifiedPlaylistsCache,
relatedUsersCache: &relatedUsersCache,
sitemapPageCache: &sitemapPageCache,
requestValidator: requestValidator,
rewardAttester: rewardAttester,
transactionSender: transactionSender,
Expand Down Expand Up @@ -822,6 +831,7 @@ type ApiServer struct {
oauthTokenCache *otter.Cache[string, oauthTokenCacheEntry]
qualifiedPlaylistsCache *otter.Cache[string, []int32]
relatedUsersCache *otter.Cache[string, []int32]
sitemapPageCache *otter.Cache[string, []byte]
requestValidator *RequestValidator
rewardManagerClient *reward_manager.RewardManagerClient
claimableTokensClient *claimable_tokens.ClaimableTokensClient
Expand Down
8 changes: 8 additions & 0 deletions api/v1_sitemaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

const sitemapLimit = 40_000
const sitemapCountCacheTTL = 1 * time.Hour
const sitemapPageCacheTTL = 1 * time.Hour

var sitemapPageRegex = regexp.MustCompile(`^(\d+)\.xml$`)

Expand Down Expand Up @@ -266,6 +267,12 @@ func (app *ApiServer) sitemapTypePage(c *fiber.Ctx) error {
return fiber.NewError(400, "Page number must be >= 1")
}

cacheKey := entityType + ":" + fileName
if data, ok := app.sitemapPageCache.Get(cacheKey); ok {
c.Set("Content-Type", "text/xml")
return c.Send(data)
}

offset := int32((pageNumber - 1) * sitemapLimit)
ctx := c.Context()
baseURL := app.audiusAppUrl
Expand Down Expand Up @@ -328,6 +335,7 @@ func (app *ApiServer) sitemapTypePage(c *fiber.Ctx) error {
if err != nil {
return err
}
app.sitemapPageCache.Set(cacheKey, data)
c.Set("Content-Type", "text/xml")
return c.Send(data)
}
11 changes: 11 additions & 0 deletions api/v1_sitemaps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ func TestSitemapTrackPage(t *testing.T) {
assert.NotContains(t, xml, "%2F")
}

func TestSitemapTrackPageCache(t *testing.T) {
app := sitemapTestApp(t)

cached := []byte(`<?xml version="1.0" encoding="UTF-8"?><urlset><url><loc>cached-track-page</loc></url></urlset>`)
require.True(t, app.sitemapPageCache.Set("track:1.xml", cached))

status, body := testGet(t, app, "/sitemaps/track/1.xml")
require.Equal(t, 200, status)
assert.Contains(t, string(body), "cached-track-page")
}

func TestSitemapPlaylistPage(t *testing.T) {
app := sitemapTestApp(t)
status, body := testGet(t, app, "/sitemaps/playlist/1.xml")
Expand Down
Loading