How to implement caching in C# Windows Application RRS feed

  • Question

  • Hi ,


    I am creating C# Windows Application that takes data from Web Service and show it on Windows forum.

    As Web Service takes lot of time to fetch data so i want to cache it.


    Can you please let me know i want to cache it.


    Can you please let me know how to implement that.



    Jaspreet Singh

    Thursday, July 28, 2011 3:23 PM


All replies

  • The specifics depend on the type and amount of data.  You could create a custom class, a collection of classes, etc.  The results would be cached in memory while the program is running.  If you want to preserve the data in some sort of storage like a file in the hard drive, you can use .Net serialization (XML or binary) which are relatively simple to use; if storing to a local database, you just write an intermediary class or method inside the data class that can connect and write to the database tables, etc.

    If you provide specifics, we can provide specifics too.

    • Proposed as answer by Jie Bao Friday, July 29, 2011 3:35 AM
    Thursday, July 28, 2011 3:38 PM
  • HI,


    I have a Windows Based application that contains a Tab Control.

    There are three tabs that shows information to user based on e-mail id selected in the previous form drop down list.


    Now this Tab Control takes and shows data by calling different web services 4 times.


    What i want is Second time when same persons e-mail address was selected from previous form...


    Tab Control will show data using Cachaing method...rather than taking data by calling Web Service again.


    Do you get my requirement???.


    Jaspreet Singh


    Thursday, July 28, 2011 4:01 PM
  • I kind of get it.  So you want an indexed cache, possibly using the email ID as key.

    Attempt 1:  Create a collection class from System.Collections.ObjectModel.KeyedCollection and make the key of type int (I suppose), and the type of items would be whatever class you create to hold the data collected from the 4 calls to web services.

    Every time you open a certain email ID, you look it up in the collection.  If not there, you lookup the information from the web services and store them in the collection.  Next time this email ID is requested, the data will be there in the collection.  Voilá!  You have a memory cache.

    • Proposed as answer by Adam_Turner Thursday, July 28, 2011 4:31 PM
    Thursday, July 28, 2011 4:08 PM
  • Hi,


    Is there any other way i can acheive it using System.Web and System.Web.Caching class..as data is coming from Web Service.


    I don't want to make repetitive calls for search for same e-mail to web service.



    jaspreet Singh

    Thursday, July 28, 2011 5:24 PM
  • I guess you could too.  Reading http://msdn.microsoft.com/en-us/library/system.web.caching.aspx, I see that the Cache class is a hashed collection.  Use this as your cache store.  It has expiration capability and probably other useful stuff that my basic setup above did not have.
    • Proposed as answer by Jie Bao Friday, July 29, 2011 3:45 AM
    • Marked as answer by Jie Bao Thursday, August 4, 2011 4:05 PM
    Thursday, July 28, 2011 5:30 PM
  • Additional to webJose, System.Web.Caching.Cache Class is a collection, why not to implement your own cache struct by the basic collection class (System.Collections.Hashtable Class). If we can try to design the cache stauct, it will be flexible and we can add some features on it.


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, July 29, 2011 3:45 AM