What is best way to use the Caching library in performance view. I have a lot of data in my db tables that i want to cache.
Lets say i have 3 tables named.
- AssignmentHead
- AssignmentRow
- Items
Today iam storing the tables as fallows:
private CacheManager _manager = _manager = CacheFactory.GetCacheManager();
I’m having one CacheManger and i stores the data like this.
_manager.add("Heads",ListofAssignmentHeads);
_manager.add("Rows",ListofAssigmmentRows);
_manager.add("Items",ListOfItems);
When retrieving data like Heads i do fallowing:
_manager.GetData("Heads");
Then i’m getting a list of heads and using linq to query what i want. But is this the best way of doing this or is it better to set up 3 CacheManager for each table and insert the primary key and then the object.
_headManager.add("1",HeadObject);
I really need your advises on these one. Thanks for the help.
Personally, all things being almost equal, I would cache the data in the format that I will need it at runtime. That way you can just get the information you need directly in the representation that you need. In your example that would be multiple CacheManagers with the primary key as the cache key.
It looks like you already have methods that return the lists and you are caching the lists. I would rather iterate over the list once when the data is loaded and then perform a lookup by key than iterate the list on every call. If your lists are large you may see a performance hit if you have to search the lists for every request since finding the element in the list is O(N) vs. O(1) for a cache with no hash collisions.
Of course, if you want to support dynamic lists then you might prefer to add lists as needed to a single CacheManager since creating dynamic CacheManagers is a bit of work.