Is there a way to unify or refactor two methods that do basically the same but the difference being of handling a type of object or a collection of the same type of object?
The following is an implementation of two methods that have very similar behavior. They handle a cache for an API, so when someone asks for a value or a set of values, it verifies that the entry is in cache already. If not, they call the method that will return from an external API: a value or a set of values. Finally the methods, update the cache and return the value/values.
public async Task<T> GetValue<T>(string cacheKey, Func<Task<T>> getItem, MemoryCacheEntryOptions memoryCacheEntryOptions = null)
{
if (_memoryCache.TryGetValue(cacheKey, out T cacheEntry))
{
return cacheEntry;
}
else
{
cacheEntry = await getItem();
return SetValue(cacheKey, cacheEntry, memoryCacheEntryOptions);
}
}
public async Task<IList<T>> GetValues<T>(string cacheKey, Func<Task<IList<T>>> getItems, MemoryCacheEntryOptions memoryCacheEntryOptions = null)
{
if (_memoryCache.TryGetValue(cacheKey, out IList<T> cacheEntries))
{
return cacheEntries;
}
else
{
cacheEntries = await getItems();
return SetValues(cacheKey, cacheEntries, memoryCacheEntryOptions);
}
}