Introducing Attribute Based Caching
"There are only two hard problems in computer science: cache invalidation, naming things, and off-by-one errors."
I am a big fan of postsharp and its creator Gael. There are a number of examples floating around of doing caching with postsharp. However, none handled cache invalidation in a nice declarative way.
- Configurable Cache Store (Options include In-process and Out-Of-Process and Off. Out-Of-Process uses Microsoft ApplicationServer.Caching)
- Declarative Caching & Declarative Cache Invalidation ( use method name or configurable “GroupName” plus method parameters to cache & trigger cache invalidation)
Simple Example( No invalidation):
[Cache.Cacheable] //this method now cached, will only be called once per guid
public SomeExpensiveObject GetExpensiveObject(Guid userId)
Full Featured Example:
//Get All Users is cached in Key = "GetAllUsers"
//GetUserById is cached using "GetUserById" + ID parameter
User GetUserById(int Id)
//Add user invalidates "GetAllUsers" cache key (User parameter is ignored)
void AddUser(User user)
//Delete user invalidates both GetAllUsers & GetUserById
//The user parameters Id property is used to build Key for "GetUserById"+ Id Key
//this is done using a bit reflection
void DeleteUser(User user)
This example shows how an expensive object (user in this case) might be cached at the repository level. It shows how attributes can be used to clear from the cache stale objects based on add update and delete changes in a declarative fashion.
Please excuse the empty method bodies for now. I will have a better sample project put together at some point.