简体   繁体   中英

How to use caching with WCF

I'm developing a WCF that will return JSON data. The method will take a parameter and return with data from a database.

If the load on the WCF is high, it would be great to use some caching, to avoid having to dig in to the db all the time.

EDIT: The method will return recent items. So the method should return the same response for all request that provides the same time intervall as a parameter.

How can this be done efficiently? I've read a litle about Basic Web HTTP Service Caching, but I'm not sure if this is the best solution.

Thanks a lot.

Personally, I like to make my WCF services session-ready and store my caches in the session. The trade-off is always between how static the data is, and how much data needs to be pulled down.

For very static data, I may cache for the length of the session, or until the data has changed (determining if a different user has changed my session's cached data is a different story). For very dynamic data, I find it hard to trust my cache longer than a few minutes (which still isn't completely safe).

The question is though, are you talking about client side caching or server side caching? Meaning are you trying to cache datasets on the client, or just cache on the web server that has pulled the dataset from the database server?

If you're talking about client caching, then you can cache in cookies, cache in javascript variables, use http caching, or the aforementioned HTML 5 application caching mechanism.

It's all about the right tool for the job though, there is no hammer in this case ;)

You could use HTTP Cache Control: http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html

Or the HTML 5 Application Cache: http://www.html5rocks.com/en/tutorials/appcache/beginner/

How will your clients consume the WCF Service? Via jQuery AJAX requests? Standard GET requests?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM