protected override void UpdateCache(CommandResponse result) { if (result.Success) { //Cleare user cache CacheHandler.Instance.Remove(CachingKey.UserBlocks(_options.UserName)); CacheHandler.Instance.Remove(CachingKey.UserInformation(_options.UserName)); CacheHandler.Instance.Remove(CachingKey.UserOverview(_options.UserName)); CacheHandler.Instance.Remove(CachingKey.UserPreferences(_options.UserName)); CacheHandler.Instance.Remove(CachingKey.UserSubscriptions(_options.UserName)); CacheHandler.Instance.Remove(CachingKey.UserData(_options.UserName)); CacheHandler.Instance.Remove(CachingKey.UserBlocks(_options.UserName)); CacheHandler.Instance.Remove(CachingKey.UserRecord(_options.UserName)); //CacheHandler.Instance.Remove(CachingKey.UserSavedItems(_options.UserName)); } }
// return user statistics for user profile overview public static UserStatsModel UserStatsModel(string userName) { var loadFunc = new Func <UserStatsModel>(() => { var userStatsModel = new UserStatsModel(); using (var db = new VoatOutOfRepositoryDataContextAccessor()) { // 5 subverses user submitted to most var subverses = db.Submission.Where(a => a.UserName == userName && !a.IsAnonymized && !a.IsDeleted) .GroupBy(a => new { a.UserName, a.Subverse }) .Select(g => new SubverseStats { SubverseName = g.Key.Subverse, Count = g.Count() }) .OrderByDescending(s => s.Count) .Take(5) .ToList(); // total comment count var comments = db.Comment.Count(a => a.UserName == userName && !a.IsDeleted); // voting habits var userData = new Domain.UserData(userName); var commentUpvotes = userData.Information.CommentVoting.UpCount; var commentDownvotes = userData.Information.CommentVoting.DownCount; var submissionUpvotes = userData.Information.SubmissionVoting.UpCount; var submissionDownvotes = userData.Information.SubmissionVoting.DownCount; //var commentUpvotes = db.CommentVoteTrackers.Count(a => a.UserName == userName && a.VoteStatus == 1); //var commentDownvotes = db.CommentVoteTrackers.Count(a => a.UserName == userName && a.VoteStatus == -1); //var submissionUpvotes = db.SubmissionVoteTrackers.Count(a => a.UserName == userName && a.VoteStatus == 1); //var submissionDownvotes = db.SubmissionVoteTrackers.Count(a => a.UserName == userName && a.VoteStatus == -1); // get 3 highest rated comments var highestRatedComments = db.Comment .Include(x => x.Submission) .Where(a => a.UserName == userName && !a.IsAnonymized && !a.IsDeleted) .OrderByDescending(s => s.UpCount - s.DownCount) .Take(3) .ToList(); // get 3 lowest rated comments var lowestRatedComments = db.Comment .Include(x => x.Submission) .Where(a => a.UserName == userName && !a.IsAnonymized && !a.IsDeleted) .OrderBy(s => s.UpCount - s.DownCount) .Take(3) .ToList(); var linkSubmissionsCount = db.Submission.Count(a => a.UserName == userName && a.Type == 2 && !a.IsDeleted && !a.IsAnonymized); var messageSubmissionsCount = db.Submission.Count(a => a.UserName == userName && a.Type == 1 && !a.IsDeleted && !a.IsAnonymized); // get 5 highest rated submissions var highestRatedSubmissions = db.Submission.Where(a => a.UserName == userName && !a.IsAnonymized && !a.IsDeleted) .OrderByDescending(s => s.UpCount - s.DownCount) .Take(5) .ToList(); // get 5 lowest rated submissions var lowestRatedSubmissions = db.Submission.Where(a => a.UserName == userName && !a.IsAnonymized && !a.IsDeleted) .OrderBy(s => s.UpCount - s.DownCount) .Take(5) .ToList(); userStatsModel.TopSubversesUserContributedTo = subverses; userStatsModel.LinkSubmissionsSubmitted = linkSubmissionsCount; userStatsModel.MessageSubmissionsSubmitted = messageSubmissionsCount; userStatsModel.LowestRatedSubmissions = lowestRatedSubmissions; userStatsModel.HighestRatedSubmissions = highestRatedSubmissions; userStatsModel.TotalCommentsSubmitted = comments; userStatsModel.HighestRatedComments = highestRatedComments; userStatsModel.LowestRatedComments = lowestRatedComments; userStatsModel.TotalCommentsUpvoted = commentUpvotes; userStatsModel.TotalCommentsDownvoted = commentDownvotes; userStatsModel.TotalSubmissionsUpvoted = submissionUpvotes; userStatsModel.TotalSubmissionsDownvoted = submissionDownvotes; ////HACK: EF causes JSON to StackOverflow on the highest/lowest comments because of the nested loading EF does with the include option, therefore null the refs here. //highestRatedComments.ForEach(x => x.Submission.Comments = null); //lowestRatedComments.ForEach(x => x.Submission.Comments = null); } return(userStatsModel); }); var cachedData = CacheHandler.Instance.Register(CachingKey.UserOverview(userName), loadFunc, TimeSpan.FromMinutes(30)); return(cachedData); }