Skip to content

Instantly share code, notes, and snippets.

@vmussak
Created January 2, 2018 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vmussak/474115fecebf0b607d7a3c5ef2fe4492 to your computer and use it in GitHub Desktop.
Save vmussak/474115fecebf0b607d7a3c5ef2fe4492 to your computer and use it in GitHub Desktop.
Adicionando a hora atual no cache
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