UserCacheManager.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using Hotline.Caches;
  2. using Hotline.Users;
  3. using XF.Domain.Cache;
  4. using XF.Domain.Dependency;
  5. using XF.Domain.Exceptions;
  6. namespace Hotline.CacheManager;
  7. public class UserCacheManager : IUserCacheManager, IScopeDependency
  8. {
  9. private readonly ITypedCache<Work> _cacheWork;
  10. private readonly IWorkRepository _workRepository;
  11. public UserCacheManager(
  12. ITypedCache<Work> cacheWork,
  13. IWorkRepository workRepository)
  14. {
  15. _cacheWork = cacheWork;
  16. _workRepository = workRepository;
  17. }
  18. /// <summary>
  19. /// 查询用户当前工作记录
  20. /// </summary>
  21. /// <param name="userId"></param>
  22. /// <returns></returns>
  23. public Work GetWorkByUser(string userId)
  24. {
  25. var work = _cacheWork.GetOrAdd(Work.GetKey(KeyMode.UserId, userId), k =>
  26. {
  27. var dbWork = _workRepository.GetCurrentWorkByUserAsync(userId)
  28. .GetAwaiter().GetResult();
  29. if (dbWork == null)
  30. throw new UserFriendlyException("该用户暂无工作记录");
  31. return dbWork;
  32. });
  33. return work;
  34. }
  35. /// <summary>
  36. /// 查询分机当前工作记录
  37. /// </summary>
  38. /// <param name="telNo"></param>
  39. /// <returns></returns>
  40. /// <exception cref="UserFriendlyException"></exception>
  41. public Work GetWorkByTel(string telNo)
  42. {
  43. var work = _cacheWork.GetOrAdd(Work.GetKey(KeyMode.TelNo, telNo), k =>
  44. {
  45. var dbWork = _workRepository.GetCurrentWorkByTelAsync(telNo)
  46. .GetAwaiter().GetResult();
  47. if (dbWork == null)
  48. throw new UserFriendlyException("该分机暂无工作记录");
  49. return dbWork;
  50. });
  51. return work;
  52. }
  53. /// <summary>
  54. /// 查询分机是否处于工作
  55. /// </summary>
  56. /// <param name="telNo"></param>
  57. /// <returns></returns>
  58. public async Task<bool> IsWorkingByTelAsync(string telNo, CancellationToken cancellationToken)
  59. {
  60. var work = _cacheWork.Get(Work.GetKey(KeyMode.TelNo, telNo));
  61. if (work is null)
  62. {
  63. var dbWork = await _workRepository.GetCurrentWorkByTelAsync(telNo, cancellationToken);
  64. if (dbWork != null)
  65. {
  66. _cacheWork.Add(Work.GetKey(KeyMode.TelNo, telNo), dbWork);
  67. return true;
  68. }
  69. return false;
  70. }
  71. return true;
  72. }
  73. /// <summary>
  74. /// 查询分机是否处于工作
  75. /// </summary>
  76. /// <param name="userId"></param>
  77. /// <param name="cancellationToken"></param>
  78. /// <returns></returns>
  79. public async Task<bool> IsWorkingByUserAsync(string userId, CancellationToken cancellationToken)
  80. {
  81. //return _cacheWork.TryGetOrAdd(Work.GetKey(KeyMode.UserId, userId),
  82. // k => _workRepository.GetCurrentWorkByUserAsync(userId).GetAwaiter().GetResult(),
  83. // out _);
  84. var work = _cacheWork.Get(Work.GetKey(KeyMode.UserId, userId));
  85. if (work is null)
  86. {
  87. var dbWork = await _workRepository.GetCurrentWorkByUserAsync(userId, cancellationToken);
  88. if (dbWork is null) return false;
  89. _cacheWork.Add(Work.GetKey(KeyMode.UserId, userId), dbWork);
  90. }
  91. return true;
  92. }
  93. /// <summary>
  94. /// 根据用户更新工作记录
  95. /// </summary>
  96. /// <param name="work"></param>
  97. /// <returns></returns>
  98. public void UpdateWorkByUser(Work work)
  99. {
  100. _cacheWork.Update(Work.GetKey(KeyMode.UserId, work.UserId), d => work);
  101. }
  102. }