Using Cache in .NET Core

cache

 Register the service in startup.cs:


services.AddMemoryCache((opts) => 
{ 
});

Inject it as IMemoryCache (e.g. in a controller):


HomeController(IMemoryCache memoryCache) {
    this.memoryCache = memoryCache;
}

Set a new value:


cache.Set("accessToken", 
	response.Data.AccessToken, 
	TimeSpan.FromSeconds(response.Data.ExpiresIn - 60)
);

Get value:


cache.TryGetValue("accessToken", out string accessToken);

Post a Comment

Previous Post Next Post