{"record":{"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":500,"severity":"error","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/a105acad6c1e4576a77f01e02973f67e962bb58d/middleware/cache/manager.go#L114-L150","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Confirm the cache middleware is not sharing its memory.Storage instance with other code that writes arbitrary types.","Ensure each Fiber app instance gets its own cache middleware (do not reuse a single *Storage across apps).","If seen in tests, check that test fixtures don't inject non-item values into the cache's memory store.","Report as a bug if reproduced with a single isolated cache middleware."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":"// The cache owns its memory.Storage; assert nothing else wrote to it.\n// In tests, validate the stored type matches expectation:\nfunc assertItemStored(s *memory.Storage, key string) error {\n    v := s.Get(key)\n    if v == nil {\n        return nil\n    }\n    if _, ok := v.(*item); !ok {\n        return fmt.Errorf(\"unexpected type %T stored under cache key\", v)\n    }\n    return nil\n}","tryCatchPattern":"if value := m.memory.Get(key); value != nil {\n    it, ok := value.(*item)\n    if !ok {\n        // invariant violation — log and treat as miss\n        log.Error(\"cache memory type violation:\", fmt.Sprintf(\"%T\", value))\n        m.memory.Delete(key)\n        return nil, errCacheMiss\n    }\n    return it, nil\n}","preventionTips":["Never share the cache middleware's memory.Storage with other packages.","Instantiate one cache middleware per Fiber app; do not reuse across apps.","In tests, do not inject non-item values into the cache's memory store."],"tags":["cache","type-assertion","memory","invariant","go","fiber"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}