(精华)2020年8月15日 redis数据库 StackExchange.Redis中Set类型(C#版)

RedisSetService 类请参考第一篇的封装

redis 数据结构Set的使用

using (RedisSetService service = new RedisSetService())
{
service.FlushAll();//清理全部数据

service.Add("advanced", "111");
service.Add("advanced", "112");
service.Add("advanced", "114");
service.Add("advanced", "114");
service.Add("advanced", "115");
service.Add("advanced", "115");
service.Add("advanced", "113");

var result = service.GetAllItemsFromSet("advanced");

var random = service.GetRandomItemFromSet("advanced");//随机获取
service.GetCount("advanced");//独立的ip数
service.RemoveItemFromSet("advanced", "114");

{
service.Add("begin", "111");
service.Add("begin", "112");
service.Add("begin", "115");

service.Add("end", "111");
service.Add("end", "114");
service.Add("end", "113");

var result1 = service.GetIntersectFromSets("begin", "end");
var result2 = service.GetDifferencesFromSet("begin", "end");
var result3 = service.GetUnionFromSets("begin", "end");
}
}

案例

//去重:IP统计去重;添加好友申请;投票限制;点赞;
//交叉并的使用
using (RedisSetService service = new RedisSetService())
{
service.FlushAll();//清理全部数据
service.Add("XT", "Powell");
service.Add("XT", "Tenk");
service.Add("XT", "spider");
service.Add("XT", "spider");
service.Add("XT", "spider");
service.Add("XT", "aaron");
service.Add("XT", "Linsan");

service.Add("Powell", "Eleven");
service.Add("Powell", "Tenk");
service.Add("Powell", "ywa");
service.Add("Powell", "Pang");
service.Add("Powell", "Jeff");

var result = service.GetIntersectFromSets("XT", "Powell");
var result2 = service.GetDifferencesFromSet("Powell", "XT");
var result3 = service.GetDifferencesFromSet("XT", "Powell");
var result4=service.GetUnionFromSets("XT", "Powell");
}

/// 
/// 好友管理 共同好友-可能认识 
/// 找出共同好友: 
/// 关系型数据库:找出2个好友列表,然后再比对一下 
/// 二次好友(可能认识):
/// 
/// 既然是使用Redis  就尽可能的去使用Redis里面封装好的Api;
/// 
public static void Show()
{ 
    using (RedisSetService service = new RedisSetService())
    { 
        service.FlushAll();
        service.Add("xut", "云怀");
        service.Add("xut", "fresh");
        service.Add("xut", "腾坤");
        service.Add("xut", "心有所属");
        service.Add("xut", "莎士比亚");

        service.Add("fresh", "奥利给");
        service.Add("fresh", "明日梦");
        service.Add("fresh", "棒棒糖");
        service.Add("fresh", "放下");
        service.Add("fresh", "阳光下的微信");
        service.Add("fresh", "腾坤");
        //1.查询共同好友; 求交集
        var resut1 = service.GetIntersectFromSets("xut", "fresh");//
        //2.好友推荐/可能认识人
        var resut2 = service.GetDifferencesFromSet("fresh", "xut");//
        var resut3 = service.GetDifferencesFromSet("xut", "fresh");//

        var result4 = service.GetUnionFromSets("xut", "fresh");

    }

}

扩展版

using (RedisSetService service = new RedisSetService())
                {
                    //key--values
                    service.KeyFulsh();
                    service.Add("Advanced", "111");
                    service.Add("Advanced", "112");
                    service.Add("Advanced", "113");
                    service.Add("Advanced", "115");
                    service.Add("Advanced", "114");
                    service.Add("Advanced", "111");

                    service.Add("Begin", "111");
                    service.Add("Begin", "112");
                    service.Add("Begin", "113");
                    service.Add("Begin", "117");
                    service.Add("Begin", "116");
                    service.Add("Begin", "111");

                    service.Add("Internal", "111");
                    service.Add("Internal", "112");
                    service.Add("Internal", "117");
                    service.Add("Internal", "119");
                    service.Add("Internal", "118");
                    service.Add("Internal", "111");

                    var result = service.GetAllItemsFromSet("Advanced");
                    var result2 = service.GetRandomItemFromSet("Advanced");
                    result = service.GetAllItemsFromSet("Begin");
                    result2 = service.GetRandomItemFromSet("Begin");

                    var result3 = service.GetIntersectFromSets("Advanced", "Begin");//交
                    result3 = service.GetDifferencesFromSet("Advanced", "Begin", "Internal");//差
                    result3 = service.GetUnionFromSets("Advanced", "Begin", "Internal");//并

                    service.RemoveItemFromSet("Advanced", "111");
                    result = service.GetAllItemsFromSet("Advanced");
                    service.RandomRemoveItemFromSet("Advanced");
                    result = service.GetAllItemsFromSet("Advanced");
                }

你可能感兴趣的:(redis数据库,redis)