使用 nuget 安裝 StackExchange.Redis 套件
連線方式

ConfigurationOptions options = new ConfigurationOptions
{
    EndPoints = { { "127.0.0.1", 6379 } },
};
ConnectionMultiplexer muxer = ConnectionMultiplexer.Connect(options);
IDatabase conn = muxer.GetDatabase(1);

設定string數據

conn.StringSet("name", "chung  chen");
string name = conn.StringGet("name");

//get value async
var pending = conn.StringGetAsync("name");
string value2 = conn.Wait(pending);

HashSet

var list = new List<User>{
    new User { Index = 1, Name = "立白" },
    new User { Index = 2, Name = "妄为" },
    new User { Index = 3, Name = "毒妇" }
    };
foreach (var item in list){
    var hashEntries = new HashEntry[]{
        new HashEntry("ID", item.Index),
        new HashEntry("Name", item.Name)
    };
    conn.HashSet("User_" + item.Index.ToString(), hashEntries);
}

Subscribe

ISubscriber sub = muxer.GetSubscriber();
sub.Subscribe("messages", (channel, message) => {
    Log.Debug("Subscribe message1:" + (string)message);
});
sub.Publish("messages", "hello");

SortedSet

string rankTag = "rank_1001";
Dictionary<string, int> userScore = new Dictionary<string, int>(){
    { "1045", 70},
    { "1046", 23}
};
foreach (KeyValuePair<string, int> data in userScore){
    await conn.SortedSetAddAsync(rankTag, data.Key, data.Value);
}
//get 1045's score
double score = (double)await conn.SortedSetScoreAsync(rankTag, "1045");

//get 1045's rank number
int rankNumber = (int)await conn.SortedSetRankAsync(rankTag, "1046")

//get rank's member between 1 to 3
RedisValue[] rankList = await conn.SortedSetRangeByRankAsync(rankTag, 1, 3);

具體還有很多, 請參考下面對應表去使用
79041-rxzl0qz9ut9.png

标签: none

添加新评论