{"record":{"id":"3748126e12e47ab5","repo":"micro/go-micro","slug":"errkeynotfound","errorCode":"ErrKeyNotFound","errorMessage":"key not found in cache","messagePattern":"key not found in cache","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"info","filePath":"cache/cache.go","lineNumber":21,"sourceCode":"import (\n\t\"context\"\n\t\"errors\"\n\t\"time\"\n)\n\nvar (\n\t// DefaultCache is the default cache.\n\tDefaultCache Cache = NewCache()\n\t// DefaultExpiration is the default duration for items stored in\n\t// the cache to expire.\n\tDefaultExpiration time.Duration = 0\n\n\t// ErrItemExpired is returned in Cache.Get when the item found in the cache\n\t// has expired.\n\tErrItemExpired error = errors.New(\"item has expired\")\n\t// ErrKeyNotFound is returned in Cache.Get and Cache.Delete when the\n\t// provided key could not be found in cache.\n\tErrKeyNotFound error = errors.New(\"key not found in cache\")\n)\n\n// Cache is the interface that wraps the cache.\ntype Cache interface {\n\t// Get gets a cached value by key.\n\tGet(ctx context.Context, key string) (interface{}, time.Time, error)\n\t// Put stores a key-value pair into cache.\n\tPut(ctx context.Context, key string, val interface{}, d time.Duration) error\n\t// Delete removes a key from cache.\n\tDelete(ctx context.Context, key string) error\n\t// String returns the name of the implementation.\n\tString() string\n}\n\n// Item represents an item stored in the cache.\ntype Item struct {\n\tValue      interface{}\n\tExpiration int64","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/cache/cache.go#L3-L39","documentation":"ErrKeyNotFound is a sentinel error returned by Cache.Get and Cache.Delete when the provided key does not exist in the cache. It is the cache's way of signaling a pure miss — the key was never stored or has already been removed. Unlike ErrItemExpired, it carries no implication about TTLs.","triggerScenarios":"Cache.Get with a key that was never set; Cache.Delete with an unknown key; Get on a key that was previously evicted or deleted by another goroutine.","commonSituations":"Reading a cache warmed by a different process/instance; typos or inconsistent key naming schemes (e.g. \"user:1\" vs \"user-1\"); race between expiry-eviction and a Get in high-churn caches; calling Delete for idempotent cleanup.","solutions":["Treat it as a cache miss: fetch from the backing store and populate the cache (errors.Is(err, cache.ErrKeyNotFound))","Verify the key format matches the one used when storing (logging the exact key helps)","Check that the cache instance/namespace used for Set is the same one used for Get","For Delete, ignore ErrKeyNotFound when deletion is meant to be idempotent"],"exampleFix":"// before\nif err := cache.Delete(ctx, \"user:1\"); err != nil { return err }\n// after\nif err := cache.Delete(ctx, \"user:1\"); err != nil && !errors.Is(err, cache.ErrKeyNotFound) {\n    return err\n}","handlingStrategy":"fallback","validationCode":"if key == \"\" { return fmt.Errorf(\"empty cache key\") }\n// verify key matches the scheme used at Put time, e.g. \"user:<id>\"","typeGuard":"func isMiss(err error) bool { return errors.Is(err, cache.ErrKeyNotFound) }","tryCatchPattern":"v, _, err := cache.Get(ctx, key)\nif errors.Is(err, cache.ErrKeyNotFound) {\n    v, err = loadFromDB(ctx, key)\n    if err != nil { return err }\n    _ = cache.Put(ctx, key, v, ttl)\n} else if err != nil { return err }","preventionTips":["Centralize key building in one helper to avoid key-format drift","Make Delete idempotent by ignoring ErrKeyNotFound","Warm caches on startup for known hot keys"],"tags":["cache","key-not-found","miss","go"],"backgroundTag":"cache-key-not-found","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}