Skip to content

Instantly share code, notes, and snippets.

Created September 26, 2019 09:34
What would you like to do?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Caching.Distributed;
using System.Text;
namespace RedisTest.Controllers
public class RedisController : Controller
private readonly IDistributedCache _distributedCache;
public RedisController(IDistributedCache distributedCache)
_distributedCache = distributedCache;
public string Get()
var cacheKey = "TheTime";
var currentTime = DateTime.Now.ToString();
var cachedTime = _distributedCache.GetString(cacheKey);
// cachedTime = "Expired";
// Cache expire trong 5s
var options = new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(5));
// Nạp lại giá trị mới cho cache
_distributedCache.SetString(cacheKey, currentTime, options);
cachedTime = _distributedCache.GetString(cacheKey);
var result = $"Current Time : {currentTime} \nCached Time : {cachedTime}";
return result;
Copy link

Bathe0603it commented Oct 28, 2021


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment