Redis緩存之Set使用


      在Redis中,我們可以將Set類型看作為沒有排序的字符集合,和List類型一樣,我們也可以在該類型的數據值上執行添加、刪除或判斷某一元素是否存在等操作。需要說明的是,這些操作的時間復雜度為O(1),即常量時間內完成次操作。Set可包含的最大元素數量是4294967295。
      和List類型不同的是,Set集合中不允許出現重復的元素,這一點和C++標准庫中的set容器是完全相同的。換句話說,如果多次添加相同元素,Set中將僅保留該元素的一份拷貝。和List類型相比,Set類型在功能上還存在着一個非常重要的特性,即在服務器端完成多個Sets之間的聚合計算操作,如unions、intersections和differences。由於這些操作均在服務端完成,因此效率極高,而且也節省了大量的網絡IO開銷。(參考:http://www.cnblogs.com/stephen-liu74/archive/2012/02/15/2352512.html

      Redis做緩存Set可能到的的比較多(一家之言,歡迎拍磚

      打開redis服務器:

      打開redis客戶端:

      這就是一個set集合!

      至於redisset的命令小伙伴們可以參考(http://redisdoc.com

      下面分享redis在.net中的使用方法

,     1,獲得集合

 1   // 獲取sortset表中setId中的所有keys,倒序獲取
 2        public List<string> GetAllItemsFromSortedSetDesc(string setId)
 3        {
 4             List<string> result = ExecuteCommand<List<string>>(client =>
 5             {
 6                 return client.GetAllItemsFromSortedSetDesc(setId);
 7             });
 8             return result;
 9         }
10 
11 
12         public List<string> GetAllItemsFromSortedSet(string setId)
13         {
14             List<string> result = ExecuteCommand<List<string>>(client =>
15             {
16                 return client.GetAllItemsFromSortedSet(setId);
17             });
18             return result;
19         }
20 
21 
22         // 獲取sortset表中setId中的所有keys,values
23         public IDictionary<string, double> GetAllWithScoresFromSortedSet(string setId)
24         {
25             IDictionary<string, double> result = ExecuteCommand<IDictionary<string, double>>(client =>
26             {
27                 return client.GetAllWithScoresFromSortedSet(setId);
28                 //return client.GetFromHash<Dictionary<string, string>>(hashID);
29             });
30 
31             return result;
32         }

 

      2,刪除某個set

        // 刪除某個KEY的值,成功返回TRUE
        public bool RemoveKey(string key)
        {
            bool result = false;
            result = ExecuteCommand<bool>(client =>
                 {
                     return client.Remove(key);
                 });
            return result;
        }

        // 刪除Set數據中的某個為item的值
        public bool RemoveItemFromSet(string setId, string item)
        {
            byte[] bvalue = System.Text.Encoding.UTF8.GetBytes(item);
            bool result = ExecuteCommand<bool>(client =>
            {
                var rc = client as RedisClient;
                if (rc != null)
                {
                    return rc.SRem(setId, bvalue) == 1;
                }
                return false;
            });
            return result;
        }

 

      3,搜索

         //搜索key
        public List<string> SearchKeys(string pattern)
        {
            List<string> result = ExecuteCommand<List<string>>(client =>
            {
                return client.SearchKeys(pattern);
            });
            return result;
        }

 

      4,增加某個元素到set

       public bool AddItemToSet(string setId, string item)
        {
            byte[] bvalue = System.Text.Encoding.UTF8.GetBytes(item);
            bool result = ExecuteCommand<bool>(client =>
            {
                var rc = client as RedisClient;
                if (rc != null)
                {
                    return rc.SAdd(setId, bvalue) == 1;
                }
                return false;
            });
            return result;

        }

 

       這里只分享幾個方法,其實還有很多關於set的操作方法。

      利用Redis提供的Sets數據結構,可以存儲一些集合性的數據,比如在微博應用中,可以將一個用戶所有的關注人存在一個集合中,將其所有粉絲存在一個集合。Redis還為集合提供了求交集、並集、差集等操作,可以非常方便的實現如共同關注、共同喜好、二度好友等功能,對上面的所有集合操作,你還可以使用不同的命令選擇將結果返回給客戶端還是存集到一個新的集合中。 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM