gofiber/fiber · error

cache: unexpected entry type %T for key

Error message

cache: unexpected entry type %T for key %q

What it means

Returned by the in-memory branch of manager.get when the value stored under a key is not an *item. The cache only ever stores *item into memory.Storage, so reaching this branch implies pool corruption, memory aliasing, or another component sharing the same memory.Storage instance and writing a different type under a colliding key.

Solutions

  1. Confirm the cache middleware is not sharing its memory.Storage instance with other code that writes arbitrary types.
  2. Ensure each Fiber app instance gets its own cache middleware (do not reuse a single *Storage across apps).
  3. If seen in tests, check that test fixtures don't inject non-item values into the cache's memory store.
  4. Report as a bug if reproduced with a single isolated cache middleware.
Defensive patterns

Strategy: type-guard

Type guard

// The cache owns its memory.Storage; assert nothing else wrote to it.
// In tests, validate the stored type matches expectation:
func assertItemStored(s *memory.Storage, key string) error {
    v := s.Get(key)
    if v == nil {
        return nil
    }
    if _, ok := v.(*item); !ok {
        return fmt.Errorf("unexpected type %T stored under cache key", v)
    }
    return nil
}

Try / catch

if value := m.memory.Get(key); value != nil {
    it, ok := value.(*item)
    if !ok {
        // invariant violation — log and treat as miss
        log.Error("cache memory type violation:", fmt.Sprintf("%T", value))
        m.memory.Delete(key)
        return nil, errCacheMiss
    }
    return it, nil
}

Prevention

When it happens

Trigger: cfg.Storage is nil (in-memory mode) and memory.Get(key) returns a value whose dynamic type is not *item. Requires a shared memory.Storage between packages or a manual Set of an alien value into the same key namespace used by the cache.

Common situations: Effectively unreachable in correct single-middleware usage; seen if the in-memory Storage is shared globally across middlewares with overlapping key spaces, or in tests that pre-seed memory.Storage with wrong types. Indicates a bug rather than a configuration mistake.

Related errors


AI-assisted analysis of gofiber/fiber@a105acad6c (2026-08-11). Data as JSON: /api/errors/80c0bc7ccb432989. Report an issue: GitHub.

Appendix: source

Thrown at middleware/cache/manager.go:132

			return nil, fmt.Errorf("cache: failed to get key %q from storage: %w", m.logKey(key), err)
		}
		if raw == nil {
			return nil, errCacheMiss
		}

		it := m.acquire()
		if _, err := it.UnmarshalMsg(raw); err != nil {
			m.release(it)
			return nil, fmt.Errorf("cache: failed to unmarshal key %q: %w", m.logKey(key), err)
		}

		return it, nil
	}

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

	return nil, errCacheMiss
}

// 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

View on GitHub (pinned to a105acad6c)