Home  /  Questions  /  Question

50   50
Sep 21, 2011

What pattern to use for caching?

Let's assume there is a requirement to cache calls to get products from database for N minutes. What design / patten would you recommend? In what layer it could be placed?

1,364   100.0
Oct 28, 2011
If you have the repository patterns at the DB layer then you can easily implement a cache repository and plugin via some IOC container or by poor man's dependency injection..

Basically its a combination of factory + strategy pattern.  Factory to create instance and strategy to select the algorithm like getting from cache or Db.

You can also use variation of the above pattern as well.

In case your DB layer is not that flexible the quickest thing would be to introduce a caching facade service which interacts with the DB layer based on cache settings....

90   96.5
Nov 01, 2011
Before delving into Design patterns, let us think architecture from resilience perspective.

At the application architecture level, I would recommend to focus on seperation of concenrs, that Data services layer should always do operations related to data.

For Design Patterns you have the answer from Rajesh, which is always within a specific layer.

From 3-tier perspective, you should have a layer to manage Caching, between business and data services layer.

That UI -> BSL-> Optionally (caching Layer)-> DB

Once the data is retrived by business layer, it may optionally can pass in to Caching layer, which will satisfy caching requirement.
Once Cached, then pass the data back to UI layer.

Hope this helps.