概述
Redis【REmote DIctionary Server】作為當前比較流行的NoSql數據庫,以其高性能,高可用的的特點,應用越來越廣泛,深得各大企業和開發人員的青睞。 本文主要以一個簡單的小例子,簡述ServiceStack.Redis動態庫在Redis方面的相關應用,僅供學習分享使用,如有不足之處,還請指正。
開發環境
相關開發環境,如下所示:
- Microsoft Visual Studio Community 2019
- ServiceStack.Redis 動態庫
- Redis 6.0.9 服務器端環境搭建
ServiceStack.Redis的安裝
在C#開發中,主要通過NuGet包管理器,來安裝ServiceStack.Redis動態庫,目前版本為5.10.4,如下所示:
示例截圖
關於C#調用Redis相關示例截圖,如下所示:
核心代碼
關於Redis操作的核心代碼,主要分兩部分:Redis客戶端管理類,Redis客戶端操作類。
Redis客戶端管理類,主要通過Redis客戶端連接池【PooledRedisClientManager】創建客戶端對象【IRedisClient】,如下所示:

1 using ServiceStack.Redis; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace DemoRedis 9 { 10 /// <summary> 11 /// Redis客戶端管理類 12 /// </summary> 13 public static class RedisManager 14 { 15 private static PooledRedisClientManager clientManager; 16 17 /// <summary> 18 /// 初始化信息 19 /// </summary> 20 private static void initInfo() 21 { 22 string ipaddr = System.Configuration.ConfigurationManager.AppSettings["ipaddr"]; 23 string port = System.Configuration.ConfigurationManager.AppSettings["port"]; 24 string host = string.Format("{0}:{1}", ipaddr, port); 25 initInfo(new string[] { host }, new string[] { host }); 26 } 27 28 /// <summary> 29 /// 初始化Redis客戶端管理 30 /// </summary> 31 /// <param name="readWriteHosts"></param> 32 /// <param name="readOnlyHosts"></param> 33 private static void initInfo(string[] readWriteHosts, string[] readOnlyHosts) 34 { 35 clientManager = new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig 36 { 37 MaxWritePoolSize = 5, // “寫”鏈接池鏈接數 38 MaxReadPoolSize = 5, // “讀”鏈接池鏈接數 39 AutoStart = true, 40 }); 41 } 42 43 public static IRedisClient getRedisClient() 44 { 45 if (clientManager == null) 46 { 47 initInfo(); 48 } 49 return clientManager.GetClient(); 50 } 51 52 } 53 54 55 }
Redis客戶端操作類,主要包括字符串(String),列表(List),哈希結構(Hash),集合(Set),有序集合(Sorted Set)等相關操作功能的封裝,如下所示:

1 using ServiceStack.Redis; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace DemoRedis 9 { 10 public class RedisHelper:IDisposable 11 { 12 private IRedisClient client; 13 14 public RedisHelper() 15 { 16 17 } 18 19 public RedisHelper(IRedisClient client ) { 20 this.client = client; 21 } 22 23 #region 字符串 24 25 public List<string> getAllKeys() 26 { 27 return this.client.GetAllKeys(); 28 } 29 30 /// <summary> 31 /// 設置值 32 /// </summary> 33 /// <param name="key"></param> 34 /// <param name="value"></param> 35 public void setString(string key,string value) 36 { 37 this.client.Set(key, value); 38 } 39 40 /// <summary> 41 /// 設置值,帶過期時間 42 /// </summary> 43 /// <param name="key"></param> 44 /// <param name="value"></param> 45 /// <param name="expiresIn"></param> 46 public void setString(string key, string value,TimeSpan expiresIn) 47 { 48 this.client.Set(key, value, expiresIn); 49 } 50 51 /// <summary> 52 /// 是否存在 53 /// </summary> 54 /// <param name="key"></param> 55 /// <returns></returns> 56 public bool containsKey(string key) 57 { 58 return this.client.ContainsKey(key); 59 } 60 61 /// <summary> 62 /// 設置字典 63 /// </summary> 64 /// <param name="dic"></param> 65 public void setAll(Dictionary<string,string> dic) 66 { 67 this.client.SetAll(dic); 68 } 69 70 /// <summary> 71 /// 返回舊值,設置新值 72 /// </summary> 73 /// <param name="key"></param> 74 /// <param name="value"></param> 75 /// <returns></returns> 76 public string getAndSetValue(string key,string value) { 77 return this.client.GetAndSetValue(key, value); 78 } 79 80 /// <summary> 81 /// 獲取對應的值 82 /// </summary> 83 /// <param name="key"></param> 84 /// <returns></returns> 85 public string getValue(string key) { 86 return this.client.GetValue(key); 87 } 88 89 public string getRandomKey() 90 { 91 return this.client.GetRandomKey(); 92 } 93 94 /// <summary> 95 /// 追加值到現有key中 96 /// </summary> 97 /// <param name="key"></param> 98 /// <param name="value"></param> 99 public void appendToValue(string key, string value) { 100 this.client.AppendToValue(key, value); 101 } 102 103 #endregion 104 105 #region List 106 107 /// <summary> 108 /// (隊列操作)增加一個值到列表 109 /// </summary> 110 /// <param name="listId"></param> 111 /// <param name="value"></param> 112 public void enqueueItemOnList(string listId ,string value) 113 { 114 this.client.EnqueueItemOnList(listId, value); 115 } 116 117 /// <summary> 118 /// 從隊列中出一個 119 /// </summary> 120 /// <param name="listId"></param> 121 /// <returns></returns> 122 public string dequeueItemFromList(string listId) 123 { 124 return this.client.DequeueItemFromList(listId); 125 } 126 127 /// <summary> 128 /// 棧,增加一個值到列表 129 /// </summary> 130 /// <param name="listId"></param> 131 /// <param name="value"></param> 132 public void pushItemToList(string listId, string value) { 133 this.client.PushItemToList(listId, value); 134 } 135 136 /// <summary> 137 /// 棧,從當前列表中出一個值,並返回 138 /// </summary> 139 /// <param name="listId"></param> 140 /// <returns></returns> 141 public string popItemFromList(string listId) 142 { 143 return this.client.PopItemFromList(listId); 144 } 145 146 147 /// <summary> 148 /// 獲取某一個位置的值 149 /// </summary> 150 /// <param name="listId"></param> 151 /// <param name="index"></param> 152 /// <returns></returns> 153 public string getItemFromList(string listId, int index) 154 { 155 return this.client.GetItemFromList(listId, index); 156 } 157 158 /// <summary> 159 /// 設置並修改指定位置的值 160 /// </summary> 161 /// <param name="listId"></param> 162 /// <param name="index"></param> 163 /// <param name="value"></param> 164 public void setItemInList(string listId, int index,string value) 165 { 166 this.client.SetItemInList(listId, index, value); 167 } 168 169 170 /// <summary> 171 /// 獲取列表所有的值 172 /// </summary> 173 /// <param name="listId"></param> 174 /// <returns></returns> 175 public List<string> getAllItemsFromList(string listId) { 176 return this.client.GetAllItemsFromList(listId); 177 } 178 179 /// <summary> 180 /// 刪除所有內容 181 /// </summary> 182 /// <param name="listId"></param> 183 public void removeAllFromList(string listId) { 184 this.client.RemoveAllFromList(listId); 185 } 186 187 /// <summary> 188 /// 刪除列表指定元素 189 /// </summary> 190 /// <param name="listId"></param> 191 /// <param name="attr"></param> 192 public void removeItemFromList(string listId, string value) { 193 this.client.RemoveItemFromList(listId, value); 194 } 195 196 /// <summary> 197 /// 獲取指定列表的長度 198 /// </summary> 199 /// <param name="listId"></param> 200 /// <returns></returns> 201 public long getListCount(string listId) { 202 return this.client.GetListCount(listId); 203 } 204 205 #endregion 206 207 #region Hash 208 209 /// <summary> 210 /// 設置Hash的值 211 /// </summary> 212 /// <param name="hashId"></param> 213 /// <param name="key"></param> 214 /// <param name="value"></param> 215 /// <returns></returns> 216 public bool setEntryInHash(string hashId, string key, string value) 217 { 218 return this.client.SetEntryInHash(hashId, key, value); 219 } 220 221 /// <summary> 222 /// 獲取Hash中的值 223 /// </summary> 224 /// <param name="hashId"></param> 225 /// <param name="key"></param> 226 /// <returns></returns> 227 public string getValueFromHash(string hashId, string key) 228 { 229 return this.client.GetValueFromHash(hashId, key); 230 } 231 232 /// <summary> 233 /// 獲取Hash列表中的所有內容 234 /// </summary> 235 /// <param name="hashId"></param> 236 public Dictionary<string,string> getAllEntriesFromHash(string hashId) 237 { 238 return this.client.GetAllEntriesFromHash(hashId); 239 } 240 241 /// <summary> 242 /// 判斷Hash是否存在指定的鍵 243 /// </summary> 244 /// <param name="hashId"></param> 245 /// <param name="key"></param> 246 /// <returns></returns> 247 public bool hashContainsEntry(string hashId, string key) 248 { 249 return this.client.HashContainsEntry(hashId, key); 250 } 251 252 /// <summary> 253 /// 設置多個值到Hash 254 /// </summary> 255 /// <param name="hashId"></param> 256 /// <param name="keyValuePairs"></param> 257 public void setRangeInHash(string hashId,Dictionary<string,string> keyValuePairs) { 258 this.client.SetRangeInHash(hashId, keyValuePairs); 259 } 260 261 /// <summary> 262 /// 獲取Hash列表的長度 263 /// </summary> 264 /// <param name="hashId"></param> 265 /// <returns></returns> 266 public long getHashCount(string hashId) { 267 return this.client.GetHashCount(hashId); 268 } 269 270 /// <summary> 271 /// 刪除某一個值 272 /// </summary> 273 /// <param name="hashId"></param> 274 /// <param name="key"></param> 275 /// <returns></returns> 276 public bool removeEntryFromHash(string hashId,string key) { 277 return this.client.RemoveEntryFromHash(hashId, key); 278 } 279 280 #endregion 281 282 #region Set 283 284 /// <summary> 285 /// 從Set中獲取隨機值 286 /// </summary> 287 /// <param name="setId"></param> 288 /// <returns></returns> 289 public string getRandomItemFromSet(string setId) 290 { 291 return this.client.GetRandomItemFromSet(setId); 292 } 293 294 /// <summary> 295 /// 獲取所有的值 296 /// </summary> 297 /// <param name="setId"></param> 298 /// <returns></returns> 299 public HashSet<string> getAllItemsFromSet(string setId) 300 { 301 return this.client.GetAllItemsFromSet(setId); 302 } 303 304 /// <summary> 305 /// 獲取set的長度 306 /// </summary> 307 /// <param name="setId"></param> 308 /// <returns></returns> 309 public long getSetCount(string setId) 310 { 311 return this.client.GetSetCount(setId); 312 } 313 314 /// <summary> 315 /// 刪除某一項 316 /// </summary> 317 /// <param name="setId"></param> 318 /// <param name="item"></param> 319 public void removeItemFromSet(string setId,string item) { 320 this.client.RemoveItemFromSet(setId, item); 321 } 322 323 /// <summary> 324 /// 新增內容 325 /// </summary> 326 /// <param name="setId"></param> 327 /// <param name="item"></param> 328 public void addItemToSet(string setId, string item) { 329 this.client.AddItemToSet(setId, item); 330 } 331 332 /// <summary> 333 /// 增加列表到Set 334 /// </summary> 335 /// <param name="setId"></param> 336 /// <param name="items"></param> 337 public void addRangeToSet(string setId,List<string> items) { 338 this.client.AddRangeToSet(setId, items); 339 } 340 341 #endregion 342 343 #region zset 344 345 /// <summary> 346 /// 添加元素到排序集合 347 /// </summary> 348 /// <param name="setId"></param> 349 /// <param name="value"></param> 350 /// <returns></returns> 351 public bool addItemToSortedSet(string setId,string value) { 352 return this.client.AddItemToSortedSet(setId, value); 353 } 354 355 /// <summary> 356 /// 添加元素到排序集合 357 /// </summary> 358 /// <param name="setId"></param> 359 /// <param name="value"></param> 360 /// <param name="score"></param> 361 /// <returns></returns> 362 public bool addItemToSortedSet(string setId, string value,double score) 363 { 364 return this.client.AddItemToSortedSet(setId, value,score); 365 } 366 367 /// <summary> 368 /// 增加列表到排序集合 369 /// </summary> 370 /// <param name="setId"></param> 371 /// <param name="values"></param> 372 /// <param name="score"></param> 373 /// <returns></returns> 374 public bool addRangeToSortedSet(string setId,List<string> values, double score) { 375 return this.client.AddRangeToSortedSet(setId, values, score); 376 } 377 378 /// <summary> 379 /// 增加列表到排序集合 380 /// </summary> 381 /// <param name="setId"></param> 382 /// <param name="values"></param> 383 /// <param name="score"></param> 384 /// <returns></returns> 385 public bool addRangeToSortedSet(string setId, List<string> values, long score) 386 { 387 return this.client.AddRangeToSortedSet(setId, values, score); 388 } 389 390 /// <summary> 391 /// 獲取所有的集合內容 392 /// </summary> 393 /// <param name="setId"></param> 394 /// <returns></returns> 395 public List<string> getAllItemsFromSortedSet(string setId) { 396 return this.client.GetAllItemsFromSortedSet(setId); 397 } 398 399 /// <summary> 400 /// 倒序獲取所有的內容 401 /// </summary> 402 /// <param name="setId"></param> 403 /// <returns></returns> 404 public List<string> getAllItemsFromSortedSetDesc(string setId) { 405 return this.client.GetAllItemsFromSortedSetDesc(setId); 406 } 407 408 /// <summary> 409 /// 帶分數一起取出 410 /// </summary> 411 /// <param name="setId"></param> 412 /// <returns></returns> 413 public IDictionary<string, double> getAllWithScoresFromSortedSet(string setId) 414 { 415 return this.client.GetAllWithScoresFromSortedSet(setId); 416 } 417 418 /// <summary> 419 /// 獲取對應值的位置 420 /// </summary> 421 /// <param name="setId"></param> 422 /// <param name="value"></param> 423 /// <returns></returns> 424 public long getItemIndexInSortedSet(string setId,string value) 425 { 426 return this.client.GetItemIndexInSortedSet(setId, value); 427 } 428 429 /// <summary> 430 /// 倒序的位置 431 /// </summary> 432 /// <param name="setId"></param> 433 /// <param name="value"></param> 434 /// <returns></returns> 435 public long getItemIndexInSortedSetDesc(string setId, string value) { 436 return this.client.GetItemIndexInSortedSetDesc(setId, value); 437 } 438 439 /// <summary> 440 /// 獲取對應元素的分數 441 /// </summary> 442 /// <param name="setId"></param> 443 /// <param name="value"></param> 444 /// <returns></returns> 445 public double getItemScoreInSortedSet(string setId, string value) { 446 return this.client.GetItemScoreInSortedSet(setId, value); 447 } 448 449 /// <summary> 450 /// 451 /// </summary> 452 /// <param name="setId"></param> 453 /// <param name="fromRank"></param> 454 /// <param name="toRank"></param> 455 /// <returns></returns> 456 public List<string> getRangeFromSortedSet(string setId, int fromRank, int toRank) { 457 return this.client.GetRangeFromSortedSet(setId, fromRank, toRank); 458 } 459 460 public IDictionary<string, double> getRangeWithScoresFromSortedSet(string setId, int fromRank, int toRank) { 461 return this.client.GetRangeWithScoresFromSortedSet(setId, fromRank, toRank); 462 } 463 464 public void Dispose() 465 { 466 if(this.client != null) 467 { 468 this.client.Dispose(); 469 } 470 } 471 #endregion 472 } 473 }
關於前端調用功能,主要是各個按鈕對應的事件,如下所示:

1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace DemoRedis 12 { 13 public partial class FrmMain : Form 14 { 15 16 private RedisHelper redisHelper; 17 18 public FrmMain() 19 { 20 InitializeComponent(); 21 } 22 23 /// <summary> 24 /// 加載 25 /// </summary> 26 /// <param name="sender"></param> 27 /// <param name="e"></param> 28 private void FrmMain_Load(object sender, EventArgs e) 29 { 30 this.redisHelper = new RedisHelper(RedisManager.getRedisClient()); 31 } 32 33 /// <summary> 34 /// 獲取所有的Key 35 /// </summary> 36 /// <param name="sender"></param> 37 /// <param name="e"></param> 38 private void btnAllKey_Click(object sender, EventArgs e) 39 { 40 List<string> keys = this.redisHelper.getAllKeys(); 41 this.combKeys.Items.AddRange(keys.ToArray()); 42 this.combKeys.SelectedIndex = 0; 43 } 44 45 private void btnSetString_Click(object sender, EventArgs e) 46 { 47 string key = this.txtKey1.Text; 48 string value = this.txtValue1.Text; 49 if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value)) 50 { 51 return; 52 } 53 this.redisHelper.setString(key, value); 54 MessageBox.Show("設置成功"); 55 } 56 57 private void btnGetString_Click(object sender, EventArgs e) 58 { 59 string key = this.txtKey2.Text; 60 if (string.IsNullOrEmpty(key)) 61 { 62 return; 63 } 64 string value = this.redisHelper.getValue(key); 65 if (string.IsNullOrEmpty(value)) { 66 MessageBox.Show("讀取失敗,值為空"); 67 } 68 else 69 { 70 this.txtValue2.Text = value; 71 } 72 } 73 74 private void btnAppend_Click(object sender, EventArgs e) 75 { 76 string key = this.txtKey3.Text; 77 string value = this.txtValue3.Text; 78 if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value)) { 79 return; 80 } 81 this.redisHelper.appendToValue(key, value); 82 MessageBox.Show("追加成功"); 83 } 84 85 private void btnAddItemToHash_Click(object sender, EventArgs e) 86 { 87 string hashId = this.txtHashId1.Text; 88 string attr = this.txtHashAttr1.Text; 89 string value = this.txtHashVal1.Text; 90 if (string.IsNullOrEmpty(hashId) || string.IsNullOrEmpty(attr) || string.IsNullOrEmpty(value)) 91 { 92 return; 93 } 94 if (this.redisHelper.setEntryInHash(hashId, attr, value)) 95 { 96 MessageBox.Show("設置成功"); 97 } 98 else { 99 MessageBox.Show("設置失敗"); 100 } 101 } 102 103 private void btnGetItemFromHash_Click(object sender, EventArgs e) 104 { 105 string hashId = this.txtHashId2.Text; 106 string attr = this.txtHashAttr2.Text; 107 if (string.IsNullOrEmpty(hashId) || string.IsNullOrEmpty(attr) ) 108 { 109 return; 110 } 111 string value = this.redisHelper.getValueFromHash(hashId, attr); 112 if (string.IsNullOrEmpty(value)) 113 { 114 MessageBox.Show("獲取失敗"); 115 } 116 else 117 { 118 this.txtHashVal2.Text = value; 119 } 120 } 121 122 private void btnGetHashCount_Click(object sender, EventArgs e) 123 { 124 string hashId = this.txtHashId3.Text; 125 if (string.IsNullOrEmpty(hashId)) 126 { 127 return; 128 } 129 long count = this.redisHelper.getHashCount(hashId); 130 if (count > 0) 131 { 132 MessageBox.Show("長度為:" + count); 133 } 134 else { 135 MessageBox.Show("獲取失敗"); 136 } 137 } 138 139 private void btnHashContainsEntry_Click(object sender, EventArgs e) 140 { 141 string hashId = this.txtHashId3.Text; 142 string attr = this.txtHashAttr3.Text; 143 if (string.IsNullOrEmpty(hashId) || string.IsNullOrEmpty(attr)) 144 { 145 return; 146 } 147 bool flag = this.redisHelper.hashContainsEntry(hashId, attr); 148 if (flag) { 149 MessageBox.Show("存在"); 150 } 151 else 152 { 153 MessageBox.Show("不存在"); 154 } 155 } 156 157 private void btnRemoveEntryFromHash_Click(object sender, EventArgs e) 158 { 159 string hashId = this.txtHashId3.Text; 160 string attr = this.txtHashAttr3.Text; 161 if (string.IsNullOrEmpty(hashId) || string.IsNullOrEmpty(attr)) 162 { 163 return; 164 } 165 bool flag = this.redisHelper.removeEntryFromHash(hashId, attr); 166 if (flag) 167 { 168 MessageBox.Show("刪除成功"); 169 } 170 else 171 { 172 MessageBox.Show("刪除失敗"); 173 } 174 } 175 176 private void btnEnqueueItemToList_Click(object sender, EventArgs e) 177 { 178 string listId = this.txtListId1.Text; 179 string item = this.txtListItem1.Text; 180 if (string.IsNullOrEmpty(listId) || string.IsNullOrEmpty(item)) 181 { 182 return; 183 } 184 this.redisHelper.enqueueItemOnList(listId, item); 185 MessageBox.Show("入隊成功"); 186 } 187 188 private void btnPushItemToList_Click(object sender, EventArgs e) 189 { 190 string listId = this.txtListId2.Text; 191 string item = this.txtListItem2.Text; 192 if (string.IsNullOrEmpty(listId) || string.IsNullOrEmpty(item)) 193 { 194 return; 195 } 196 this.redisHelper.pushItemToList(listId, item); 197 MessageBox.Show("入棧成功"); 198 } 199 200 private void btnGetItemFromList_Click(object sender, EventArgs e) 201 { 202 string listId = this.txtListId3.Text; 203 string index = this.txtListIndex3.Text; 204 if (string.IsNullOrEmpty(listId) || string.IsNullOrEmpty(index)) 205 { 206 return; 207 } 208 string value = this.redisHelper.getItemFromList(listId, int.Parse(index)); 209 if (string.IsNullOrEmpty(value)) 210 { 211 MessageBox.Show("值為:" + value); 212 } 213 else 214 { 215 MessageBox.Show("獲取失敗"); 216 } 217 } 218 219 private void btnRemoveItemFromList_Click(object sender, EventArgs e) 220 { 221 string listId = this.txtListId4.Text; 222 string item = this.txtListItem4.Text; 223 if (string.IsNullOrEmpty(listId) || string.IsNullOrEmpty(item)) 224 { 225 return; 226 } 227 this.redisHelper.removeItemFromList(listId, item); 228 MessageBox.Show("刪除成功"); 229 } 230 231 private void btnAddItemToSet_Click(object sender, EventArgs e) 232 { 233 string setId = this.txtSetId1.Text; 234 string item = this.txtSetItem1.Text; 235 if (string.IsNullOrEmpty(setId) || string.IsNullOrEmpty(item)) 236 { 237 return; 238 } 239 this.redisHelper.addItemToSet(setId, item); 240 MessageBox.Show("添加成功"); 241 } 242 243 private void btnGetSetCount_Click(object sender, EventArgs e) 244 { 245 string setId = this.txtSetId1.Text; 246 if (string.IsNullOrEmpty(setId)) 247 { 248 return; 249 } 250 long count = this.redisHelper.getSetCount(setId); 251 if (count > 0) 252 { 253 MessageBox.Show("長度為:" + count); 254 } 255 else 256 { 257 MessageBox.Show("獲取失敗"); 258 } 259 } 260 261 private void btnAddItemToSortSet_Click(object sender, EventArgs e) 262 { 263 string setId = this.txtZsetId1.Text; 264 string item = this.txtZsetItem1.Text; 265 if (string.IsNullOrEmpty(setId) || string.IsNullOrEmpty(item)) 266 { 267 return; 268 } 269 bool flag = this.redisHelper.addItemToSortedSet(setId, item); 270 if (flag) 271 { 272 MessageBox.Show("添加成功"); 273 } 274 else 275 { 276 MessageBox.Show("添加失敗"); 277 } 278 } 279 280 private void btnGetScoreByValue_Click(object sender, EventArgs e) 281 { 282 string setId = this.txtZsetId2.Text; 283 string item = this.txtZsetItem2.Text; 284 if (string.IsNullOrEmpty(setId) || string.IsNullOrEmpty(item)) 285 { 286 return; 287 } 288 double score = this.redisHelper.getItemScoreInSortedSet(setId, item); 289 MessageBox.Show("分數為:" + score); 290 } 291 292 private void btnAddItemWithScoreToZset_Click(object sender, EventArgs e) 293 { 294 string setId = this.txtZsetId2.Text; 295 string item = this.txtZsetItem2.Text; 296 string score = this.txtZsetScore2.Text; 297 if (string.IsNullOrEmpty(setId) || string.IsNullOrEmpty(item) || string.IsNullOrEmpty(score)) 298 { 299 return; 300 } 301 bool flag = this.redisHelper.addItemToSortedSet(setId, item,double.Parse(score)); 302 if (flag) 303 { 304 MessageBox.Show("添加成功"); 305 } 306 else 307 { 308 MessageBox.Show("添加失敗"); 309 } 310 } 311 } 312 }
注意事項
如果在Redis服務端和客戶端不在同一個電腦上,可能會出現無法訪問的情況,前端報連接超時錯誤。則可能是服務端防火牆限制。需要執行如下命令即可:
1 [root@localhost bin]# /sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT
另外Redis.conf配置文件中,關於bind配置,默認bind為127.0.0.1只允許本機訪問,需要修改,如下所示:
1 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2 bind 127.0.0.1 192.168.127.131
其中:192.168.127.131 為服務器IP地址。
備注
使至塞上
朝代:唐代 | 作者:王維
單車欲問邊,屬國過居延。
征蓬出漢塞,歸雁入胡天。
大漠孤煙直,長河落日圓。
蕭關逢候騎,都護在燕然。