Skip to content

Instantly share code, notes, and snippets.

@Chriz76
Created July 10, 2021 19:52
Show Gist options
  • Save Chriz76/ceccec7740ec5dd65a9df89a07db5d4e to your computer and use it in GitHub Desktop.
Save Chriz76/ceccec7740ec5dd65a9df89a07db5d4e to your computer and use it in GitHub Desktop.
public class UserWithPostCount
{
public int ID { get; set; }
public string Name { get; set; }
public int PostCount { get; set; }
}
public IEnumerable<UserWithPostCount> ReadTopUsers(int offset, int countCategories)
{
var cache = _muxer.GetDatabase();
var topUsers = cache.SortedSetRangeByRankWithScores("UsersByPostCount", offset, offset + countCategories - 1, Order.Descending);
var ret = new List<Dto.UserWithPostCount>();
var keys = new List<RedisKey>();
foreach (var e in topUsers)
{
string redisId = e.Element.ToString();
keys.Add(new RedisKey(redisId + ":Name"));
}
var names = cache.StringGet(keys.ToArray());
for (int i = 0; i < names.Length; i++)
{
int.TryParse(topUsers[i].Element.ToString().Substring(5), out int id);
ret.Add(new Dto.UserWithPostCount { ID = id, PostCount = (int)topUsers[i].Score, Name = names[i] });
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment