gofiber/fiber · warning

cache: unexpected raw entry type %T for key %q

Error message

cache: unexpected raw entry type %T for key %q

What it means

Thrown at middleware/cache/manager.go:156 on the in-memory fallback path of getRaw: memory.Get(key) returned a non-nil value that is not []byte. The raw path expects byte slices; any other Go type indicates cross-middleware contamination of the shared memory store.

Source

Thrown at middleware/cache/manager.go:156

}

// get raw data from storage or memory
func (m *manager) getRaw(ctx context.Context, key string) ([]byte, error) {
	if m.storage != nil {
		raw, err := m.storage.GetWithContext(ctx, key)
		if err != nil {
			return nil, fmt.Errorf("cache: failed to get raw key %q from storage: %w", m.logKey(key), err)
		}
		if raw == nil {
			return nil, errCacheMiss
		}
		return raw, nil
	}

	if value := m.memory.Get(key); value != nil {
		raw, ok := value.([]byte)
		if !ok {
			return nil, fmt.Errorf("cache: unexpected raw entry type %T for key %q", value, m.logKey(key))
		}
		return raw, nil
	}

	return nil, errCacheMiss
}

// set data to storage or memory
func (m *manager) set(ctx context.Context, key string, it *item, exp time.Duration) error {
	if m.storage != nil {
		raw, err := it.MarshalMsg(nil)
		if err != nil {
			m.release(it)
			return fmt.Errorf("cache: failed to marshal key %q: %w", m.logKey(key), err)
		}
		if err := m.storage.SetWithContext(ctx, key, raw, exp); err != nil {
			m.release(it)
			return fmt.Errorf("cache: failed to store key %q: %w", m.logKey(key), err)

View on GitHub (pinned to 9a4c7e57fe)

Solutions

  1. Give the cache middleware its own memory.Storage (pass nil for Storage so it allocates privately).
  2. Namespace keys with distinct prefixes if sharing a store is unavoidable.
  3. Reset shared memory.Storage between tests to avoid cross-test leakage.
  4. Audit every .Set call on the shared store to confirm only []byte values land on raw-path keys.

Example fix

// before: shared store serves both msgp *item and raw []byte paths
store := memory.New()
m := newManager(store, false)

// after: private store per manager
m := newManager(nil, false) // owns its memory.Storage; type collisions impossible
Defensive patterns

Strategy: type-guard

Validate before calling

// Don't share the cache's memory store with components that store non-[]byte values.
if cfg.Storage == nil { /* private memory store - safe */ }

Type guard

func asBytes(v any) ([]byte, bool) {
    b, ok := v.([]byte)
    return b, ok
}

Try / catch

v := m.memory.Get(key)
if v != nil {
    raw, ok := v.([]byte)
    if !ok {
        log.Printf("unexpected raw type %T", v)
        return nil, errCacheMiss
    }
    _ = raw
}

Prevention

When it happens

Trigger: Same class as error 142: only fires when the cache's internal memory.Storage is shared with another component that stored a non-[]byte value under a colliding key. Does not occur in default wiring where each manager owns its private store.

Common situations: Test harness reusing one memory.Storage across cache + another middleware; custom fork injecting a shared store; deliberate key reuse across components in development.

Related errors


AI-assisted analysis of gofiber/fiber@9a4c7e57fe (2026-08-04). Data as JSON: /data/errors/24abc2613af04f28.json. Report an issue: GitHub.