jeudi 19 mars 2015

What is the design pattern name for a class that handles 2nd level caching?


I have a system where I want to start introducing some 2nd level caching. I want to abstract data access a bit and allow some type of other class to do the dirty work of fetching a DTO; it will check cache first and only go to database if it has to. Also, it might do some additional work on the DTO before passing it back, like applying some business rules, etc. Would this be considered the Proxy, Facade, or "Manager" design pattern? Here is an example:



class UserDTO {
int Id {get;set;}
string UserName {get;set;}
}

class UserXXX { // <<< What is this called? Proxy, Facade, Manager?
UserDTO GetUser(int userId) {
UserDTO user;

//get user: check cache first
string cacheKey = "user_" + userId.ToString();
if (cache.Exists(cacheKey)){
user = cache.Get(cacheKey);
} else{
user = db.Users.Find(userId);
//add user to cache for next time
cache.Add(cacheKey, user);
}

return user;
}
}




Aucun commentaire:

Enregistrer un commentaire