Question
Yii Cache::get() cannot distinguish cached false from missing key
Zo Dey
0 reputation · 24 Jan 2022, 20:38 UTC
121.1K views0
Yii 2's base Cache::get() returns false both when a key is absent and when the stored value is boolean false. This makes cached feature flags, permission results, and validation outcomes indistinguishable from a cache miss using get() alone.
Cache::has() can test key existence independently, but it is less prominent in core guides, and Yii 1.x may not expose it consistently. Adapter serialization may also differ across FileCache, DbCache, MemCache, and RedisCache.
Goal: determine the recommended pattern for reliably detecting a cached false value without triggering unnecessary recomputation.
- Should applications call Cache::has() before Cache::get() to disambiguate false?
- Is wrapping false in an array or sentinel object the safer cross-adapter approach?
- Does Yii 1.x lack Cache::has(), requiring a version-specific workaround?