Quantcast
Channel: WCF, ASMX and other Web Services
Viewing all articles
Browse latest Browse all 555

Using cache in a web service

$
0
0

I have inherited a asmx web service that makes numerous database calls but doesn't cache any of the results.  Thus, each call to the web service hits the database.  I'd like to implement the use of caching to reduce the number of such calls and hopefully, improve overall performance.  Unfortunately, I've never used (or written) any type of caching logic.  Thus, I have several questions as follows:

1) How do I go about implementing cache logic in the web service?  I presume I should write a separate class but that is just a presumption as I've seen some examples which had cache logic written directly in a web method.  So, I'm looking for what would be the best practice for implementing cache logic into a web service.

2) One of the database queries checks for the existence of an  item and returns true if the item exists in the database. Otherwise, false is returned.  Would it be better to store the results of such a query in a dictionary (i.e. dictionary<string, boolean>) then cache the dictionary or could I simply use the item as the cache key and have the value be the boolean?  I like the idea of the latter because it seems easier but there could possibly be several thousand items resulting in several thousand cache keys.  Is that acceptable or would having so many cache keys cause issues?  I'm thinking not but haven't been able to find a definitive answer or whether this would go against best practices. 

3) Related to 2, if I need to use a dictionary, how could I add new items to the dictionary safely (i.e. how do I update cache in a thread safe manner)?  I've seen lots of postings showing how to read from cache or add a new value to it but not how to safely update an existing value.  

While I am sure I'll have more questions as I go along, hopefully, answers to the above will get me started down the right path.  Also, any examples, links, etc. are appreciated.

Thank you.


Viewing all articles
Browse latest Browse all 555

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>