/// <summary> /// 更新用户未读消息 /// </summary> /// <param name="unReadUpdateData"></param> public void UpdateUnReadDataCache(List <UnReadMsg> unReadUpdateData) { string unReadKey = MsCacheName.GetUserUnReadData; var unReadData = MsCacheHelper.GetCache <List <UnReadMsg> >(unReadKey); if (unReadData == null) { ResetCache(); } else { //遍历,如果有记录,那么加1,否则就添加一条 foreach (var item in unReadUpdateData) { var first = unReadData.FirstOrDefault(x => x.clientUserId == item.clientUserId && x.GroupId == item.GroupId && x.MessageType == item.MessageType); if (first != null) { first.UnReadCount += 1; first.AllCount += 1; } else { unReadData.Add(item); } } //重置缓存数据 SetUnReadDataCache(unReadData); } }
/// <summary> /// 根据lambda表达式更新相关数据 /// </summary> /// <param name="predicate"></param> /// <returns></returns> public List <UnReadMsg> UpdateUnReadDataFromCacheByPredicate(Func <UnReadMsg, bool> predicate, int count = 0) { string unReadKey = MsCacheName.GetUserUnReadData; var unReadData = MsCacheHelper.GetCache <List <UnReadMsg> >(unReadKey); if (unReadData == null) { ResetCache(); } else { //遍历 var list = unReadData.Where(predicate).ToList(); foreach (var item in list) { count = count == 0 ? item.UnReadCount : count; unReadData.Add(new UnReadMsg { clientUserId = item.clientUserId, UnReadCount = item.UnReadCount - count, GroupId = item.GroupId, AllCount = item.AllCount - count,//减去清零的那部分 MessageType = item.MessageType }); unReadData.Remove(item); } //重置缓存数据 SetUnReadDataCache(unReadData); } return(unReadData); }
/// <summary> /// 私有方法,获取所有未读信息 /// </summary> /// <returns></returns> private List <UnReadMsg> GetAllUnReadDataFromCache() { string unReadKey = MsCacheName.GetUserUnReadData; var unReadData = MsCacheHelper.GetCache <List <UnReadMsg> >(unReadKey); if (unReadData == null) { //从队列导入到数据库 unReadData = ResetCache(); } return(unReadData ?? new List <UnReadMsg>()); }
/// <summary> /// 获取组用户cache /// </summary> /// <param name="groupId"></param> /// <returns></returns> public GroupUser GetGroupUserFromCache(string groupId) { string groupAllUserKey = string.Format(MsCacheName.GetGroupAllUser, groupId); var groupUser = MsCacheHelper.GetCache <GroupUser>(groupAllUserKey); if (groupUser == null) { //从数据库获取,赋值给groupuser groupUser = UnReadDataBLL.Instance.GetEntMembers(groupId); if (groupUser != null && groupUser.clientUserIds.Count > 0) { SetGroupUserCache(groupUser, groupId); } } return(groupUser ?? new GroupUser() { GroupId = "0", clientUserIds = new List <string>() }); }