public static long GetOccurences(string word, string langCode) { if (word.Contains(" ")) { word = "\"" + word + "\""; } // Check the cache first string query = word + " lang:" + langCode; if (GoogleOccurencesCache.IsInCache(query)) { long occurences = GoogleOccurencesCache.GetFromCache(query); return(occurences); } Console.WriteLine("Looking Google occurences count of {0} ({1})", word, langCode); for (int attempt = 0; attempt < MAX_ATTEMPTS; attempt++) { try { long occurences = TryToGetOccurences(word, langCode); GoogleOccurencesCache.AddToCache(query, occurences); return(occurences); } catch (Exception) { // Google search failed. Wait random time and try again Console.WriteLine( "Google search failed at attempt {0} of {1}.", attempt + 1, MAX_ATTEMPTS); Random rand = new Random(); int randomTime = rand.Next(MIN_DELAY_MS, MAX_DELAY_MS); Thread.Sleep(randomTime); } } throw new Exception(String.Format("After {0} attempts Google failed to " + "return the number of occurences for the word {1}.", MAX_ATTEMPTS, word)); }
public static long AsyncCalcOccurences(string word, string langCode) { if (queriesQueue == null) { // Create the queries queue and start the working threads queriesQueue = new Queue <WordAndLang>(); workingThreads = new Thread[MAX_THREADS_COUNT]; for (int i = 0; i < MAX_THREADS_COUNT; i++) { workingThreads[i] = new Thread(new ThreadStart(ProcessQueriesQueue)); workingThreads[i].Name = "Google.Query.Thread#" + i; workingThreads[i].Start(); } } if (word.Contains(" ")) { word = "\"" + word + "\""; } // Check the cache first string query = word + " lang:" + langCode; if (GoogleOccurencesCache.IsInCache(query)) { long occurences = GoogleOccurencesCache.GetFromCache(query); return(occurences); } // Add the query to a queue for asynchronous processing lock (queriesQueue) { WordAndLang wordAndLang = new WordAndLang(word, langCode); queriesQueue.Enqueue(wordAndLang); Monitor.Pulse(queriesQueue); } return(0); }