public static GuessResult GetCachedGuessResult(LanguageGuesser serviceItem, string phrase)
        {
            if(!useCache)
                return new GuessResult(serviceItem, phrase);

            string key = phrase.Trim().ToLowerInvariant();
            if(key.Length > 300)
                key = key.Substring(0, 300);

            GuessResultsHashtable collection;
            bool collection_exists = true;

            lock(cache)
            {
                if(!cache.TryGetValue(key, out collection))
                {
                    collection = new GuessResultsHashtable();
                    cache.Add(key, collection);
                    collection_exists = false;
                }
            }

            int hash = serviceItem.FullName.GetHashCode();
            bool needed_new_result = !collection_exists;

            GuessResult res = null;

            lock(collection)
            {
                if(!needed_new_result)
                {
                    if(!collection.TryGetValue(hash, out res))
                        needed_new_result = true;
                    else
                    {
                        needed_new_result = (res.Error != null && !res.ResultNotFound) || res.Phrase != phrase;
                    }
                }

                if(needed_new_result)
                {
                    res = new GuessResult(serviceItem, phrase);
                    collection[hash] = res;
                    lock(results_history)
                    {
                        results_history.Add(res);
                    }
                }
                else
                {
                    res.LastUsed = DateTime.Now;
                    lock(results_history)
                    {
                        results_history.Remove(res);
                        results_history.Add(res);
                    }
                }
            }
            return res;
        }
Exemple #2
0
 public GuessResult(LanguageGuesser detectorItem, string phrase)
 {
     this.DetectorItem = detectorItem;
     this.phrase       = phrase;
 }
        public static GuessResult GetCachedGuessResult(LanguageGuesser serviceItem, string phrase)
        {
            if (!useCache)
            {
                return(new GuessResult(serviceItem, phrase));
            }

            string key = phrase.Trim().ToLowerInvariant();

            if (key.Length > 300)
            {
                key = key.Substring(0, 300);
            }

            GuessResultsHashtable collection;
            bool collection_exists = true;

            lock (cache)
            {
                if (!cache.TryGetValue(key, out collection))
                {
                    collection = new GuessResultsHashtable();
                    cache.Add(key, collection);
                    collection_exists = false;
                }
            }

            int  hash = serviceItem.FullName.GetHashCode();
            bool needed_new_result = !collection_exists;

            GuessResult res = null;

            lock (collection)
            {
                if (!needed_new_result)
                {
                    if (!collection.TryGetValue(hash, out res))
                    {
                        needed_new_result = true;
                    }
                    else
                    {
                        needed_new_result = (res.Error != null && !res.ResultNotFound) || res.Phrase != phrase;
                    }
                }

                if (needed_new_result)
                {
                    res = new GuessResult(serviceItem, phrase);
                    collection[hash] = res;
                    lock (results_history)
                    {
                        results_history.Add(res);
                    }
                }
                else
                {
                    res.LastUsed = DateTime.Now;
                    lock (results_history)
                    {
                        results_history.Remove(res);
                        results_history.Add(res);
                    }
                }
            }
            return(res);
        }