예제 #1
0
        public void ClearGroupWall(string groupId)
        {
            var key = GetEntryKey(FeedPrefix, groupId);
            var db  = StackExchangeRedisConnectionProvider.GetDataBase();

            db.KeyDelete(key);
        }
예제 #2
0
        public void RemovePost(string postId)
        {
            var entryKey = GetEntryKey(CacheEntryPrefix, postId);
            var db       = StackExchangeRedisConnectionProvider.GetDataBase();

            db.KeyDelete(entryKey);
        }
예제 #3
0
        public IEnumerable <string> GetGroupFollowers(string groupId)
        {
            var entryKey = GetEntryKey(GroupCachePrefix, groupId);
            var db       = StackExchangeRedisConnectionProvider.GetDataBase();

            return(db.SetMembers(entryKey).Select(p => p.ToString()));
        }
예제 #4
0
        public void IncrementPostLikeCount(string postId)
        {
            var entryKey = GetEntryKey(postLikeCounterPrefix, postId);
            var db       = StackExchangeRedisConnectionProvider.GetDataBase();

            db.StringIncrement(entryKey);
        }
예제 #5
0
        public void FollowUser(string userId, string followerId)
        {
            var entryKey = GetEntryKey(UserCachePrefix, userId);
            var db       = StackExchangeRedisConnectionProvider.GetDataBase();

            db.SetAdd(entryKey, followerId);
        }
예제 #6
0
        public void UnfollowGroup(string groupId, string followerId)
        {
            var entryKey = GetEntryKey(GroupCachePrefix, groupId);
            var db       = StackExchangeRedisConnectionProvider.GetDataBase();

            db.SetRemove(entryKey, followerId);
        }
예제 #7
0
        void ICommentCountCacheRepository.Increment(string postId)
        {
            var entryKey = GetEntryKey(RedisNameConstants.CommentCounterNamePrefix, postId);
            var db       = StackExchangeRedisConnectionProvider.GetDataBase();

            db.StringIncrement(entryKey);
        }
예제 #8
0
        public void Remove(string postId)
        {
            var entryKey = GetEntryKey(RedisNameConstants.CommentCounterNamePrefix, postId);
            var db       = StackExchangeRedisConnectionProvider.GetDataBase();

            db.KeyDelete(entryKey);
        }
        public void RemoveEvent(NewsfeedEventModel entry, IEnumerable <string> followers)
        {
            var activityEntry = MapToActivity(entry);
            var keys          = MapRedisKeys(entry.ReferencePostId, followers);
            var isPostRemoval = entry.ActionType == NewsfeedActionType.wallpost;


            var transaction = StackExchangeRedisConnectionProvider.GetTransaction();

            if (isPostRemoval)
            {
                foreach (var key in keys)
                {
                    transaction.SetRemoveAsync(key.PostFeedKey, entry.ReferencePostId);
                    transaction.KeyDeleteAsync(key.PostActionsKey);
                }
            }
            else
            {
                foreach (var key in keys)
                {
                    transaction.SortedSetDecrementAsync(key.PostActionsKey, activityEntry, 1);
                    transaction.SortedSetRemoveRangeByScoreAsync(key.PostActionsKey, -10, 0);
                }
            }
            transaction.Execute();
        }
예제 #10
0
        public void IncrementCommentLikeCount(long commentId)
        {
            var entryKey = GetEntryKey(commentLikeCounterPrefix, commentId.ToString());
            var db       = StackExchangeRedisConnectionProvider.GetDataBase();

            db.StringIncrement(entryKey);
        }
예제 #11
0
        public void SavePost(WallPostCacheModel model)
        {
            var entryKey = GetEntryKey(CacheEntryPrefix, model.Id);
            var item     = JsonConvert.SerializeObject(model);
            var db       = StackExchangeRedisConnectionProvider.GetDataBase();

            db.StringSet(entryKey, item);
        }
예제 #12
0
        public int GetCommentCount(string postId)
        {
            var entryKey = GetEntryKey(RedisNameConstants.CommentCounterNamePrefix, postId);
            var db       = StackExchangeRedisConnectionProvider.GetDataBase();
            var value    = db.StringGet(entryKey);

            return(!string.IsNullOrWhiteSpace(value) ? Convert.ToInt32(value) : 0);
        }
예제 #13
0
        public int GetCommentLikeCount(long commentId)
        {
            var entryKey = GetEntryKey(commentLikeCounterPrefix, commentId.ToString());
            var db       = StackExchangeRedisConnectionProvider.GetDataBase();
            var value    = db.StringGet(entryKey);

            return(!string.IsNullOrWhiteSpace(value) ? Convert.ToInt32(value) : 0);
        }
예제 #14
0
        public WallPostCacheModel GetPost(string postId)
        {
            var entryKey = GetEntryKey(CacheEntryPrefix, postId);
            var db       = StackExchangeRedisConnectionProvider.GetDataBase();
            var item     = db.StringGet(entryKey);

            return(!string.IsNullOrWhiteSpace(item) ? JsonConvert.DeserializeObject <WallPostCacheModel>(item) : null);
        }
예제 #15
0
        public void RefreshGroupWall(string groupId, IEnumerable <string> postIds)
        {
            var key         = GetEntryKey(FeedPrefix, groupId);
            var transaction = StackExchangeRedisConnectionProvider.GetTransaction();

            foreach (var postId in postIds)
            {
                transaction.SetAddAsync(key, postId);
            }
            transaction.Execute();
        }
예제 #16
0
        public void RemovePosts(IEnumerable <string> postIds)
        {
            var keyList = new List <RedisKey>();

            foreach (var postId in postIds)
            {
                keyList.Add(GetEntryKey(CacheEntryPrefix, postId));
            }
            var db = StackExchangeRedisConnectionProvider.GetDataBase();

            db.KeyDelete(keyList.ToArray());
        }
        public void AddEvent(NewsfeedEventModel entry, IEnumerable <string> followers)
        {
            var activityEntry = MapToActivity(entry);
            var keys          = MapRedisKeys(entry.ReferencePostId, followers);

            var transaction = StackExchangeRedisConnectionProvider.GetTransaction();

            foreach (var key in keys)
            {
                transaction.SetAddAsync(key.PostFeedKey, entry.ReferencePostId);
                transaction.SortedSetIncrementAsync(key.PostActionsKey, activityEntry, 1);
            }
            transaction.Execute();
        }
