public static T?GetOrSetJsonObject <T>(this IDistributedCache cache, string key, Func <T?> valueFactory, DistributedCacheEntryOptions entryOptions, JsonSerializerOptions?serializerOptions = null) where T : class { var cached = cache.GetJsonObject <T>(key, serializerOptions); if (cached != null) { return(cached); } var value = valueFactory(); if (value != null) { cache.SetJsonObject(key, value, entryOptions, serializerOptions); } return(value); }