终端一体化运控平台
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

70 righe
2.1 KiB

  1. using ServiceStack.Redis;
  2. using System.Diagnostics;
  3. namespace BPASmartClient.Compiler
  4. {
  5. public class RedisHelper
  6. {
  7. private volatile static RedisHelper _Instance;
  8. public static RedisHelper GetInstance => _Instance ?? (_Instance = new RedisHelper());
  9. private RedisHelper() { }
  10. RedisClient client;
  11. public async Task<bool> ConnectAsync(string redisconnection)
  12. {
  13. return await Task.Factory.StartNew(new Func<bool>(() =>
  14. {
  15. if (client == null)
  16. {
  17. //"124.222.238.75:16000,password=123456";
  18. client = new RedisClient("124.222.238.75",16000,"123456",1);
  19. client.ConnectTimeout = 5000;
  20. Stopwatch sw = new Stopwatch();
  21. sw.Start();
  22. while (!client.IsSocketConnected())
  23. {
  24. if (sw.ElapsedMilliseconds >= client.ConnectTimeout) break;
  25. Thread.Sleep(1000);
  26. }
  27. string status = client.IsSocketConnected() ? "成功" : "失败";
  28. }
  29. return client.IsSocketConnected();
  30. }));
  31. }
  32. /// <summary>
  33. /// 清除所有redis 数据
  34. /// </summary>
  35. public void FlushDb()
  36. {
  37. client?.FlushDb();
  38. }
  39. /// <summary>
  40. /// 设置值
  41. /// </summary>
  42. /// <typeparam name="TValue"></typeparam>
  43. /// <param name="key"></param>
  44. /// <param name="value"></param>
  45. public void SetValue<TValue>(string key,TValue value)
  46. {
  47. var res = client?.Set<TValue>(key,value);
  48. }
  49. /// <summary>
  50. /// 获取值
  51. /// </summary>
  52. /// <typeparam name="TResult"></typeparam>
  53. /// <param name="key"></param>
  54. /// <returns></returns>
  55. public TResult GetValue<TResult>(string key)
  56. {
  57. if (client == null) return default(TResult);
  58. return client.Get<TResult>(key);
  59. }
  60. }
  61. }