{"id":"80c0bc7ccb432989","repo":"gofiber/fiber","slug":"cache-unexpected-entry-type-t-for-key-q","errorCode":null,"errorMessage":"cache: unexpected entry type %T for key %q","messagePattern":"cache: unexpected entry type %T for key %q","errorType":"http","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"middleware/cache/manager.go","lineNumber":132,"sourceCode":"\t\t\treturn nil, fmt.Errorf(\"cache: failed to get key %q from storage: %w\", m.logKey(key), err)\n\t\t}\n\t\tif raw == nil {\n\t\t\treturn nil, errCacheMiss\n\t\t}\n\n\t\tit := m.acquire()\n\t\tif _, err := it.UnmarshalMsg(raw); err != nil {\n\t\t\tm.release(it)\n\t\t\treturn nil, fmt.Errorf(\"cache: failed to unmarshal key %q: %w\", m.logKey(key), err)\n\t\t}\n\n\t\treturn it, nil\n\t}\n\n\tif value := m.memory.Get(key); value != nil {\n\t\tit, ok := value.(*item)\n\t\tif !ok {\n\t\t\treturn nil, fmt.Errorf(\"cache: unexpected entry type %T for key %q\", value, m.logKey(key))\n\t\t}\n\t\treturn it, nil\n\t}\n\n\treturn nil, errCacheMiss\n}\n\n// get raw data from storage or memory\nfunc (m *manager) getRaw(ctx context.Context, key string) ([]byte, error) {\n\tif m.storage != nil {\n\t\traw, err := m.storage.GetWithContext(ctx, key)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"cache: failed to get raw key %q from storage: %w\", m.logKey(key), err)\n\t\t}\n\t\tif raw == nil {\n\t\t\treturn nil, errCacheMiss\n\t\t}\n\t\treturn raw, nil","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/middleware/cache/manager.go#L114-L150","documentation":"Thrown at middleware/cache/manager.go:132 on the in-memory fallback path: memory.Get(key) returned a non-nil value whose Go type is not *item. The cache middleware expects every value in its private memory.Storage to be an *item; finding any other type means the store was populated by something else under a colliding key.","triggerScenarios":"Only possible when the cache middleware is NOT given an explicit Storage and instead uses its internally-allocated memory.Storage, yet that same *memory.Storage pointer is somehow shared with another middleware/component that writes a different Go type under the same key. In normal usage newManager allocates a private store so this never fires.","commonSituations":"Test contamination where a shared memory.Storage is reused across middleware instances in a test harness; hand-rolled wiring that injects a pre-populated memory.Storage into multiple managers; a fork that changed newManager to accept a shared store.","solutions":["Stop sharing one *memory.Storage instance across middleware that store different value types; let each middleware allocate its own (the default when Storage is nil).","If sharing is intentional, partition the keyspace with distinct prefixes so types never collide on the same key.","In tests, construct a fresh memory.Storage per middleware instance rather than reusing a global.","Audit any custom code that calls .Set on the memory.Storage to ensure it only stores the type the owning middleware expects."],"exampleFix":"// before (bug): one memory store shared across two middlewares\nshared := memory.New()\ncacheMgr := newManager(shared, false) // expects *item\ncsrfMgr  := newStorageManager(shared, false) // expects []byte -> collision\n\n// after: each manager owns its private store\ncacheMgr := newManager(nil, false)       // allocates private memory\ncsrfMgr  := newStorageManager(nil, false)","handlingStrategy":"type-guard","validationCode":"// Ensure the cache middleware owns its memory store; never inject a shared one.\nif cfg.Storage == nil {\n    // newManager(nil, ...) allocates a private *memory.Storage - safe.\n}","typeGuard":"// If you must read from a shared store, narrow the type before using it.\nfunc asItem(v any) (*item, bool) {\n    it, ok := v.(*item)\n    return it, ok\n}","tryCatchPattern":"raw := m.memory.Get(key)\nif raw != nil {\n    it, ok := raw.(*item)\n    if !ok {\n        // programming error - log loudly and treat as miss\n        log.Printf(\"unexpected type %T in cache memory store\", raw)\n        return nil, errCacheMiss\n    }\n    _ = it\n}","preventionTips":["Never share one *memory.Storage across middlewares with different value types.","Construct middleware with Storage: nil so each allocates privately.","Reset memory.Storage between tests to prevent cross-contamination.","Namespace keys with distinct prefixes if sharing is unavoidable."],"tags":["cache","memory","type-safety","fiber"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}