Exemplo n.º 1
0
        /// <summary>
        /// 获取指定动态的评论数
        /// </summary>
        /// <param name="activityId">动态 ID</param>
        /// <exception cref="ArgumentNullException"><paramref name="activityId"/> 为 null</exception>
        /// <returns>动态的评论数</returns>
        public async Task <int> GetActivityCommentCountAsync([NotNull] string activityId)
        {
            if (activityId == null)
            {
                throw new ArgumentNullException(nameof(activityId));
            }
            var cacheKey    = ActivityCommentCountCacheKey(activityId);
            var redisDb     = _redis.GetDatabase();
            var cacheResult = await redisDb.StringGetAsync(cacheKey);

            if (cacheResult.HasValue)
            {
                await redisDb.KeyExpireAsync(cacheKey, CachedDataProvider.DefaultTtl);

                return((int)cacheResult);
            }

            var commentCount = await _dbContext.ActivityComments
                               .Where(c => c.ActivityId == activityId)
                               .CountAsync();

            await redisDb.StringSetAsync(cacheKey, commentCount, CachedDataProvider.DefaultTtl);

            return(commentCount);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            RedisProvider d     = new RedisProvider();
            var           redis = d.GetDatabase();

            User us = new User()
            {
                Id = 10000, Name = "loong", Password = "******"
            };

            redis.JsonHashSet("User", "10000", us);

            us = new User()
            {
                Id = 10001, Name = "loong2", Password = "******"
            };
            redis.JsonHashSet("User", "10001", us);

            us = redis.JsonHashGet <User>("User", "10000");

            ///////////////////////////////////////////////////////////////////////////////////

            string key = "Token:10000";

            redis.JsonSet(key, "0000000000000000", null);
            key = "Token:10001";
            redis.JsonSet(key, "0000000000000000", null);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 判断指定 Steam App 是否已入库
        /// </summary>
        /// <param name="userId">用户 ID</param>
        /// <param name="steamAppId">Steam App ID</param>
        /// <returns>如果已入库,返回 <c>true</c></returns>
        public async Task <bool> IsSteamAppInLibraryAsync(string userId, int steamAppId)
        {
            if (userId == null)
            {
                return(false);
            }
            var cacheKey = UserSteamAppLibraryCacheKey(userId);
            var redisDb  = _redis.GetDatabase();

            if (!await redisDb.KeyExistsAsync(cacheKey))
            {
                foreach (var appId in await _dbContext.UserSteamGameRecords.Where(r => r.UserId == userId)
                         .Select(r => r.SteamAppId).ToListAsync())
                {
                    await redisDb.SetAddAsync(cacheKey, appId);
                }
            }
            await redisDb.KeyExpireAsync(cacheKey, CachedDataProvider.DefaultTtl);

            return(await redisDb.SetContainsAsync(cacheKey, steamAppId));
        }
        /// <summary>
        ///     获取指定用户未读消息数
        /// </summary>
        /// <param name="userId">用户 ID</param>
        /// <exception cref="ArgumentNullException"><paramref name="userId"/> 为 null</exception>
        /// <returns>未读消息数</returns>
        public async Task <int> GetUserUnreadMessageCountAsync([NotNull] string userId)
        {
            if (userId == null)
            {
                throw new ArgumentNullException(nameof(userId));
            }

            var cacheKey     = UserUnreadMessageCountKey(userId);
            var redisDb      = _redis.GetDatabase();
            var cachedResult = await redisDb.StringGetAsync(cacheKey);

            if (cachedResult.HasValue)
            {
                await redisDb.KeyExpireAsync(cacheKey, CachedDataProvider.DefaultTtl);

                return((int)cachedResult);
            }

            var unreadCount = await _dbContext.Messages.CountAsync(m => m.ReceiverId == userId && m.Unread);

            await redisDb.StringSetAsync(cacheKey, unreadCount, CachedDataProvider.DefaultTtl);

            return(unreadCount);
        }
Exemplo n.º 5
0
        /// <summary>
        ///     获取指定用户获得的总认可数
        /// </summary>
        /// <param name="userId">用户 ID</param>
        /// <exception cref="ArgumentNullException"><paramref name="userId"/> 为 null</exception>
        /// <returns>用户获得的总认可数</returns>
        public async Task <int> GetUserLikeCountAsync([NotNull] string userId)
        {
            if (userId == null)
            {
                throw new ArgumentNullException(nameof(userId));
            }

            var cacheKey     = UserLikeCountCacheKey(userId);
            var redisDb      = _redis.GetDatabase();
            var cachedResult = await redisDb.StringGetAsync(cacheKey);

            if (cachedResult.HasValue)
            {
                await redisDb.KeyExpireAsync(cacheKey, CachedDataProvider.DefaultTtl);

                return((int)cachedResult);
            }

            var articleLikeCount = await(from article in _dbContext.Articles
                                         join like in _dbContext.Likes on article.Id equals like.TargetId
                                         where like.TargetType == LikeTargetType.Article && article.AuthorId == userId
                                         select article
                                         ).CountAsync();
            var articleCommentLikeCount = await(from comment in _dbContext.ArticleComments
                                                join like in _dbContext.Likes on comment.Id equals like.TargetId
                                                where like.TargetType == LikeTargetType.ArticleComment && comment.CommentatorId == userId
                                                select comment
                                                ).CountAsync();
            var activityLikeCount = await(from activity in _dbContext.Activities
                                          join like in _dbContext.Likes on activity.Id equals like.TargetId
                                          where like.TargetType == LikeTargetType.Activity && activity.AuthorId == userId
                                          select activity
                                          ).CountAsync();
            var activityCommentLikeCount = await(from comment in _dbContext.ActivityComments
                                                 join like in _dbContext.Likes on comment.Id equals like.TargetId
                                                 where like.TargetType == LikeTargetType.ActivityComment && comment.CommentatorId == userId
                                                 select comment
                                                 ).CountAsync();
            var conferenceEntryLikeCount = await(from entry in _dbContext.ConferenceEntries
                                                 join like in _dbContext.Likes on entry.Id equals like.TargetId
                                                 where like.TargetType == LikeTargetType.ConferenceEntry && entry.AuthorId == userId
                                                 select entry
                                                 ).CountAsync();

            var likeCount = articleLikeCount + articleCommentLikeCount + activityLikeCount +
                            activityCommentLikeCount + conferenceEntryLikeCount;
            await redisDb.StringSetAsync(cacheKey, likeCount, CachedDataProvider.DefaultTtl);

            return(likeCount);
        }
Exemplo n.º 6
0
        private async Task <string> InitUserSubscribedTargetsAsync([NotNull] string userId)
        {
            var cacheKey = UserSubscribedTargetsCacheKey(userId);
            var redisDb  = _redis.GetDatabase();

            if (!await redisDb.KeyExistsAsync(cacheKey))
            {
                foreach (var subscription in await _dbContext.Subscriptions.Where(s => s.SubscriberId == userId)
                         .Select(s => new { s.TargetId, s.TargetType }).ToListAsync())
                {
                    await redisDb.SetAddAsync(cacheKey,
                                              UserSubscribedTargetCacheValue(subscription.TargetId, subscription.TargetType));
                }
            }
            await redisDb.KeyExpireAsync(cacheKey, CachedDataProvider.DefaultTtl);

            return(cacheKey);
        }
Exemplo n.º 7
0
        public async Task Should_Redis_Set_By_Multiple_Threads()
        {
            var redisProvider = new RedisProvider(_redisOptions);
            var database      = redisProvider.GetDatabase();
            var tasks         = new List <Task>();
            var dic           = new Dictionary <int, int>();

            for (int i = 0; i < 1000; i++)
            {
                //tasks.Add(Task.Factory.StartNew(t => RunLock(database, (int)t), i));

                tasks.Add(RunLockAsync(database, i));

                //tasks.Add(Task.Factory.StartNew(t => database.StringSet(t.ToString(), "test", TimeSpan.FromSeconds(30)), i));

                //tasks.Add(database.StringSetAsync(i.ToString(), "test", TimeSpan.FromSeconds(30)));
            }

            await Task.WhenAll(tasks);

            await Task.Delay(5000);
        }