Pages

How to disable webclient caching in windows phone

Few months back, I was working with Silverlight for Windows Phone. Application makes an API call to get a list of items which were randomized on every new call to an API service. There was nothing to be done in this as the randomization logic of items was being handled by the server code.

The items were nothing but blocks of text so many a times it was hard (may be an excuse :p) to check if returned items are actually randomized or not! I always relied on the JSON output which I got in browser by manually hitting API endpoint. :O. I moved ahead and completed app so that we could give a demo, before starting work on rest of the things. So far so good, everything went well, 2 days later I got a mail stating items are not displayed in random order throughout the execution of app even if refresh button (this was provided so that user can see randomized items.. lol :D) . They were randomized only when app was re-launched! Huhhhhh....

I was surprised and kept thinking what could be the reason behind this. Because calling an API, parsing response and displaying it, is not a rocket science, be it any platform or language. Started debugging relevant code and what I observed was really interesting. For the fresh new request (say, http://xyz.com/api/items/), it took 1-2 seconds to hit Webclient.OpenReadCompleted event handler. However on subsequent requests (say, i clicked refresh button) to same URL, the event handler was hit almost immediately and the items were being returned in same order as that of returned for first request.

The reason this happens is Webclient caches URLs and responses (think of how you maintain key-value pairs using Dictionary in C#, well but I don't know internally how Silverlight does that for url-response!), and if request to the same URL is made, it does not reaches server instead returns results from the cache for a given URL. Remember this caching is scoped to the current execution of application only meaning if you close the app cache is cleared.

Is it a flaw?:

Its not a flaw. It is by design.

Workaround:

One thing that could be done on Server side is to disallow browsers to cach the response for given URL. However you don't always have access to server or at least you are not the one who designed API.

So here's a workaround. Yes that's just a work-around and not a solution. Simply pass an extra parameter to the API calls. This parameter must be unique for each new request of same URL. For example, instead of making request to http://xyz.com/api/items/, i requested http://xyz.com/api/items/?rnd=[guid]. In my case, i used GUID that is guaranteed to be unique every time. You may use complete datetime value (including milliseconds) etc.

Note that new key name can be anything (here, rnd), but it should not be the one from the set of keys accepted by API code. Almost all the APIs are designed in such a way that they ignore keys which are not relevant for the processing. So passing and extra key-value pair does not harm API behavior. (Cool!)

Hope this helped you!

2 comments:

  1. Thank you

    This is very good post by you it's help me lot's in windows phone I think why API return same result after refresh so I just pass one changeable value query string with that and my problem solved.

    ReplyDelete
    Replies
    1. Make sure you apply this workaround only in appropriate cases and not always. For example, you would not want to append that parameter to request url when user is navigating back (using h/w back button) to the same screen. In this case you would display the same results which were fetched previously.

      Delete