CacheKeys.Post.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. namespace JiaZhiQuan.Common
  3. {
  4. public static partial class CacheKeys
  5. {
  6. /// <summary>
  7. /// 文章缓存1分钟
  8. /// </summary>
  9. public const int PostDetailCacheSecs = 60;
  10. /// <summary>
  11. /// 文章详情缓存编号
  12. /// </summary>
  13. public static string PostDetail(long postId)
  14. {
  15. return GenerateKey($"post_detail:{postId}");
  16. }
  17. /// <summary>
  18. /// 1天
  19. /// </summary>
  20. public const int PostRecommendListCacheSecs = 60 * 60 * 24;
  21. /// <summary>
  22. /// 文章推荐列表缓存的Key
  23. /// </summary>
  24. /// <param name="day">当天几号</param>
  25. /// <param name="userId">非登录用户,传0</param>
  26. /// <param name="categoryType">如果没有,则传null</param>
  27. /// <param name="contentType">null表示所有,1为带图片的,2为带视频的</param>
  28. /// <param name="hasTopic">如果没有,则传null</param>
  29. public static string PostRecommendList(int day, long userId, int? categoryType, int? contentType, bool? hasTopic)
  30. {
  31. return GenerateKey($"post_rcml_{day}:{userId}:{categoryType?.ToString() ?? "null"}_{contentType?.ToString() ?? "null"}" + (hasTopic == null ? "" : hasTopic.Value ? "_t1" : "_t0"));
  32. }
  33. public const int PostRecommendCacheCursorSecs = 60 * 60 * 48;
  34. /// <summary>
  35. /// 文章推荐缓存用户端访问的游标,内容是已取得的最后一个`{postId}_{index}_{over}`
  36. /// </summary>
  37. /// <param name="clientKey">客户端编号</param>
  38. /// <param name="categoryType">如果没有,则传null</param>
  39. /// <param name="contentType">null表示所有,1为带图片的,2为带视频的</param>
  40. /// <param name="hasTopic">如果没有,则传null</param>
  41. public static string PostRecommendCacheCursor(string clientKey, int? categoryType, int? contentType, bool? hasTopic)
  42. {
  43. return GenerateKey($"post_rcml_cursor:{clientKey}:{categoryType?.ToString() ?? "null"}_{contentType?.ToString() ?? "null"}" + (hasTopic == null ? "" : hasTopic.Value ? "_t1" : "_t0"));
  44. }
  45. /// <summary>
  46. /// 新文章推荐缓存
  47. /// </summary>
  48. /// <param name="categoryType">如果没有,则传null</param>
  49. /// <param name="contentType">null表示所有,1为带图片的,2为带视频的</param>
  50. /// <param name="hasTopic">如果没有,则传null</param>
  51. public static string PostNewRecommendCache(string time, int? categoryType, int? contentType, bool? hasTopic)
  52. {
  53. return GenerateKey($"post_rcml_new:{time}:{categoryType?.ToString() ?? "null"}_{contentType?.ToString() ?? "null"}" + (hasTopic == null ? "" : hasTopic.Value ? "_t1" : "_t0"));
  54. }
  55. public const int PostContentCacheSecs = 20;
  56. /// <summary>
  57. /// 文章内容缓存,用于用户发布,在AI审核时优先从缓存获取
  58. /// </summary>
  59. /// <param name="postId"></param>
  60. /// <returns></returns>
  61. public static string PostContent(long postId)
  62. {
  63. return GenerateKey($"post_content:{postId}");
  64. }
  65. public const int PostNewNotificationCacheSecs = 6 * 60 * 60; // 6小时
  66. /// <summary>
  67. /// 发动态后,6小时内发的新动态不会通知,1个自然日最多1条
  68. /// </summary>
  69. /// <returns></returns>
  70. public static int PostNewNotificationCacheTime()
  71. {
  72. // 获取当前时间
  73. DateTime currentTime = DateTime.Now;
  74. // 获取今天结束的时间(明天的凌晨)
  75. DateTime endOfDay = currentTime.Date.AddDays(1);
  76. // 计算时间差
  77. TimeSpan timeRemaining = endOfDay - currentTime;
  78. // 获取剩余秒数
  79. int secondsRemaining = (int)timeRemaining.TotalSeconds;
  80. // 如果剩余时间大于6小时,则返回距离今天结束剩余的秒数
  81. if(secondsRemaining > PostNewNotificationCacheSecs)
  82. {
  83. return secondsRemaining;
  84. }
  85. // 如果剩余时间不足6小时,则返回6小时
  86. else
  87. {
  88. return PostNewNotificationCacheSecs;
  89. }
  90. }
  91. /// <summary>
  92. /// 一个人发了动态之后,将状态缓存到Redis,判断是否需要通知
  93. /// </summary>
  94. public static string PostNewNotification(long userId)
  95. {
  96. return GenerateKey($"post_new:{userId}");
  97. }
  98. /// <summary>
  99. /// 一个人发了商品之后,将状态缓存到Redis,判断是否需要通知
  100. /// </summary>
  101. public static string GoodsNewNotification(long userId)
  102. {
  103. return GenerateKey($"mall:goods_new:{userId}");
  104. }
  105. /// <summary>
  106. /// 发商品后,1个自然日最多1条通知
  107. /// </summary>
  108. /// <returns></returns>
  109. public static int GoodsNewNotificationCacheTime()
  110. {
  111. // 获取当前时间
  112. DateTime currentTime = DateTime.Now;
  113. // 获取今天结束的时间(明天的凌晨)
  114. DateTime endOfDay = currentTime.Date.AddDays(1);
  115. // 计算时间差
  116. TimeSpan timeRemaining = endOfDay - currentTime;
  117. // 获取剩余秒数
  118. return (int)timeRemaining.TotalSeconds;
  119. }
  120. }
  121. }