예제 #18
0
        public IEnumerable <WallPostCacheModel> GetItems(IEnumerable <string> postIds)
        {
            var result    = new List <WallPostCacheModel>();
            var entryKeys = postIds.Select(t => GetEntryKey(CacheEntryPrefix, t)).Select(p => (RedisKey)p).ToArray();
            var db        = StackExchangeRedisConnectionProvider.GetDataBase();
            var results   = db.StringGet(entryKeys);

            foreach (var resultItem in results)
            {
                if (!string.IsNullOrWhiteSpace(resultItem))
                {
                    result.Add(JsonConvert.DeserializeObject <WallPostCacheModel>(resultItem));
                }
            }

            return(result);
        }
        public void UpdateFeed(string userId, IEnumerable <NewsfeedEventModel> events)
        {
            var feedKey          = GetEntryKey(FeedPrefix, userId);
            var server           = StackExchangeRedisConnectionProvider.GetServer();
            var existingFeedKeys = server.Keys(pattern: feedKey).Select(p => (RedisKey)p).ToArray();

            var transaction = StackExchangeRedisConnectionProvider.GetTransaction();

            transaction.KeyDeleteAsync(existingFeedKeys);

            foreach (var userEvent in events)
            {
                var activityEntry = MapToActivity(userEvent);
                var actionKey     = GetEntryKey(FeedPrefix, string.Concat(userId, ":", userEvent.ReferencePostId));
                transaction.SetAddAsync(feedKey, userEvent.ReferencePostId);
                transaction.SortedSetIncrementAsync(actionKey, activityEntry, 1);
            }
            transaction.Execute();
        }
        public IEnumerable <NewsfeedResponseModel> GetUserFeed(string userId, int skip, int take)
        {
            var userFeedKey = GetEntryKey(FeedPrefix, userId);
            List <NewsfeedResponseModel> responseItems = new List <NewsfeedResponseModel>();
            var startIndex = skip;
            var counter    = 0;
            var db         = StackExchangeRedisConnectionProvider.GetDataBase();
            var allPostIds = db.SetMembers(userFeedKey).ToList();

            if (allPostIds != null && allPostIds.Count > 0)
            {
                IEnumerable <RedisValue> postIds;
                if (allPostIds.Count > take)
                {
                    postIds = allPostIds.Skip(skip).Take(take);
                }
                else
                {
                    postIds = allPostIds.Take(take);
                }
                foreach (var postId in postIds)
                {
                    //Read post from cache
                    var currentPost = wallPostRepo.GetPost(postId);
                    if (currentPost != null)
                    {
                        var userActionsOnPostKey = GetEntryKey(FeedPrefix, string.Concat(userId, ":", postId));
                        //10000 is just a random rank assumed to be the max events on a post
                        var userActionsOnPost = db.SortedSetRangeByScoreWithScores(userActionsOnPostKey, 0, 10000);
                        if (userActionsOnPost != null && userActionsOnPost.Length > 0)
                        {
                            var mappedActions = new List <NewsfeedAction>();
                            foreach (var action in userActionsOnPost)
                            {
                                var values = action.Element.ToString().Split(':');
                                mappedActions.Add(new NewsfeedAction {
                                    Action = (NewsfeedActionType)Convert.ToInt16(values[1]), By = values[0]
                                });
                            }
                            var totalCommentCount = commentCountRepo.GetCommentCount(currentPost.Id);
                            var totalLikeCount    = entryLikeRepo.GetPostLikeCount(currentPost.Id);

                            var model = new NewsfeedResponseModel
                            {
                                Body         = currentPost.Body,
                                CreatedDate  = currentPost.CreatedDate,
                                Id           = currentPost.Id,
                                ModifiedDate = currentPost.ModifiedDate,
                                PostedBy     = currentPost.PostedBy,
                                PostType     = currentPost.PostType,
                                WallOwner    = new WallModel {
                                    OwnerId = currentPost.TargetWall.Id, WallOwnerType = (WallType)currentPost.TargetWall.WallOwnerTypeId
                                },
                                FeedDescription = mappedActions,
                                LikeCount       = totalLikeCount,
                                CommentCount    = totalCommentCount
                            };
                            responseItems.Add(model);

                            counter++;
                            if (counter == take)
                            {
                                break;
                            }
                        }
                        else
                        {
                            db.SetRemove(userFeedKey, postId);
                            db.KeyDelete(userActionsOnPostKey);
                        }
                    }
                    else
                    {
                        db.SetRemove(userFeedKey, postId);
                    }
                }
            }
            return(responseItems);
        }
