If you've added Redis to an ASP.NET Core app, you've probably met IDistributedCache. It does the job, but it only speaks in byte[] and string keys. So every read or write turns into the same little ritual: serialize, null-check, deserialize. Do that in one place and it's fine. Do it in twenty, and you've got a pile of nearly identical caching code that's surprisingly easy to get wrong.

This post keeps Redis as your store but caches typed objects instead of byte arrays, using a small open-source package called CacheManager.Redis. We'll look at the boilerplate problem, write a clean cache-aside method, prefix keys for shared Redis, and figure out when you'd actually want to reach for something else.

The boilerplate problem with IDistributedCache

Here's the usual cache-aside read with the raw IDistributedCache:

public async Task<Weather?> GetForecastAsync(string city, CancellationToken ct)