国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C#Redis使用之StackExchange

2019-11-14 13:41:55
字體:
來源:轉載
供稿:網友

第1章   安裝

在.NET平臺使用Redis需要做如下準備工作:

  創建一個新的Visual Studio應用或者打開一個已經存在的Visual Studio應用。

  打開NuGet程序包

  

  搜索并添加StackExchange.Redis程序包

  

第2章   數據緩存

使用redis做為數據緩存服務

string host = "192.168.200.131";
//取連接對象
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(host);
//取得DB對象
IDatabase db = redis.GetDatabase();
//設置數據對象
db.StringSet("User", "{Name:/"TOM/"}");
//添加數據對象
db.StringAppend("User", ",{Name:/"JACK/"}");

//取得數據對象
string user = db.StringGet("User");
Console.WriteLine(user);

第3章   訂閱及發布

發布消息

static void Main(string[] args)
{
string host = "192.168.28.106";
//取連接對象
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(host);
//設置數據對象
//取得訂閱對象
ISubscriber sub = redis.GetSubscriber();
//發布消息到channel:c1
sub.Publish("c1", "123");
//發送100萬個字符的消息
sub.Publish("c1", "zx".PadRight(1000000, 'x'));
string reader = "start send";
while(reader!="exit")
{
reader = Console.ReadLine();
sub.Publish("c1", reader);
}
Console.ReadLine();
}

訂閱消息

static void Main(string[] args)
{

string host = "192.168.28.106";
//取連接對象
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(host);
//設置數據對象
//取得訂閱對象
ISubscriber sub = redis.GetSubscriber();
//設置訂閱事件
sub.Subscribe("c1", new Action<RedisChannel, RedisValue>(GetMessage));

Console.Read();


Console.ReadLine();
}
/// <summary>
/// 獲取消息
/// </summary>
/// <param name="channel"></param>
/// <param name="message"></param>
static void GetMessage(RedisChannel channel, RedisValue message)
{
Console.WriteLine(channel);
Console.WriteLine(message);
}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桐乡市| 衡阳市| 黔南| 东台市| 衡南县| 长寿区| 黄平县| 山丹县| 武陟县| 朝阳县| 巨鹿县| 容城县| 永春县| 赤峰市| 内江市| 永善县| 外汇| 昌黎县| 武夷山市| 邛崃市| 霸州市| 开鲁县| 金溪县| 长泰县| 霍邱县| 延边| 冷水江市| 满洲里市| 荔波县| 漾濞| 阿勒泰市| 昆山市| 重庆市| 阿尔山市| 滦南县| 临夏县| 敦化市| 海安县| 深圳市| 屯昌县| 岢岚县|