Beispiel #1
0
        public static Result GetCachedResult(ServiceItem serviceItem, string phrase, LanguagePair languagesPair, string subject)
        {
            if (!useCache)
            {
                return(new Result(serviceItem, phrase, languagesPair, subject));
            }

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

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

            ResultsHashtable collection;
            bool             collection_exists = true;

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

            int  hash = Result.GetHashCode(serviceItem.FullName, languagesPair, subject);
            bool needed_new_result = !collection_exists;

            Result 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 Result(serviceItem, phrase, languagesPair, subject);
                    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);
        }
Beispiel #2
0
        public static Result GetCachedResult(ServiceItem serviceItem, string phrase, LanguagePair languagesPair, string subject)
        {
            if(!useCache)
                return new Result(serviceItem, phrase, languagesPair, subject);

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

            ResultsHashtable collection;
            bool collection_exists = true;

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

            int hash = Result.GetHashCode(serviceItem.FullName, languagesPair, subject);
            bool needed_new_result = !collection_exists;

            Result 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 Result(serviceItem, phrase, languagesPair, subject);
                    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;
        }