Redis连同Jquery UI Autocomplete


    
    
public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string searchDbInforItem = context.Request.Form["searchDbInforItem"];
            //string searchDbInforItem = "龙血竭胶囊";

            List> result = new List>();
            using (var redisClient = PooledRedisHelper.GetClient())
            {
                if (redisClient.DbSize == 0)
                {
                    RedisClient client = new RedisClient("127.0.0.1", 6379);
                    client.FlushAll();
                    //查出所有数据。
                    List list = Products.GetSearchKeyList();
                    for (int i = 0; i < list.Count; i++)
                    {
                        //逐字分割取出来的数据,插入到Redis缓存中
                        string[] keys = new string[list[i].prd_title.Length];
                        for (int j = 0; j < list[i].prd_title.Length; j++)
                        {
                            keys[j] = list[i].prd_title.Substring(j, 1);
                            client.AddItemToSortedSet(keys[j], list[i].prd_title);//这儿有个bug并没有存取所有的数据到Redis中。暂时没有找到原因
                            client.ExpireEntryAt(keys[j], DateTime.Now.AddDays(10));
                        }
                        client.AddItemToSortedSet(list[i].prd_title, list[i].prd_title);
                        client.ExpireEntryAt(list[i].prd_title, DateTime.Now.AddDays(10));
                    }
                    List returnResult = redisClient.GetRangeFromSortedList(searchDbInforItem, 0, 20);
                    if (returnResult != null)
                    {
                        for (int i = 0; i < returnResult.Count; i++)
                        {
                            string title = returnResult[i];
                            result.Add(new KeyValuePair(title, title));
                        }
                        List returnList = new List();
                        foreach (var item in result)
                        {
                            returnList.Add(item.Value);
                        }
                        JavaScriptSerializer json = new JavaScriptSerializer();
                        string returnJson = json.Serialize(returnList);
                        context.Response.Write(returnJson);
                    }
                }
                else
                {
                    List returnResult = redisClient.GetRangeFromSortedList(searchDbInforItem, 0, 20);
                    if (returnResult != null)
                    {
                        for (int i = 0; i < returnResult.Count; i++)
                        {
                            string title = returnResult[i];
                            result.Add(new KeyValuePair(title, title));
                        }
                        List returnList = new List();
                        foreach (var item in result)
                        {
                            returnList.Add(item.Value);
                        }
                        JavaScriptSerializer json = new JavaScriptSerializer();
                        string returnJson = json.Serialize(returnList);
                        context.Response.Write(returnJson);
                    }
                }
            }
        }

引用到了一个类

 // 数据库
        public static int InitialDb = 1;
        private static PooledRedisClientManager instance = null;
        public static PooledRedisClientManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new PooledRedisClientManager(new string[] { "127.0.0.1:6379" });//注意这儿直接写IP和端口就行了。
                }

                return instance;
            }
        }

        static PooledRedisHelper()
        {
        }

        public static IRedisClient GetClient()
        {
            return Instance.GetClient();
        }
    }

Redis连同Jquery UI Autocomplete_第1张图片

引用到的Redis dll。

参考:

    https://www.cnblogs.com/hnsongbiao/p/5102795.html

    https://my.oschina.net/zimingforever/blog/63877/


你可能感兴趣的:(.NET,MVC)