Web Service Cache For Static Values

Is there any reason to cache a service WebMethod like this that returns a non-database-required response? This service call will be used by mobile app to retrieve codes once a day. Assume 1 million users for server load. The cache is currently set to one hour.

<WebMethod(CacheDuration:=3600)> _
   Public Function GetIDs() As String
      Dim myArray(4) As Object 

      myArray(0) = New With {Key .id = "one", .name = "1", .passcode = ""}
      myArray(1) = New With {Key .id = "two", .name = "2", .passcode = ""}
      myArray(2) = New With {Key .id = "three", .name = "3", .passcode = ""}
      myArray(3) = New With {Key .id = "four", .name = "4", .passcode = ""}
      myArray(4) = New With {Key .id = "five", .name = "5", .passcode = ""}

      Dim js As JavaScriptSerializer = New JavaScriptSerializer()
      Dim sJSON As String = js.Serialize(myArray)

      Return sJSON
   End Function

Answers


Yes.

My intuition says "yes". Serialization costs cpu time, and caching hits happen early in the pipleline, so you should definitely see improvements when caching these requests, even if they don't hit the database. Storing data like this in cache only uses an inconsequential amount of memory, so any improvement is good enough.

I'm sure you don't trust my intuition though, and you have no reason to. I wrote up a little test webservice that has your test method (identical), and a non-cached version to compare (identical but for the CacheDuration property). I used the following client to compare performance between the two methods:

static void Main(string[] args)
{
    var stopwatch = new Stopwatch();
    var tasks = new List<Task>();

    stopwatch.Start();

    for (var x = 0; x < 10; x++)
    {
        tasks.Add(Task.Factory.StartNew(() =>
        {
            using (var service = new localService.TestService())
            {
                for (var i = 0; i < 100; i++)
                {
                    var ids = service.GetIDs();
                }
            }
        }));
    }

    // completion order should be about the same as order in the list
    // let's not worry too much
    tasks.ForEach(d => d.Wait());

    stopwatch.Stop();

    var timeElapsed = stopwatch.ElapsedMilliseconds;

    Console.WriteLine(timeElapsed);
}

Results on my little old laptop are:

  • non cached method: 8 seconds
  • cached method: 1.5 seconds

I did 5 runs each and averaged the time. Your measurements may turn out to not show this big a difference. It still seems like enough reason to use caching on this method.

Also: if the method does not hit the database, I guess the values are compiled into the assembly. In that case I see no reason to set the cacheDuration to only one hour: set it to its maximum. When you replace the assembly the application pool recycles and the cache needs to be rebuilt anyway.


Need Your Help

div doesn't fully expand to content

html css height

I'm not a css expert but I'm running out of ideas what could trigger this unwanted behaviour. I'm want to use a div as background for the main section of my site. Once there is a lot of content in ...

automatically play next soundcloud

javascript jquery youtube widget soundcloud

I'm currently working on my music blog, and I'm trying to make the widgets start when previous one finishes. I have both soundcloud and youtube widgets, but the priority is to implement this

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.