コード例 #1
0
        public bool AcquireSyncLock(string type, int giftId)
        {
            var key   = string.Format(SYNC_ROOT_KEY_FMT, type, giftId);
            var redis = _redisHelper.GetDatabase();

            return(redis.StringSet(key, 0, TimeSpan.FromSeconds(60), when: StackExchange.Redis.When.NotExists));
        }
コード例 #2
0
        public DisplayNotice ShouldDisplayNotice(string sign, int theMonth)
        {
            var    redis      = _redisHelper.GetDatabase();
            string awardeeKey = string.Format(KEY_AWARDEE_NOTICE, theMonth);

            if (redis.KeyExists(awardeeKey))
            {
                if (!_redisHelper.SetContains(awardeeKey, sign))
                {
                    return(DisplayNotice.Awardee);
                }
            }

            string awardKey = string.Format(KEY_AWARD_NOTICE, theMonth);

            if (redis.KeyExists(awardKey))
            {
                if (!_redisHelper.SetContains(awardKey, sign))
                {
                    return(DisplayNotice.Award);
                }
            }

            return(DisplayNotice.None);
        }
コード例 #3
0
        public async Task <List <long> > WhichHasContainsMID(long mid, IEnumerable <long> cids)
        {
            var redis = _redisHelper.GetDatabase();

            var batch = redis.CreateBatch();

            var result = new List <long>();
            var tasks  = new List <Task>();

            foreach (var item in cids)
            {
                var task = batch.SetContainsAsync(string.Format(KEY_COLLECTION_MOTTO_FMT, item.ToString()), mid).ContinueWith(t =>
                {
                    if (t.Result)
                    {
                        result.Add(item);
                    }
                });

                tasks.Add(task);
            }

            batch.Execute();
            await Task.WhenAll(tasks);

            return(result);
        }
コード例 #4
0
        public bool HasUserInRole(string userId, string role)
        {
            var redis  = _redisHelper.GetDatabase();
            var result = (string)redis.StringGet(string.Format(USER_ROLE_KEY_FMT, userId, role));

            return("YES".Equals(result));
        }
コード例 #5
0
        public async Task <Motto[]> GetMottos(int theDay, int page, int pSize)
        {
            var db  = _redisHelper.GetDatabase();
            var ids = db.SortedSetRangeByScore(string.Format(KEY_RANK_MOTTO_FMT, theDay.ToString()),
                                               order: Order.Descending,
                                               skip: (page - 1) * pSize,
                                               take: pSize);

            if (ids != null && ids.Length > 0)
            {
                var batch = db.CreateBatch();
                var tasks = new List <Task <Motto> >();
                foreach (var item in ids)
                {
                    var t = batch.HashGetAllAsync(string.Format(KEY_EVAL_MOTTO_FMT, theDay.ToString(), (string)item)).ContinueWith(tresult =>
                    {
                        if (tresult.IsCompleted)
                        {
                            if (tresult.Result != null && tresult.Result.Length > 0)
                            {
                                var motto = ConvertFromHashEntries(tresult.Result);
                                motto.ID  = Convert.ToInt64(item);
                                return(motto);
                            }
                        }

                        return(null);
                    });

                    tasks.Add(t);
                }

                batch.Execute();

                return(await Task.WhenAll(tasks));
            }

            return(null);
        }
コード例 #6
0
        public User GetUserById(string uid)
        {
            var redis   = _redisHelper.GetDatabase();
            var entries = redis.HashGetAll(string.Format(KEY_USER_INFO_FMT, uid));

            if (entries != null && entries.Length >= 16) //如果用户的信息数缺失时,将会重新加载用户信息
            {
                var user = ConvertFromHashEntries(entries);
                user.Id = uid;
                return(user);
            }

            return(null);
        }