Home / Function/ New() — fiber Function Reference

New() — fiber Function Reference

Architecture documentation for the New() function in cache.go from the fiber codebase.

Entity Profile

Dependency Diagram

graph TD
  96447356_67b9_a364_148e_b703c487e1ba["New()"]
  af95e058_7e86_ec88_42f0_cd294e342508["cache.go"]
  96447356_67b9_a364_148e_b703c487e1ba -->|defined in| af95e058_7e86_ec88_42f0_cd294e342508
  ce89cad2_6f17_177a_4391_43647b4433ad["safeUnixSeconds()"]
  96447356_67b9_a364_148e_b703c487e1ba -->|calls| ce89cad2_6f17_177a_4391_43647b4433ad
  1cbe65a5_d3fb_f901_591e_d885d0a87b2b["makeHashAuthFunc()"]
  96447356_67b9_a364_148e_b703c487e1ba -->|calls| 1cbe65a5_d3fb_f901_591e_d885d0a87b2b
  94a0c0d9_758b_e1dd_a717_293b190ce2c3["makeBuildVaryKeyFunc()"]
  96447356_67b9_a364_148e_b703c487e1ba -->|calls| 94a0c0d9_758b_e1dd_a717_293b190ce2c3
  5343e48d_16ff_1d1d_8d05_06538558e8fd["secondsToTime()"]
  96447356_67b9_a364_148e_b703c487e1ba -->|calls| 5343e48d_16ff_1d1d_8d05_06538558e8fd
  27f304b5_9285_92cb_4e9d_3057235a1733["parseRequestCacheControl()"]
  96447356_67b9_a364_148e_b703c487e1ba -->|calls| 27f304b5_9285_92cb_4e9d_3057235a1733
  de131d33_14bf_b0ce_186c_daf7c86d2bac["hasDirective()"]
  96447356_67b9_a364_148e_b703c487e1ba -->|calls| de131d33_14bf_b0ce_186c_daf7c86d2bac
  3928e718_6cb7_4319_b7d2_636e6c27978e["loadVaryManifest()"]
  96447356_67b9_a364_148e_b703c487e1ba -->|calls| 3928e718_6cb7_4319_b7d2_636e6c27978e
  478b6047_638b_2fc8_7606_20639ce40639["remainingFreshness()"]
  96447356_67b9_a364_148e_b703c487e1ba -->|calls| 478b6047_638b_2fc8_7606_20639ce40639
  91ff379d_ad97_f896_9c48_6574a957d3c1["cachedResponseAge()"]
  96447356_67b9_a364_148e_b703c487e1ba -->|calls| 91ff379d_ad97_f896_9c48_6574a957d3c1
  0fffe292_722c_995a_0597_418541b90104["lookupCachedHeader()"]
  96447356_67b9_a364_148e_b703c487e1ba -->|calls| 0fffe292_722c_995a_0597_418541b90104
  0106d00f_98fa_a6f5_6862_701065730619["cacheBodyFetchError()"]
  96447356_67b9_a364_148e_b703c487e1ba -->|calls| 0106d00f_98fa_a6f5_6862_701065730619
  03bedd00_a450_6002_7e6e_f9a1125a9591["clampDateSeconds()"]
  96447356_67b9_a364_148e_b703c487e1ba -->|calls| 03bedd00_a450_6002_7e6e_f9a1125a9591
  021e3c97_8af0_f456_5883_d87c6349628e["appendWarningHeaders()"]
  96447356_67b9_a364_148e_b703c487e1ba -->|calls| 021e3c97_8af0_f456_5883_d87c6349628e
  style 96447356_67b9_a364_148e_b703c487e1ba fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

middleware/cache/cache.go lines 108–905

func New(config ...Config) fiber.Handler {
	// Set default config
	cfg := configDefault(config...)

	type evictionCandidate struct {
		key     string
		size    uint
		exp     uint64
		heapIdx int
	}

	redactKeys := !cfg.DisableValueRedaction

	maskKey := func(key string) string {
		if redactKeys {
			return redactedKey
		}
		return key
	}

	// Nothing to cache
	if int(cfg.Expiration.Seconds()) < 0 {
		return func(c fiber.Ctx) error {
			return c.Next()
		}
	}

	var (
		// Cache settings
		mux       = &sync.RWMutex{}
		timestamp = safeUnixSeconds(time.Now())
	)
	// Create manager to simplify storage operations ( see manager.go )
	manager := newManager(cfg.Storage, redactKeys)
	// Create indexed heap for tracking expirations ( see heap.go )
	heap := &indexedHeap{}
	// count stored bytes (sizes of response bodies)
	var storedBytes uint
	// Pool for hex encoding buffers
	hexBufPool := &sync.Pool{
		New: func() any {
			buf := make([]byte, hexLen)
			return &buf
		},
	}
	hashAuthorization := makeHashAuthFunc(hexBufPool)
	buildVaryKey := makeBuildVaryKeyFunc(hexBufPool)

	// Update timestamp in the configured interval
	go func() {
		ticker := time.NewTicker(timestampUpdatePeriod)
		defer ticker.Stop()
		for range ticker.C {
			atomic.StoreUint64(&timestamp, safeUnixSeconds(time.Now()))
		}
	}()

	// Delete key from both manager and storage
	deleteKey := func(ctx context.Context, dkey string) error {
		if err := manager.del(ctx, dkey); err != nil {
			return err
		}
		// External storage saves body data with different key
		if cfg.Storage != nil {
			if err := manager.del(ctx, dkey+"_body"); err != nil {
				return err
			}
		}
		return nil
	}

	removeHeapEntry := func(entryKey string, heapIdx int) {
		if cfg.MaxBytes == 0 {
			return
		}

		if heapIdx < 0 || heapIdx >= len(heap.indices) {
			return
		}

		indexedIdx := heap.indices[heapIdx]

Subdomains

Frequently Asked Questions

What does New() do?
New() is a function in the fiber codebase, defined in middleware/cache/cache.go.
Where is New() defined?
New() is defined in middleware/cache/cache.go at line 108.
What does New() call?
New() calls 20 function(s): allowsSharedCacheDirectives, appendWarningHeaders, cacheBodyFetchError, cachedResponseAge, clampDateSeconds, hasDirective, isHeuristicFreshness, loadVaryManifest, and 12 more.

Analyze Your Own Codebase

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

Try Supermodel Free