I am working on web api
project, my web api
is calling repository.Repository calls third party data source to perform CRUD.Calling the data source
is very costly, and it gets updated weekly.
So I thought to implement caching. I have seen few output caching packages, but it does not fulfill my requirement, because:
- If I output cache Get method, I am not able to use same Cache output in
GetById
method or the same cached data for some other operation like find opeartion. I have to also manually update cache when ever any update/post happens. - One more thing i am confused what to do in this scenario whether remove cache or update cache whenever put or post operation happens?
I am totally confused to complete this requirement.Please suggest me how to fulfill this requirement.I searched on web,but have not found anything like that.
I am novice both on SO and WebAPI so pardon me if question not fulfilling the standard
To use the cached data for different operations like GetById and Find you need to store data in different data structures. Caches like REDIS supports hashmap for objects which can be used by GetById. It totally depends on your case what kind DS you need to use.
To answer second part of your first question and this one I would say you need to choose between a writeback and write through cache. You can read more about WB and WT caches in this article. Basically there are two approaches
In terms of write / update latency option 1 is the fastest but has the risk of getting stale data. Option 2 will slow down both GET, PUT / POST operations and option 3 will slow down the write operations.
Your choice should depend on the ratio of read and write operations in the system. If the system you are designing is read heavy then option 3 is better than 2.