平安校园
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

MemoryCacheHashService.cs 2.4 KiB

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