Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save leandroribeiro/9e46b50235ad4e93eeb97f6eb576a0bb to your computer and use it in GitHub Desktop.
Save leandroribeiro/9e46b50235ad4e93eeb97f6eb576a0bb to your computer and use it in GitHub Desktop.
.NET Core Memory Cache Simple Demo 01
try
{
var provider = new ServiceCollection()
.AddMemoryCache()
.BuildServiceProvider();
//And now?
var cache = provider.GetService<IMemoryCache>();
using (var entry = cache.CreateEntry("item2"))
{
entry.Value = 2;
entry.AbsoluteExpiration = DateTime.UtcNow.AddDays(1);
}
if (cache.TryGetValue("item2", out object item1))
{
Console.WriteLine(item1);
}
}
catch(NullReferenceException e)
{
Console.WriteLine(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment