平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

99 line
2.4 KiB

  1. //
  2. namespace SafeCampus.Cache;
  3. /// <summary>
  4. /// <inheritdoc cref="ISimpleCacheService"/>
  5. /// 内存缓存
  6. /// </summary>
  7. public partial class MemoryCacheService
  8. {
  9. /// <inheritdoc/>
  10. public void HashAdd<T>(string key, string hashKey, T value)
  11. {
  12. //获取字典
  13. var exist = _memoryCache.GetDictionary<T>(key);
  14. if (exist.ContainsKey(hashKey))//如果包含Key
  15. exist[hashKey] = value;//重新赋值
  16. else exist.TryAdd(hashKey, value);//加上新的值
  17. _memoryCache.Set(key, exist);
  18. }
  19. //private IDictionary<string,T> GetDictionary(string key,string)
  20. /// <inheritdoc/>
  21. public bool HashSet<T>(string key, Dictionary<string, T> dic)
  22. {
  23. //获取字典
  24. var exist = _memoryCache.GetDictionary<T>(key);
  25. dic.ForEach(it =>
  26. {
  27. if (exist.ContainsKey(it.Key))//如果包含Key
  28. exist[it.Key] = it.Value;//重新赋值
  29. else exist.Add(it.Key, it.Value);//加上新的值
  30. });
  31. return true;
  32. }
  33. /// <inheritdoc/>
  34. public int HashDel<T>(string key, params string[] fields)
  35. {
  36. var result = 0;
  37. //获取字典
  38. var exist = _memoryCache.GetDictionary<T>(key);
  39. foreach (var field in fields)
  40. {
  41. if (field != null && exist.ContainsKey(field))//如果包含Key
  42. {
  43. exist.Remove(field);//删除
  44. result++;
  45. }
  46. }
  47. return result;
  48. }
  49. /// <inheritdoc/>
  50. public List<T> HashGet<T>(string key, params string[] fields)
  51. {
  52. var list = new List<T>();
  53. //获取字典
  54. var exist = _memoryCache.GetDictionary<T>(key);
  55. foreach (var field in fields)
  56. {
  57. if (exist.TryGetValue(field, out var value))//如果包含Key
  58. {
  59. list.Add(value);
  60. }
  61. else { list.Add(default); }
  62. }
  63. return list;
  64. }
  65. /// <inheritdoc/>
  66. public T HashGetOne<T>(string key, string field)
  67. {
  68. //获取字典
  69. var exist = _memoryCache.GetDictionary<T>(key);
  70. exist.TryGetValue(field, out var result);
  71. var data = result.DeepClone();
  72. return data;
  73. }
  74. /// <inheritdoc/>
  75. public IDictionary<string, T> HashGetAll<T>(string key)
  76. {
  77. var data = _memoryCache.GetDictionary<T>(key);
  78. return data;
  79. }
  80. }