예제 #21
0
        public IEnumerable <GroupWallResponseModel> GetGroupWall(string groupId)
        {
            var result     = new List <GroupWallResponseModel>();
            var key        = GetEntryKey(FeedPrefix, groupId);
            var db         = StackExchangeRedisConnectionProvider.GetDataBase();
            var allPostIds = db.SetMembers(key).ToList();

            if (allPostIds != null && allPostIds.Count > 0)
            {
                var postKeys = allPostIds.Select(p => (RedisKey)GetEntryKey(RedisNameConstants.WallPostRepoPrefix, p)).ToArray();
                var likeKeys = allPostIds.Select(p => (RedisKey)GetEntryKey(RedisNameConstants.PostLikeCounterNamePrefix, p)).ToArray();
                var comment  = allPostIds.Select(p => (RedisKey)GetEntryKey(RedisNameConstants.CommentCounterNamePrefix, p)).ToArray();
                //foreach (var postId in allPostIds)
                //{

                //var transaction = db.CreateTransaction();
                //var posts = transaction.StringGetAsync(postKeys);
                var posts = db.StringGet(postKeys);
                foreach (var post in posts)
                {
                    var currentPost = JsonConvert.DeserializeObject <WallPostCacheModel>(post);

                    var model = new GroupWallResponseModel
                    {
                        Body         = currentPost.Body,
                        CreatedDate  = currentPost.CreatedDate,
                        Id           = currentPost.Id,
                        ModifiedDate = currentPost.ModifiedDate,
                        PostedBy     = currentPost.PostedBy,
                        PostType     = currentPost.PostType,
                        WallOwner    = new WallModel {
                            OwnerId = currentPost.TargetWall.Id, WallOwnerType = (WallType)currentPost.TargetWall.WallOwnerTypeId
                        },
                    };

                    var currentCommentCountEntry = GetEntryKey(RedisNameConstants.CommentCounterNamePrefix, currentPost.Id);
                    var totalCommentCountEntry   = db.StringGet(currentCommentCountEntry);
                    model.CommentCount = !string.IsNullOrWhiteSpace(totalCommentCountEntry) ? Convert.ToInt32(totalCommentCountEntry) : 0;

                    var totalLikeCountEntryKey = GetEntryKey(RedisNameConstants.PostLikeCounterNamePrefix, currentPost.Id);
                    var value = db.StringGet(totalLikeCountEntryKey);
                    model.LikeCount = !string.IsNullOrWhiteSpace(value) ? Convert.ToInt32(value) : 0;
                    result.Add(model);
                }
                //JsonConvert.DeserializeObject<List<WallPostCacheModel>>();


                //    var comments = transaction.StringGetAsync(comment);
                //    transaction.Execute();


                // var entryKey = GetEntryKey(RedisNameConstants.WallPostRepoPrefix, postId);
                // var currentPostValue = db.StringGet(entryKey);
                //// var currentPost =  !string.IsNullOrWhiteSpace(currentPostValue) ? JsonConvert.DeserializeObject<WallPostCacheModel>(currentPostValue) : null;
                // if (!string.IsNullOrWhiteSpace(currentPostValue))
                // {
                //     //var currentCommentCountEntry = GetEntryKey(RedisNameConstants.CommentCounterNamePrefix, postId);
                //     //var totalCommentCountEntry = db.StringGet(currentCommentCountEntry);
                //     //var totalCommentCount = !string.IsNullOrWhiteSpace(totalCommentCountEntry) ? Convert.ToInt32(totalCommentCountEntry) : 0;

                //     //var totalLikeCountEntryKey = GetEntryKey(RedisNameConstants.PostLikeCounterNamePrefix, postId);
                //     //var value = db.StringGet(entryKey);
                //     //var totalLikeCount =  !string.IsNullOrWhiteSpace(value) ? Convert.ToInt32(value) : 0;

                //     var model = new GroupWallResponseModel
                //     {
                //         //Body = currentPost.Body,
                //         //CreatedDate = currentPost.CreatedDate,
                //         Id = "123"//,
                //         //ModifiedDate = currentPost.ModifiedDate,
                //         //PostedBy = currentPost.PostedBy,
                //         //PostType = currentPost.PostType,
                //         //WallOwner = new WallModel { OwnerId = currentPost.TargetWall.Id, WallOwnerType = (WallType)currentPost.TargetWall.WallOwnerTypeId },
                //         //LikeCount = totalLikeCount,
                //         //CommentCount = totalCommentCount
                //     };
                //     result.Add(model);
                //}
                //}
            }
            return(result);
        }