RedisCache.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using Newtonsoft.Json;
  2. using ServiceStack.Redis;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. namespace CarLocalMeter
  8. {
  9. public class RedisCache : ICache
  10. {
  11. public T GetCache<T>(string key)
  12. {
  13. using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort))
  14. {
  15. T value = default(T);
  16. var redisValue = client.Get<string>(key);
  17. if (string.IsNullOrWhiteSpace(redisValue))
  18. return value;
  19. ValueInfoEntry valueEntry = JsonConvert.DeserializeObject<ValueInfoEntry>(redisValue.Trim());
  20. value = JsonConvert.DeserializeObject<T>(valueEntry.Value);
  21. if (valueEntry.ExpireTime > 0 && valueEntry.ExpireType == ExpireType.Relative)
  22. SetKeyExpire(key, valueEntry.ExpireTime);
  23. return value;
  24. }
  25. }
  26. public void RemoveCache(string key)
  27. {
  28. using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort))
  29. {
  30. client.Del(key);
  31. }
  32. }
  33. public void SetCache(string key, object value, int timeout = 0, ExpireType expireType = ExpireType.Absolute)
  34. {
  35. string jsonStr = string.Empty;
  36. if (value is string)
  37. jsonStr = value as string;
  38. else
  39. jsonStr = JsonConvert.SerializeObject(value);
  40. ValueInfoEntry entry = new ValueInfoEntry
  41. {
  42. Value = jsonStr,
  43. TypeName = value.GetType().AssemblyQualifiedName,
  44. ExpireTime = timeout,
  45. ExpireType = expireType
  46. };
  47. string theValue = JsonConvert.SerializeObject(entry); ;
  48. using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort))
  49. {
  50. client.Set(key, theValue);
  51. if (timeout > 0)
  52. client.Expire(key, timeout);
  53. }
  54. }
  55. public void SetKeyExpire(string key, int expire)
  56. {
  57. using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort))
  58. {
  59. client.Expire(key, expire);
  60. }
  61. }
  62. public void setRedisHash(string hashId, string key, object value, int timeout = 0, ExpireType expireType = ExpireType.Absolute)
  63. {
  64. string jsonStr = string.Empty;
  65. if (value is string)
  66. jsonStr = value as string;
  67. else
  68. jsonStr = JsonConvert.SerializeObject(value);
  69. ValueInfoEntry entry = new ValueInfoEntry
  70. {
  71. Value = jsonStr,
  72. TypeName = value.GetType().AssemblyQualifiedName,
  73. ExpireTime = timeout,
  74. ExpireType = expireType
  75. };
  76. string theValue = JsonConvert.SerializeObject(entry); ;
  77. using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort))
  78. {
  79. client.SetEntryInHash(hashId, key, theValue);
  80. if (timeout > 0)
  81. client.Expire(hashId, timeout);
  82. }
  83. }
  84. public Dictionary<string, string> getRedisHash(string hashId)
  85. {
  86. using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) //还可以接密码跟长度,这里没设置
  87. {
  88. if (client == null) return null;
  89. return client.GetAllEntriesFromHash(hashId);
  90. }
  91. }
  92. }
  93. }