In-memory cashing for an auto-complete list

When implementing an auto-complete control on your site, you may want to cache the results to keep the database queries to a minimum. Here’s a quick way to do that:

        public static Dictionary AutoCompleteHashList = new Dictionary();
        const string resultsFormatString ="{0}|{1}rn";
        private static string GetSuggestedResults(string s)
            const int maxHashLengthToCache = 9;
            if (s.Length 
                            results.AppendFormat(resultsFormatString, recipe.Key, recipe.Value)
            if (s.Length < maxHashLengthToCache)
                AutoCompleteHashList.Add(s, results.ToString());
            return results.ToString();

Leave a Reply