开发者

.net Artchitecture that works like - Cache everything, read from cache

开发者 https://www.devze.com 2023-01-03 00:55 出处:网络
Hi I want a sample that does following: Database <-> Data Access + Cache <-> Business logic <-> UI

Hi I want a sample that does following: Database <-> Data Access + Cache <-> Business logic <-> UI

so basically everything you want from database should be accessible from cache, if it's not in cache, underlying data access layer will populate if and return it otherwise retur开发者_如何学运维ned from cache

is there any disadvantage? in what scenerios this could be a good solution


I like creating my own static wrapper class for the System.Web.Caching.Cache class.

Essentially you create a class in your web application module, and create all the standard Cache functions (get, add, remove, etc). The methods need to be implemented with generics to ensure type safety.

Here is a good example

You then create another static class, which acts as like a service model from your web tier through to your data tier.

Your web tier would invoke methods on the static class, which would first generate a CacheKey based on the supplied method parameters, check cache, if found return, otherwise call data layer, add to cache and return.

Depending on how your business objects are setup, your might need to provide deep copies (ie implement IClonable and ovveride the Clone method) on your objects.

Also, your cache solution depends on your web farm architecture. If you have lots of web servers, the chances are your data could become stale so you need to decide on the best option there (SQLCacheDependecy, Distributed Caching, etc).


The obvious disadvantages are cache validity (how do you know that the data was not changed/added since you cached it) and memory/disk usage.

It is a good solution when your data is static (no need to think when to update cache).

We used a similar approach with dynamic data and cache introduced quite a number of problems. Sometimes cache updates were too expensive (the server had to notify all clients about the data which they cached and which has been changed), sometimes memory usage on clients was too high.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号