Home / Function/ Test_Cache_RevalidationWithMaxBytes() — fiber Function Reference

Test_Cache_RevalidationWithMaxBytes() — fiber Function Reference

Architecture documentation for the Test_Cache_RevalidationWithMaxBytes() function in cache_test.go from the fiber codebase.

Entity Profile

Dependency Diagram

graph TD
  6f8eb8fa_11bb_adef_bb23_83f65cf1c584["Test_Cache_RevalidationWithMaxBytes()"]
  8453a087_9678_fe96_1b20_2d125b6f8656["cache_test.go"]
  6f8eb8fa_11bb_adef_bb23_83f65cf1c584 -->|defined in| 8453a087_9678_fe96_1b20_2d125b6f8656
  d85d9be6_add5_aae9_e8ed_6409b730b1bf["stableAscendingExpiration()"]
  6f8eb8fa_11bb_adef_bb23_83f65cf1c584 -->|calls| d85d9be6_add5_aae9_e8ed_6409b730b1bf
  style 6f8eb8fa_11bb_adef_bb23_83f65cf1c584 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

middleware/cache/cache_test.go lines 3442–3636

func Test_Cache_RevalidationWithMaxBytes(t *testing.T) {
	t.Parallel()

	t.Run("max-age=0 revalidation removes old entry on storage success", func(t *testing.T) {
		t.Parallel()

		app := fiber.New()

		app.Use(New(Config{
			MaxBytes: 100,
		}))

		requestCount := 0
		app.Get("/test", func(c fiber.Ctx) error {
			requestCount++
			c.Set(fiber.HeaderCacheControl, "max-age=60")
			return c.SendString(fmt.Sprintf("response-%d", requestCount))
		})

		// First request - cache the response
		req1 := httptest.NewRequest(fiber.MethodGet, "/test", http.NoBody)
		resp1, err := app.Test(req1)
		require.NoError(t, err)
		require.Equal(t, cacheMiss, resp1.Header.Get("X-Cache"))

		// Request with max-age=0 to force revalidation
		req2 := httptest.NewRequest(fiber.MethodGet, "/test", http.NoBody)
		req2.Header.Set(fiber.HeaderCacheControl, "max-age=0")
		resp2, err := app.Test(req2)
		require.NoError(t, err)
		body2, err := io.ReadAll(resp2.Body)
		require.NoError(t, err)
		require.Equal(t, "response-2", string(body2))

		// Next request should serve the NEW cached entry
		req3 := httptest.NewRequest(fiber.MethodGet, "/test", http.NoBody)
		resp3, err := app.Test(req3)
		require.NoError(t, err)
		require.Equal(t, cacheHit, resp3.Header.Get("X-Cache"))
		body3, err := io.ReadAll(resp3.Body)
		require.NoError(t, err)
		require.Equal(t, "response-2", string(body3), "New entry should be cached")
	})

	t.Run("min-fresh revalidation with MaxBytes", func(t *testing.T) {
		t.Parallel()

		app := fiber.New()

		app.Use(New(Config{
			MaxBytes: 100,
		}))

		requestCount := 0
		app.Get("/test", func(c fiber.Ctx) error {
			requestCount++
			c.Set(fiber.HeaderCacheControl, "max-age=2")
			return c.SendString(fmt.Sprintf("response-%d", requestCount))
		})

		// First request - cache the response
		req1 := httptest.NewRequest(fiber.MethodGet, "/test", http.NoBody)
		resp1, err := app.Test(req1)
		require.NoError(t, err)
		require.Equal(t, cacheMiss, resp1.Header.Get("X-Cache"))

		// Wait a bit so the entry has aged
		time.Sleep(1 * time.Second)

		// Request with min-fresh that exceeds remaining freshness
		req2 := httptest.NewRequest(fiber.MethodGet, "/test", http.NoBody)
		req2.Header.Set(fiber.HeaderCacheControl, "min-fresh=5")
		resp2, err := app.Test(req2)
		require.NoError(t, err)
		body2, err := io.ReadAll(resp2.Body)
		require.NoError(t, err)
		require.Equal(t, "response-2", string(body2))

		// Next request should serve the NEW cached entry
		req3 := httptest.NewRequest(fiber.MethodGet, "/test", http.NoBody)
		resp3, err := app.Test(req3)

Subdomains

Frequently Asked Questions

What does Test_Cache_RevalidationWithMaxBytes() do?
Test_Cache_RevalidationWithMaxBytes() is a function in the fiber codebase, defined in middleware/cache/cache_test.go.
Where is Test_Cache_RevalidationWithMaxBytes() defined?
Test_Cache_RevalidationWithMaxBytes() is defined in middleware/cache/cache_test.go at line 3442.
What does Test_Cache_RevalidationWithMaxBytes() call?
Test_Cache_RevalidationWithMaxBytes() calls 1 function(s): stableAscendingExpiration.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free