Skip to content

Multiple cache keys for single object #305

Answered by jodydonetti
ivann14 asked this question in Q&A
Discussion options

You must be logged in to vote

Another thing to consider as a potential optimization is this:

private User GetUserByGuid(Guid guid, User? user) {
  return cache.GetOrSet<User>($"userByGuid:{guid}",_ => user ?? GetFromDbByGuid(guid));
}

public User GetUserByGuid(Guid guid) {
  return GetUserByGuid(guid, null));
}

public User GetUserById(int id) {
  User? user = null;
  var guid = cache.GetOrSet<Guid>($"userGuidById:{id}",_ => (user = GetFromDbById(id))?.Guid);
  return GetUserByGuid(guid, user); // THIS WILL STILL USE THE CACHE VIA THE GetUserByGuid
}

public User GetUserByEmail(string email) {
  User? user = null;
  var guid = cache.GetOrSet<Guid>($"userGuidByEmail:{email}",_ => (user = GetFromDbByEmail(email))?.Guid);

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@ivann14
Comment options

@jodydonetti
Comment options

@jodydonetti
Comment options

Answer selected by ivann14
@ivann14
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants