Basic Redis Caching Demo
This app returns the number of repositories a Github account has. When you first search for an account, the server calls Github's API to return the response. This can take 100s of milliseconds. The server then adds the details of this slow response to Redis for future requests. When you search again, the next response comes directly from Redis cache instead of calling Github. The responses are usually usually in a millisecond or so making it blazing fast.
How it works?
1. How the data is stored:
- Set the number of repositories for the account (use the user name for key):
SETEX <account name> <seconds till expire> <number of public repos>
- E.g
SETEX microsoft 3600 197
- E.g
Code example:
var data = new ResponseModel {
Repos = gitData.PublicRepos.ToString(), Username = username, Cached = true
};
await distributedCache.SetStringAsync($"/repos/:{username}",
JsonConvert.SerializeObject(data),
new DistributedCacheEntryOptions() {
AbsoluteExpiration = DateTimeOffset.UtcNow.AddSeconds(3)
});
2. How the data is accessed:
- Get number of public repositories for an account:
GET <account name>
- E.g
GET microsoft
- E.g
Code example:
var cache = await distributedCache.GetStringAsync($"/repos/:{username}");
if (string.IsNullOrEmpty(cache))
{
// ...
How to run it locally?
Write in environment variable or Dockerfile actual connection to Redis:
PORT = "API port"
REDIS_ENDPOINT_URL = "Redis server URI"
REDIS_PASSWORD = "Password to the server"
Run frontend (in ClientApp folder)
yarn
yarn serve
Run backend
dotnet run
Open in your berwser: localhost:5000