語言:c#
使用函式庫:StackExchange.Redis
//設定連線字串 using (ConnectionMultiplexer conn = ConnectionMultiplexer.Connect("127.0.0.1:10001")) { // 取得操作介面 IDatabase cache = conn.GetDatabase(); const string custKey1 = "Key1"; // 先刪除該Key以確保這個key對應資料型態是全新的 cache.KeyDeleteAsync(custKey1); // 設定值 cache.HashSetAsync(custKey1, "abc", "def"); cache.HashSetAsync(custKey1, "ghi", "jkl"); cache.HashSetAsync(custKey1, "mno", "pqr"); // 依照custKey1取得對應hash值 HashEntry[] tResult = cache.HashGetAll(custKey1); foreach (HashEntry tHE in tResult) { Console.WriteLine(tHE.Name + ":" + tHE.Value); } }
我們也可以使用HashSet() 一次設定好Hash的值
//設定連線字串 using (ConnectionMultiplexer conn = ConnectionMultiplexer.Connect("127.0.0.1:10001")) { // 取得操作介面 IDatabase cache = conn.GetDatabase(); const string custKey1 = "Key1"; // 先刪除該Key以確保這個key對應資料型態是全新的 cache.KeyDeleteAsync(custKey1); // 設定值 HashEntry[] hashEntries = { new HashEntry("abc", "def"), new HashEntry("ghi", "jkl"), new HashEntry("mno", "pqr"), }; cache.HashSet(custKey1, hashEntries); // 依照custKey1取得對應hash值 HashEntry[] tResult = cache.HashGetAll(custKey1); foreach (HashEntry tHE in tResult) { Console.WriteLine(tHE.Name + ":" + tHE.Value); } }
以下為使用搜索範例
// 搜索這個key的所有內容 IEnumerable條件過濾t1 = cache.HashScan(custKey1); HashEntry[] v1 = t1.ToArray(); Console.WriteLine("==HashScan(custKey1)=="); foreach (HashEntry tHE in v1) { Console.WriteLine(tHE.Name + ":" + tHE.Value); }
// 加條件的過濾搜索 IEnumerablet2 = cache.HashScan(custKey1, "*h*"); HashEntry[] v2 = t2.ToArray(); Console.WriteLine("==HashScan(custKey1,'*h*')=="); foreach (HashEntry tHE in v2) { Console.WriteLine(tHE.Name + ":" + tHE.Value); }
沒有留言:
張貼留言