Created
May 7, 2018 17:16
-
-
Save leandroribeiro/9e46b50235ad4e93eeb97f6eb576a0bb to your computer and use it in GitHub Desktop.
.NET Core Memory Cache Simple Demo 01
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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