Question

I am working on classified ads web application using asp.net Mvc-3 C#.

Every ad can share on social media like facebook,twitter,google plus ,linkedin.

I am calculating how many times ad is share on these website and saving this record in database.

Using this function

 string jsonString = new System.Net.WebClient().DownloadString(URL-of-ad);
 var serialize= new System.Web.Script.Serialization.JavaScriptSerializer();
 var dictionary = serialize.Deserialize<Dictionary<string, string>>(jsonString);
 var twitter = dict["count"];
 var facebook= dict["share_count"];

// calculating for other website by this function

This is function is working fine but very time consuming for every ad .I have thousand of ads so i have to loop through thousand time and call this function for every ad.

I am calling this function twice in a day.

There is any other better and fast way to do this task?

Please give your suggestions.

Was it helpful?

Solution

You may think of improving your design

1 - Create a Windows Service and set it to run twice a day , most probably figure out when the traffic on your site is very minimal.

2- Move the Sharing count logic into windows service which will do it in another process and free up your main application.

3- Downloading something which is dependent upon the network speed so make yopur downloading Asynchronous rather than synchronous.

OTHER TIPS

A few suggestions:

  1. Run this in parallel, such as using Parrallel.ForEach, passing in the URL of the add, or an async version, such as DownloadStringAsync. Both will run multiple downloads at the same time.
  2. Run this when the page is loaded that shows the add. Load the add then do a web service call to the server to get the count and share_count. It may be a bit more work but it will be more up to date and, if each add is shown rarely, this may be more efficient.

I assume you are not running this during page creation but, if you are, move it to a separate service or process as Saurabh suggests.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top