{"record":{"id":"e9b95689128884ef","repo":"dgraph-io/badger","slug":"invalid-cache-type","errorCode":null,"errorMessage":"invalid cache type","messagePattern":"invalid cache type","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"db.go","lineNumber":2146,"sourceCode":")\n\n// CacheMaxCost updates the max cost of the given cache (either block or index cache).\n// The call will have an effect only if the DB was created with the cache. Otherwise it is\n// a no-op. If you pass a negative value, the function will return the current value\n// without updating it.\nfunc (db *DB) CacheMaxCost(cache CacheType, maxCost int64) (int64, error) {\n\tif db == nil {\n\t\treturn 0, nil\n\t}\n\n\tif maxCost < 0 {\n\t\tswitch cache {\n\t\tcase BlockCache:\n\t\t\treturn db.blockCache.MaxCost(), nil\n\t\tcase IndexCache:\n\t\t\treturn db.indexCache.MaxCost(), nil\n\t\tdefault:\n\t\t\treturn 0, errors.New(\"invalid cache type\")\n\t\t}\n\t}\n\n\tswitch cache {\n\tcase BlockCache:\n\t\tdb.blockCache.UpdateMaxCost(maxCost)\n\t\treturn maxCost, nil\n\tcase IndexCache:\n\t\tdb.indexCache.UpdateMaxCost(maxCost)\n\t\treturn maxCost, nil\n\tdefault:\n\t\treturn 0, errors.New(\"invalid cache type\")\n\t}\n}\n\nfunc (db *DB) LevelsToString() string {\n\tlevels := db.Levels()\n\th := func(sz int64) string {","sourceCodeStart":2128,"sourceCodeEnd":2164,"githubUrl":"https://github.com/dgraph-io/badger/blob/2a001d466f6b71a917319a1db41f99860e16e269/db.go#L2128-L2164","documentation":"Returned by db.MaxCost (the query variant of SetMaxCost) when the cache argument is neither BlockCache nor IndexCache. Only those two cache kinds are recognized; passing any other badger.CacheType value yields this error.","triggerScenarios":"Calling db.MaxCost(cache) with a cache type value outside the defined BlockCache/IndexCache set — e.g. a zero-value CacheType, a cast integer, or a type from a different badger version.","commonSituations":"Storing cache kinds in config files and reading them unvalidated; a refactor renaming/adding cache constants; code shared across badger versions where the CacheType enum differs.","solutions":["Only pass the exported constants badger.BlockCache or badger.IndexCache","Validate/cache-normalize the value from config before calling MaxCost","Check the badger API docs for the exact CacheType constants in your version"],"exampleFix":"// before\ncost, err := db.MaxCost(badger.CacheType(cfg.CacheKind))\n\n// after\nvar ct badger.CacheType\nswitch cfg.CacheKind {\ncase \"block\": ct = badger.BlockCache\ncase \"index\": ct = badger.IndexCache\ndefault: return fmt.Errorf(\"unknown cache kind %q\", cfg.CacheKind)\n}\ncost, err := db.MaxCost(ct)","handlingStrategy":"type-guard","validationCode":"if ct != badger.BlockCache && ct != badger.IndexCache { return errors.New(\"unsupported cache type\") }","typeGuard":"func validCacheType(ct badger.CacheType) bool {\n    return ct == badger.BlockCache || ct == badger.IndexCache\n}","tryCatchPattern":null,"preventionTips":["Only use exported badger cache constants","Validate cache settings from config against a whitelist","Re-check constants when upgrading badger versions"],"tags":["badger","cache","invalid-argument"],"backgroundTag":"invalid-enum-value","analyzedSha":"2a001d466f6b71a917319a1db41f99860e16e269","analyzedAt":"2026-09-05T13:00:02.264Z","contentChangedAt":"2026-09-05T13:00:02.264Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}