Best Solution for Caching

后端 未结 9 683
走了就别回头了
走了就别回头了 2020-12-13 11:57

Where is the best place to implement caching in a web based app?

  • At the presentation layer (hope not)?
  • At the Business Logic Layer?
  • At the da
相关标签:
9条回答
  • 2020-12-13 12:00

    If you're caching business objects, and given the 3 tiers you describe you're using, the business layer is the obvious place.

    A simple setup would be (for a single business object):

    1. MyCache is a static class
    2. One static method: MyCache.Retrieve(id), returns an object
    3. The class uses a static Dictionary for storage, and a static ReaderWriterLock

    The Retrieve method then performs the following:

    1. Is the id in your cache (a Dictionary of id/object)?
    2. No
      • Grab the object from the database
      • AquireWriterLock from the ReaderWriterLock (why ReaderWriter? because you're not writing frequently but you are reading often)
      • Add the object to the cache
    3. Yes
      • Retrieve the object from the dictionary

    That's my prefered solution, you can add timeouts, clearing and so on if you need. Or use memcached or the Microsoft Enterprise Library Caching Block but they're usually overkill.

    0 讨论(0)
  • 2020-12-13 12:01

    Data layer. But it is confusing because we use ASP.NET caching. With its expiration and dependency capability, it's pretty handy. So our business layer has no idea the data layer might be caching but the data layer uses a presentation-layer technology for caching! :(

    0 讨论(0)
  • 2020-12-13 12:02

    I can't speak for ASP.NET specific stuff, but I've generally found that you can cache at each level. Your data tier will cache data responses, your presentation layer may find the need to cache generated presentation elements (e.g., user-specific style sheets), etc. In most cases, the data tier is the heaviest cache user and the other tiers will include an object cache if one is proven to be necessary.

    The hardest part is getting cache invalidation working correctly. Stale caches are a major headache during deployment. Make sure to figure out how you are going to manage the lifetime of each cache object. LRU caches work well for caching static elements based on demand. Most data caches, however, will need an explicit life cycle whether it be based on an expiration timer or tied to some sort of a user session.

    0 讨论(0)
  • 2020-12-13 12:09

    We implement it at the business logic layer between interactions with the presentation layer. If a request from the presentation layer comes in and it is cached we can then skip a whole lot of logic and data access.

    I'd highly recommend MemCached over MS Velocity, Velocity was a pain to setup!

    0 讨论(0)
  • 2020-12-13 12:09

    Cache: implemented at the data access layer, accessed by the Business logic layer.

    this mean you must watch out carefully for the cache going stale.

    0 讨论(0)
  • 2020-12-13 12:10

    Data Layer - all layers above this layer don't need to know where the data is sourced from. I would also only cache data thats not very volatile, or include some expiration policy.

    0 讨论(0)
提交回复
热议问题