Show how the redis works with .NET 5.
Here's a short video that explains the project and how it uses Redis:
- The AAPL's details - market cap of 2,6 trillions and USA origin - are stored in a hash like below:
- E.g
HSET "company:AAPL" symbol "AAPL" market_cap "2600000000000" country USA
- E.g
- The Ranks of AAPL of 2,6 trillions are stored in a ZSET.
- E.g
ZADD companyLeaderboard 2600000000000 company:AAPL
- E.g
- Top 10 companies:
- E.g
ZREVRANGE companyLeaderboard 0 9 WITHSCORES
- E.g
- All companies:
- E.g
ZREVRANGE companyLeaderboard 0 -1 WITHSCORES
- E.g
- Bottom 10 companies:
- E.g
ZRANGE companyLeaderboard 0 9 WITHSCORES
- E.g
- Between rank 10 and 15:
- E.g
ZREVRANGE companyLeaderboard 9 14 WITHSCORES
- E.g
- Show ranks of AAPL, FB and TSLA:
- E.g
ZSCORE companyLeaderBoard company:AAPL company:FB company:TSLA
- E.g
- Adding market cap to companies:
- E.g
ZINCRBY companyLeaderBoard 1000000000 "company:FB"
- E.g
- Reducing market cap to companies:
- E.g
ZINCRBY companyLeaderBoard -1000000000 "company:FB"
- E.g
- Companies over a Trillion:
- E.g
ZCOUNT companyLeaderBoard 1000000000000 +inf
- E.g
- Companies between 500 billion and 1 trillion:
- E.g
ZCOUNT companyLeaderBoard 500000000000 1000000000000
- E.g
[HttpGet("top10")]
public async Task<IActionResult> GetTop10()
{
return Ok(await _rankService.Range(0, 9, true));
}
public async Task<List<RankResponseModel>> Range(int start, int ent, bool isDesc)
{
var data = new List<RankResponseModel>();
var results = await _redisClient.SortedSetRangeByRankWithScoresAsync("REDIS_LEADERBOARD",start,ent, isDesc? Order.Descending:Order.Ascending);
var startRank = isDesc ? start + 1 : (results.Count() / 2 - start);
var increaseFactor = isDesc ? 1 : -1;
var items = results.ToList();
for (var i = 0; i < items.Count; i++)
{
var company = await GetCompanyBySymbol(items[i].Element);
data.Add(
new RankResponseModel
{
Company = company.Item1,
Country = company.Item2,
Rank = startRank,
Symbol = items[i].Element,
MarketCap = items[i].Score,
});
startRank += increaseFactor;
}
return data;
}
git clone https://github.com/redis-developer/basic-redis-leaderboard-demo-dotnet.git
REDIS_ENDPOINT_URL = "Redis server URI:PORT_NUMBER"
REDIS_PASSWORD = "Password to the server"
dotnet run
Static content runs automatically with the backend part. In case you need to run it separately, please see README in the client folder.