using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Hosting;
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Services.AddTransient<IMyService, MyService>();
builder.Services.AddMemoryCache();
using IHost host = builder.Build();
Console.WriteLine("Starting the application");
MyProgram(host.Services.GetRequiredService<IMyService>());
static void MyProgram(IMyService svc){
Console.WriteLine("Attempting to save the item into memory");
svc.SaveIt("key","value");
public interface IMyService{
void SaveIt(string key,string val);
public class MyService : IMyService {
private readonly IMemoryCache cache;
public MyService(IMemoryCache cache){
public void SaveIt(string key,string val){
cache.Set(key, val, TimeSpan.FromDays(1));
var item = cache.Get(key);
Console.WriteLine(item.ToString());