using System.Collections.Generic;
using System.Threading.Tasks;
using System.Diagnostics;
public record MoodDto(DateTime Day, int Value);
public interface IMoodService {
Task<List<MoodDto>> GetMoods(int id);
public class MoodService : IMoodService
public async Task<List<MoodDto>> GetMoods(int id)
new MoodDto(DateTime.Now, Random.Shared.Next(0,5)),
new MoodDto(DateTime.Now, Random.Shared.Next(0,5)),
new MoodDto(DateTime.Now, Random.Shared.Next(0,5))
public class CachingMoodService : IMoodService
private readonly IMoodService _service;
private readonly Dictionary<int, List<MoodDto>> _cache = new();
public CachingMoodService(IMoodService service)
public async Task<List<MoodDto>> GetMoods(int id)
if(!_cache.ContainsKey(id))
var mood = await _service.GetMoods(id);
public class LoggingMoodService : IMoodService
private readonly IMoodService _service;
public string Name {get;set;} = "MoodService";
public LoggingMoodService(IMoodService service)
public async Task<List<MoodDto>> GetMoods(int id)
Console.WriteLine($"|{Name}| /‾‾ Reading moods for userId {id}...");
var timer = new Stopwatch();
var moods = await _service.GetMoods(id);
var str = string.Join(", ", moods.Select(x=>x.Value));
Console.WriteLine($"|{Name}| \\__ Moods for userId {id}: [{str}]. Time taken: {timer.Elapsed.TotalSeconds:F3}s");
public static async Task Main()
IMoodService moodService = new LoggingMoodService(new CachingMoodService(new MoodService()));
var moods1 = await moodService.GetMoods(user1Id);
var moods1_2 = await moodService.GetMoods(user1Id);
var moods1_3 = await moodService.GetMoods(user1Id);
var moods2 = await moodService.GetMoods(user2Id);
var moods2_2 = await moodService.GetMoods(user2Id);