I want to use AOP to apply cache functionality to all of method of BLL layer in my website.
Just like below:
[Cacheable(new string[] { "id" }, GroupName = "UserInfoByID", LiveTime = "00:05:00")]
public UserInfo GetUserInfoByID(int id)
{
return UserDAO.GetUserInfo(id);
}
The first argument of Cacheable attribute figures out which arg of method will be used to create cache key. The second argument GroupName figures out the prefix of cache key. As the above example, a key-“UserInfoByID(123)” will be generated (supposing the query id be 123). I create another attribute:
[PurgeCache(new string[] { "userInfo.ID" }, GroupName = "UserInfoByID")]
public void UpdateUserInfo(UserInfo userInfo)
{
UserDAO.UpdateUserInfo(userInfo);
}
It will use userInfo.ID and the same GroupName-UserInfoByID to generate a same key to purge cache: UserInfoByID(123). Is it every simple to use and elegant? There is, however ,a very tough problem. I have another method:
public void ManageUser(int[] userID, int status)
{
UserDAO.ManageUser(userID, status);
}
The method is used to change user’s status. How can I refresh cache after execution? Should I generate key with int[] userID one by one and purge all of them? Is it too complex in consideration of high performance cache, even though it’s just a very simple case. Supposing I have a list query mehod:
[Cacheable(new string[] { "regionID" }, GroupName = "UserList", LiveTime = "00:05:00")]
public List<UserInfo> GetUserList(int regionID)
{
List<UserInfo> result = UserDAO.GetUserList(regionID);
return result;
}
If I change user’s status to disactiviated, how can I refresh the cache of above method?
I built a small aop caching fw sometime ago, MbCache, where I decided to let invalidation be an “explicit” operation rather than a configuration issue. Made things a lot easier.
Even though this fw uses code configuration rather than attributes (I don’t really like the attribute thingy for different reasons – personal opinion), the same could be applicable here I guess. You should maybe remove your PurgeCacheAttribute and instead let that be some runtime operation/method?
This is how invalidation is made in MbCache, maybe that will give you some ideas?