Adicionando a hora atual no cache
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
public IActionResult Index() | |
{ | |
//Verifica se existe ou não valor em um cache chamado "DataAtual" | |
if (!_cache.TryGetValue("DataAtual", out DateTime dataAtual)) | |
{ | |
//Caso não exista, colocamos a data atual | |
dataAtual = DateTime.Now; | |
//Aqui configuramos as opções do cache | |
var cacheEntryOptions = new MemoryCacheEntryOptions() | |
.SetAbsoluteExpiration(TimeSpan.FromSeconds(5)); //Tempo de expiração do cache | |
//Salvando :) | |
_cache.Set("DataAtual", dataAtual, cacheEntryOptions); | |
} | |
ViewBag.DataAtual = dataAtual; | |
return View(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment