123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- using System;
- namespace JiaZhiQuan.Common
- {
- public static partial class CacheKeys
- {
- /// <summary>
- /// 文章缓存1分钟
- /// </summary>
- public const int PostDetailCacheSecs = 60;
- /// <summary>
- /// 文章详情缓存编号
- /// </summary>
- public static string PostDetail(long postId)
- {
- return GenerateKey($"post_detail:{postId}");
- }
- /// <summary>
- /// 1天
- /// </summary>
- public const int PostRecommendListCacheSecs = 60 * 60 * 24;
- /// <summary>
- /// 文章推荐列表缓存的Key
- /// </summary>
- /// <param name="day">当天几号</param>
- /// <param name="userId">非登录用户,传0</param>
- /// <param name="categoryType">如果没有,则传null</param>
- /// <param name="contentType">null表示所有,1为带图片的,2为带视频的</param>
- /// <param name="hasTopic">如果没有,则传null</param>
- public static string PostRecommendList(int day, long userId, int? categoryType, int? contentType, bool? hasTopic)
- {
- return GenerateKey($"post_rcml_{day}:{userId}:{categoryType?.ToString() ?? "null"}_{contentType?.ToString() ?? "null"}" + (hasTopic == null ? "" : hasTopic.Value ? "_t1" : "_t0"));
- }
- public const int PostRecommendCacheCursorSecs = 60 * 60 * 48;
- /// <summary>
- /// 文章推荐缓存用户端访问的游标,内容是已取得的最后一个`{postId}_{index}_{over}`
- /// </summary>
- /// <param name="clientKey">客户端编号</param>
- /// <param name="categoryType">如果没有,则传null</param>
- /// <param name="contentType">null表示所有,1为带图片的,2为带视频的</param>
- /// <param name="hasTopic">如果没有,则传null</param>
- public static string PostRecommendCacheCursor(string clientKey, int? categoryType, int? contentType, bool? hasTopic)
- {
- return GenerateKey($"post_rcml_cursor:{clientKey}:{categoryType?.ToString() ?? "null"}_{contentType?.ToString() ?? "null"}" + (hasTopic == null ? "" : hasTopic.Value ? "_t1" : "_t0"));
- }
- /// <summary>
- /// 新文章推荐缓存
- /// </summary>
- /// <param name="categoryType">如果没有,则传null</param>
- /// <param name="contentType">null表示所有,1为带图片的,2为带视频的</param>
- /// <param name="hasTopic">如果没有,则传null</param>
- public static string PostNewRecommendCache(string time, int? categoryType, int? contentType, bool? hasTopic)
- {
- return GenerateKey($"post_rcml_new:{time}:{categoryType?.ToString() ?? "null"}_{contentType?.ToString() ?? "null"}" + (hasTopic == null ? "" : hasTopic.Value ? "_t1" : "_t0"));
- }
- public const int PostContentCacheSecs = 20;
- /// <summary>
- /// 文章内容缓存,用于用户发布,在AI审核时优先从缓存获取
- /// </summary>
- /// <param name="postId"></param>
- /// <returns></returns>
- public static string PostContent(long postId)
- {
- return GenerateKey($"post_content:{postId}");
- }
- public const int PostNewNotificationCacheSecs = 6 * 60 * 60; // 6小时
- /// <summary>
- /// 发动态后,6小时内发的新动态不会通知,1个自然日最多1条
- /// </summary>
- /// <returns></returns>
- public static int PostNewNotificationCacheTime()
- {
- // 获取当前时间
- DateTime currentTime = DateTime.Now;
- // 获取今天结束的时间(明天的凌晨)
- DateTime endOfDay = currentTime.Date.AddDays(1);
- // 计算时间差
- TimeSpan timeRemaining = endOfDay - currentTime;
- // 获取剩余秒数
- int secondsRemaining = (int)timeRemaining.TotalSeconds;
- // 如果剩余时间大于6小时,则返回距离今天结束剩余的秒数
- if(secondsRemaining > PostNewNotificationCacheSecs)
- {
- return secondsRemaining;
- }
- // 如果剩余时间不足6小时,则返回6小时
- else
- {
- return PostNewNotificationCacheSecs;
- }
- }
- /// <summary>
- /// 一个人发了动态之后,将状态缓存到Redis,判断是否需要通知
- /// </summary>
- public static string PostNewNotification(long userId)
- {
- return GenerateKey($"post_new:{userId}");
- }
- /// <summary>
- /// 一个人发了商品之后,将状态缓存到Redis,判断是否需要通知
- /// </summary>
- public static string GoodsNewNotification(long userId)
- {
- return GenerateKey($"mall:goods_new:{userId}");
- }
- /// <summary>
- /// 发商品后,1个自然日最多1条通知
- /// </summary>
- /// <returns></returns>
- public static int GoodsNewNotificationCacheTime()
- {
- // 获取当前时间
- DateTime currentTime = DateTime.Now;
- // 获取今天结束的时间(明天的凌晨)
- DateTime endOfDay = currentTime.Date.AddDays(1);
- // 计算时间差
- TimeSpan timeRemaining = endOfDay - currentTime;
- // 获取剩余秒数
- return (int)timeRemaining.TotalSeconds;
- }
- }
- }
|