1. 程式人生 > >Redis簡單介紹以及數據類型存儲

Redis簡單介紹以及數據類型存儲

博客 個數 取值 rom 特點 而且 ring oid wan

因為我們在大型互聯網項目其中。用戶訪問量比較大,比較多。會產生並發問題,對於此。我們該怎樣解決呢。Redis橫空出世,首先,我們來簡單的認識一下Redis。具體介紹例如以下所看到的:

Redis是一個開源的。使用C語言編寫。面向“鍵/值”對類型數據的分布式NoSQL數據庫系統,特點是高性能。持久存儲,適應高並發的應用場景。Redis純粹為應用而產生,她是一個高性能的Key-Value數據庫,而且操作了多種語言的API性能測試將誒過表示SET操作每秒鐘可達110000次。GET操作每秒81000次。當然不同的server配置性能不同,redis眼下提供五種數據類型,string(字符串),list(鏈表),hash(哈希),set(集合)及zset(sorted set)有序集合。

說到Redis,小夥伴們可能會想到Memcached,小編給小夥伴們推薦一篇文章。文中具體的描寫敘述了Redis和Memcached,有興趣的小夥伴能夠點擊相關鏈接,進行查看哦`(*∩_∩*)′!

在前面的介紹中。我們知道,redis眼下提供五種數據類型。string(字符串)、list(鏈表)、hash(哈希)、set(集合)、zset(sorted set)有序集合,那麽這些是怎樣進行存儲的呢?以下小編來具體的介紹一下。

第一步,我們須要新建一個空白項目,例如以下圖所看到的:

技術分享
第二步,加入一個控制臺應用程序。例如以下圖所看到的:

技術分享
第三步,有三個dll文件,redis為c#開放的API,我們就是通過她來操作redis,為此,我們須要引用dll文件。點擊瀏覽,例如以下圖所看到的:

技術分享

第四步,找到須要引用的dll文件。例如以下圖所看到的:

技術分享

接著,我們就來看怎樣對數據類型進行存儲,首先我們來看String類型。String類型是最經常使用的一種數據類型,普通的key/value存儲都能夠歸為此類,一個key相應一個value,string類型二進制的。Redis的string能夠包括不論什麽數據,比方jpg或者序列化的對象,我們來看具體的代碼該怎樣編寫。代碼例如以下所看到的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServiceStack.Redis;

namespace RedisApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //字符串類型
            var client = new RedisClient("local", 6379); //首先new一個客戶端
            client.Set<string>("name", "laowang");       // 存儲字符串類型
            string userName = client.Get<string>("name");  //通過get取值
            Console.WriteLine(userName);
            Console.ReadKey();

         }
    }
}

接著,我們來看哈希表的存儲,hash是一個string類型的field和value的映射表。hash特別適合存儲對象,相對於講對象的每一個字段存成單個string類型。一個對象存儲在hash類型中會占用更少的內存。而且能夠更方便的存取整個對象。

作為一個key value存在,非常多開發人員自然的使用set/get方式來使用Redis。實際上這並非最優化的用法,尤其在未啟用VM情況下,Redis全部數據須要放入內存,節約內存尤其重要,我們來看具體的代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServiceStack.Redis;

namespace RedisApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //字符串類型
            var client = new RedisClient("local", 6379); //首先new一個客戶端
            client.Set<string>("name", "laowang");       // 存儲字符串類型
            string userName = client.Get<string>("name");  //通過get取值
            Console.WriteLine(userName);
            Console.ReadKey();

            //哈希存儲結果
            client.SetEntryInHash("userinfoId", "name", "zhangsan");
            client.GetHashKeys("userinfoId"); //獲取全部的key
            client.GetHashValues("userinfoId"); //獲取全部的值

          }
    }
}
我們再來看list類型。list是一個鏈表結構的,主要功能是push。pop獲取一個範圍的左右的值等,操作中key理解為鏈表名稱。Redis的list類型事實上就是一個每一個字元素都是string類型的雙向鏈表。我們能夠通過push,pop操作從鏈表的頭部或者尾部加入刪除元素,這樣list既能夠作為棧。也能夠作為隊列。Redis list的實現為一個雙向鏈表。既能夠支持反向查找和遍歷。更方便操作,只是帶來了部分額外的內存開銷,Redis內部的非常多實現。包括發送緩沖隊列等也都是用的這個數據結構,代碼例如以下所看到的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServiceStack.Redis;

namespace RedisApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //字符串類型
            var client = new RedisClient("local", 6379); //首先new一個客戶端
            client.Set<string>("name", "laowang");       // 存儲字符串類型
            string userName = client.Get<string>("name");  //通過get取值
            Console.WriteLine(userName);
            Console.ReadKey();

            //哈希存儲結果
            client.SetEntryInHash("userinfoId", "name", "zhangsan");
            client.GetHashKeys("userinfoId"); //獲取全部的key
            client.GetHashValues("userinfoId"); //獲取全部的值

            //隊列使用
            client.EnqueueItemOnList("name1", "laowang");//入隊
            client.EnqueueItemOnList("name1", "laoma");//入隊
            int length = client.GetListCount("nama1");
            for (int i = 0; i < length; i++)
            {
                Console.WriteLine(client.DequeueItemFromList("name1")); //出隊
            }

            //棧的使用
            client.PushItemToList("name2", "laowang");//入棧
            client.PushItemToList("name2", "laoma");
            int lentgh = client.GetListCount("name2");
            {
                Console.WriteLine(client.PopItemFromList("name2"));//出棧
            }

        }
    }
}
Set類型
她是string類型的無序集合。set是通過hash table實現的。加入、刪除和查找,對集合我們能夠取並集、交集、差集。對Set類型進行操作。例如以下所看到的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServiceStack.Redis;

namespace RedisApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //字符串類型
            var client = new RedisClient("local", 6379); //首先new一個客戶端
            client.Set<string>("name", "laowang");       // 存儲字符串類型
            string userName = client.Get<string>("name");  //通過get取值
            Console.WriteLine(userName);
            Console.ReadKey();

            //哈希存儲結果
            client.SetEntryInHash("userinfoId", "name", "zhangsan");
            client.GetHashKeys("userinfoId"); //獲取全部的key
            client.GetHashValues("userinfoId"); //獲取全部的值

            //隊列使用
            client.EnqueueItemOnList("name1", "laowang");//入隊
            client.EnqueueItemOnList("name1", "laoma");//入隊
            int length = client.GetListCount("nama1");
            for (int i = 0; i < length; i++)
            {
                Console.WriteLine(client.DequeueItemFromList("name1")); //出隊
            }

            //棧的使用
            client.PushItemToList("name2", "laowang");//入棧
            client.PushItemToList("name2", "laoma");
            int lentgh = client.GetListCount("name2");
            {
                Console.WriteLine(client.PopItemFromList("name2"));//出棧
            }

            //對Set類型進行操作
            client.AddItemToSet("a3", "ddd");
            client.AddItemToSet("a3", "ccc");
            client.AddItemToSet("a3", "ttt");
            client.AddItemToSet("a3", "sss");
            client.AddItemToSet("a3", "hhh");
            System.Collections.Generic.HashSet<string> hashset = client.GetAllItemsFromSet("a3");
            foreach (string str in hashset)
            {
                Console.WriteLine(str);
            }

        }
    }
}
Sorted Set類型
Sorted set是set的一個升級版本號。她是在set的基礎撒花姑娘添加了一個順序的屬性。這一個屬性在加入改動。元素的時候能夠指定,每次指定後。zset(表示有序集合)會自己主動又一次按新的值調整順序,能夠理解為有序列的表,一列存value。一列存順序。操作中key理解為zset的名字。

Redis sorted set的使用場景與set相似。差別是set不是自己主動有序的,而sorted set能夠通過用戶額外提供一個優先級(scord)的參數來為成員排序,而且是插入有序的,即自己主動排序。當你須要一個有序的而且不反復的集合列表。那麽能夠選擇sorted set數據結構。代碼例如以下所看到的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServiceStack.Redis;

namespace RedisApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //字符串類型
            var client = new RedisClient("local", 6379); //首先new一個客戶端
            client.Set<string>("name", "laowang");       // 存儲字符串類型
            string userName = client.Get<string>("name");  //通過get取值
            Console.WriteLine(userName);
            Console.ReadKey();

            //哈希存儲結果
            client.SetEntryInHash("userinfoId", "name", "zhangsan");
            client.GetHashKeys("userinfoId"); //獲取全部的key
            client.GetHashValues("userinfoId"); //獲取全部的值

            //隊列使用
            client.EnqueueItemOnList("name1", "laowang");//入隊
            client.EnqueueItemOnList("name1", "laoma");//入隊
            int length = client.GetListCount("nama1");
            for (int i = 0; i < length; i++)
            {
                Console.WriteLine(client.DequeueItemFromList("name1")); //出隊
            }

            //棧的使用
            client.PushItemToList("name2", "laowang");//入棧
            client.PushItemToList("name2", "laoma");
            int lentgh = client.GetListCount("name2");
            {
                Console.WriteLine(client.PopItemFromList("name2"));//出棧
            }

            //對Set類型進行操作
            client.AddItemToSet("a3", "ddd");
            client.AddItemToSet("a3", "ccc");
            client.AddItemToSet("a3", "ttt");
            client.AddItemToSet("a3", "sss");
            client.AddItemToSet("a3", "hhh");
            System.Collections.Generic.HashSet<string> hashset = client.GetAllItemsFromSet("a3");
            foreach (string str in hashset)
            {
                Console.WriteLine(str);
            }

            //Sorted Set類型
            client.AddItemToSortedSet("a5", "ffff");
            client.AddItemToSortedSet("a5", "bbbb");
            client.AddItemToSortedSet("a5", "gggg");
            client.AddItemToSortedSet("a5", "cccc");
            client.AddItemToSortedSet("a5", "aaaa");
            System.Collections.Generic.List<string> list = client.GetAllItemsFromSortedSet("a5");
            foreach (string str in list)
            {
                Console.WriteLine(str);
            }

        }
    }
}
小編寄語:該博客。小編主要簡單的介紹了一下Redis和數據存儲的類型,在redis中另一個非常重要的事兒,幾乎忘了,文件並發(日誌處理),多線程操作同一個文件時會出現並發問題,解決的一個辦法就是給文件加鎖(lock),可是這種話,一個線程操作文件時,其它的都得等待,這種話性能非常差,另外一個解決方式。就是先將數據放在隊列中,然後開啟一個線程,負責從隊列中取出數據,再寫到文件裏。

對於這塊的內容小編還不是非常理解。還請各位小夥伴多多不吝賜教哦`(*∩_∩*)′!

Redis簡單介紹以及數據類型存儲