Redis(7)新增效能一哥List

Redis的List資料結構和c# 的LinkeList 很類似,對於Insert速度相當快(0(1)),

也可決定將item存放至左或右,但要注意搜尋一個很大的List效能相當不好(因為是循序),

由於Insert速度效能超好,所以實務上都會用來處理logs,因為logs絕對是寫入遠大於讀取。

下圖為List相關方法,但我這裡老樣子只示範CRUD。

 Program _Program = new Program();
            //_Program.Flushdb();
            StackExchangeRedisHelper _StackExchangeRedisHelper = new StackExchangeRedisHelper();
            using (conn = _StackExchangeRedisHelper.SafeConn)
            {
                IDatabase _IDatabase = conn.GetDatabase();
                var listKey = "RicolistKey";
                _IDatabase.KeyDelete(listKey, CommandFlags.FireAndForget);//delete all item

                _IDatabase.ListRightPush(listKey, "ricoisme".Select(x => (RedisValue) x.ToString()).ToArray());//put 8 item
                Console.WriteLine(_IDatabase.ListLength(listKey)); 

                var firstFour = _IDatabase.ListRange(listKey, 0, 3);
                Console.WriteLine(string.Concat(firstFour));

                var lastFive = _IDatabase.ListRange(listKey, -5);
                  Console.WriteLine(string.Concat(lastFive));

                _IDatabase.ListRemove(listKey, "i");  //remove i
                 Console.WriteLine(string.Concat(_IDatabase.ListRange(listKey)));

                Console.ReadLine();
}

 

參考

Data types