Is there a cleaner way of caching data
https://softwareengineering.stackexchange.com/questions/187575
-
05-11-2019 - |
Question
We've started implementing some caching code in our MVC project. We've derived from .Net's ObjectCache to store it in a SQL database.
What we do right now is in each method we want to cache we have code like this:
public StatusCounts GetCounts()
{
var cache = new SqlCache<StatusCounts>("mykey");
if(cache.HasCachedData())
return cache.GetCachedData();
var data = StatusRepository.GetCounts();
cache.SetData(data, DateTime.Now.AddHours(2));
}
As far as caching goes, it works. But my concern is it seems like now GetCounts has multiple responsibilities. It's no longer just calculating counts, it's getting them from a cache (if they exist) and saving them to a cache before returning.
Is there a cleaner way to do this?
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with softwareengineering.stackexchange