예제 #1
0
        /// <summary>
        /// 取得名单列表
        /// </summary>
        public async Task <IEnumerable <ListItem> > GetRosterListAsync(RosterType rosterType, string api, Policy policy, string policyKey)
        {
            var key = FromatRosterKey(rosterType, api, policy, policyKey);

            //判断是否存在key
            if (await _db.ExistsAsync(key))
            {
                //取得数据
                var data = await _db.ZRangeWithScoresAsync(key, 0, -1);

                return(data.Select(x => new ListItem {
                    Value = x.member, ExpireTicks = x.score
                }));
            }
            else
            {
                var data = (await _storage.GetRosterListAsync(rosterType, api, policy, policyKey)).ToList();

                //Ip地址转换
                if (policy == Policy.Ip)
                {
                    foreach (var item in data)
                    {
                        item.Value = CommonHelper.IpToNum(item.Value);
                        //保存
                        await _db.ZAddAsync(key, (item.ExpireTicks, item.Value));
                    }
                }
                //设置缓存过期时间
                await _db.ExpireAsync(key, expiry.Value);

                return(data);
            }
        }
예제 #2
0
        /// <summary>
        /// 取得名单列表
        /// </summary>
        public async Task <IEnumerable <ListItem> > GetRosterListAsync(RosterType rosterType, string api, Policy policy, string policyKey)
        {
            var key = FromatRosterKey(rosterType, api, policy, policyKey);

            //判断是否存在key
            if (await _db.KeyExistsAsync(key))
            {
                //取得数据
                var data = await _db.SortedSetRangeByScoreWithScoresAsync(key, start : DateTime.Now.Ticks, stop : double.PositiveInfinity, exclude : Exclude.Both, order : Order.Ascending);

                return(data.Select(x => new ListItem {
                    Value = (string)x.Element, ExpireTicks = x.Score
                }));
            }
            else
            {
                var data = (await _storage.GetRosterListAsync(rosterType, api, policy, policyKey)).ToList();
                //Ip地址转换
                if (policy == Policy.Ip)
                {
                    foreach (var item in data)
                    {
                        item.Value = Common.IpToNum(item.Value);
                    }
                }
                SortedSetEntry[] entrys = data.Select(x => new SortedSetEntry(x.Value, x.ExpireTicks)).ToArray();
                //保存
                await _db.SortedSetAddAsync(key, entrys);

                //设置缓存过期时间
                await _db.KeyExpireAsync(key, expiry);

                return(data);
            }
        }
예제 #3
0
        public async Task <IEnumerable <ListItem> > GetRosterListAsync(RosterType rosterType, string api, Policy policy, string policyKey)
        {
            var key = FromatRosterKey(rosterType, api, policy, policyKey);

            var result = await Task.FromResult((IEnumerable <ListItem>) _cache.Get(key));

            throw new NotImplementedException();
        }
예제 #4
0
        /// <summary>
        /// 移除名单
        /// </summary>
        /// <param name="policy">策略</param>
        /// <param name="item">项目</param>
        public async Task RemoveRosterAsync(RosterType rosterType, string api, Policy policy, string policyKey, params string[] item)
        {
            //从名单中移除
            await _storage.RemoveRosterAsync(rosterType, api, policy, policyKey, item);

            //清除缓存
            await _cache.ClearRosterListCacheAsync(rosterType, api, policy, policyKey);
        }
예제 #5
0
 /// <summary>
 /// 删除名单中数据
 /// </summary>
 /// <param name="rosterType">名单类型</param>
 /// <param name="api">API</param>
 /// <param name="policy">策略</param>
 /// <param name="policyKey">策略Key</param>
 /// <param name="expiry">过期时间</param>
 /// <param name="item">项目</param>
 public async Task RemoveRosterAsync(RosterType rosterType, string api, Policy policy, string policyKey, params string[] item)
 {
     if (item == null || item.Length == 0)
     {
         return;
     }
     var key = FromatRosterKey(rosterType, api, policy, policyKey);
     //删除
     await _db.ZRemAsync(key, item);
 }
        /// <summary>
        /// 取得名单列表
        /// </summary>
        /// <param name="rosterType">名单类型</param>
        /// <param name="api">API</param>
        /// <param name="policy">策略</param>
        /// <param name="policyKey">策略Key</param>
        public async Task <IEnumerable <ListItem> > GetRosterListAsync(RosterType rosterType, string api, Policy policy, string policyKey)
        {
            var key = FromatRosterKey(rosterType, api, policy, policyKey);

            //取得数据
            var data = await _db.SortedSetRangeByScoreWithScoresAsync(key, start : DateTime.Now.Ticks, stop : double.PositiveInfinity, exclude : Exclude.Both, order : Order.Ascending);

            return(data.Select(x => new ListItem {
                Value = (string)x.Element, ExpireTicks = x.Score
            }));
        }
예제 #7
0
        private string FromatRosterKey(RosterType rosterType, string api, Policy policy, string policyKey)
        {
            var key = $"{_options.CacheKeyPrefix}:{rosterType.ToString().ToLower()}:{policy.ToString().ToLower()}";

            if (!string.IsNullOrEmpty(policyKey))
            {
                key += ":" + Common.EncryptMD5Short(policyKey);
            }
            key += ":" + api.ToLower();
            return(key);
        }
예제 #8
0
        private string FromatRosterKey(RosterType rosterType, string api, Policy policy, string policyKey)
        {
            var key = $"apithrottle:cache:{rosterType.ToString().ToLower()}:{policy.ToString().ToLower()}";

            if (!string.IsNullOrEmpty(policyKey))
            {
                key += ":" + MD5Util.EncryptMD5Short(policyKey);
            }
            key += ":" + api.ToLower();
            return(key);
        }
예제 #9
0
        /// <summary>
        /// 添加名单
        /// </summary>
        /// <param name="rosterType">名单类型</param>
        /// <param name="policy">策略</param>
        /// <param name="policyKey">策略Key</param>
        /// <param name="expiry">过期时间</param>
        /// <param name="item">项目</param>
        /// <remarks>因为要保存过期时间,所以名单通过Redis 有序集合(sorted set)来存储,score来存储过期时间Ticks</remarks>
        public async Task AddRosterAsync(RosterType rosterType, string api, Policy policy, string policyKey, TimeSpan?expiry, params string[] item)
        {
            //保存名单
            await _storage.AddRosterAsync(rosterType, api, policy, policyKey, expiry, item);

            //从反名单中移除
            await _storage.RemoveRosterAsync(rosterType == RosterType.BlackList?RosterType.WhiteList : RosterType.BlackList, api, policy, policyKey, item);

            //清除缓存
            await _cache.ClearRosterListCacheAsync(rosterType, api, policy, policyKey);

            await _cache.ClearRosterListCacheAsync(rosterType == RosterType.BlackList?RosterType.WhiteList : RosterType.BlackList, api, policy, policyKey);
        }
예제 #10
0
        /// <summary>
        /// 取得名单列表
        /// </summary>
        /// <param name="rosterType">名单类型</param>
        /// <param name="api">API</param>
        /// <param name="policy">策略</param>
        /// <param name="policyKey">策略Key</param>
        public async Task <IEnumerable <ListItem> > GetRosterListAsync(RosterType rosterType, string api, Policy policy, string policyKey)
        {
            var key = FromatRosterKey(rosterType, api, policy, policyKey);

            //取得数据
            var data = await _db.ZRangeWithScoresAsync(key, 0, -1);

            foreach (var item in data)
            {
            }
            return(data.Select(x => new ListItem {
                Value = x.member, ExpireTicks = x.score
            }));
        }
        /// <summary>
        /// 删除名单中数据
        /// </summary>
        /// <param name="rosterType">名单类型</param>
        /// <param name="api">API</param>
        /// <param name="policy">策略</param>
        /// <param name="policyKey">策略Key</param>
        /// <param name="expiry">过期时间</param>
        /// <param name="item">项目</param>
        public async Task RemoveRosterAsync(RosterType rosterType, string api, Policy policy, string policyKey, params string[] item)
        {
            if (item == null || item.Length == 0)
            {
                return;
            }
            //删除数据
            RedisValue[] delValues = new RedisValue[item.Length];
            for (int i = 0; i < item.Length; i++)
            {
                delValues[i] = item[i];
            }
            var key = FromatRosterKey(rosterType, api, policy, policyKey);

            //删除
            await _db.SortedSetRemoveAsync(key, delValues);
        }
예제 #12
0
        /// <summary>
        /// 添加名单
        /// </summary>
        /// <param name="rosterType">名单类型</param>
        /// <param name="policy">策略</param>
        /// <param name="policyKey">策略Key</param>
        /// <param name="expiry">过期时间</param>
        /// <param name="item">项目</param>
        /// <remarks>因为要保存过期时间,所以名单通过Redis 有序集合(sorted set)来存储,score来存储过期时间Ticks</remarks>
        public async Task AddRosterAsync(RosterType rosterType, string api, Policy policy, string policyKey, TimeSpan?expiry, params string[] item)
        {
            if (item == null || item.Length == 0)
            {
                return;
            }

            //过期时间计算
            double score = expiry == null ? double.PositiveInfinity : DateTime.Now.Add(expiry.Value).Ticks;


            var key = FromatRosterKey(rosterType, api, policy, policyKey);
            //保存
            await _db.ZAddAsync(key, (score, item));

            //删除过期名单数据
            await _db.ZRemRangeByScoreAsync(key, 0, DateTime.Now.Ticks);
        }
예제 #13
0
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string ID = e.CommandArgument.ToString().Trim();
        if (e.CommandName == "CustomDelete")
        {

            TList<DoctorRoster> listDoctorRoster = DataRepository.DoctorRosterProvider.GetByRosterTypeId((Int32.Parse(ID)));
            if (listDoctorRoster.Count > 0)
            {
                Response.Write(@"<script language='javascript'>alert('Vui lòng xóa tất cả chi tiết.')</script>");

            }
            else
            {
                RosterType objRosterType= new RosterType();
                objRosterType.Id= Int32.Parse(ID);
                DataRepository.RosterTypeProvider.Delete(objRosterType);
            }
            GridView1.DataBind();
        }
    }
        /// <summary>
        /// 添加名单
        /// </summary>
        /// <param name="rosterType">名单类型</param>
        /// <param name="policy">策略</param>
        /// <param name="policyKey">策略Key</param>
        /// <param name="expiry">过期时间</param>
        /// <param name="item">项目</param>
        /// <remarks>因为要保存过期时间,所以名单通过Redis 有序集合(sorted set)来存储,score来存储过期时间Ticks</remarks>
        public async Task AddRosterAsync(RosterType rosterType, string api, Policy policy, string policyKey, TimeSpan?expiry, params string[] item)
        {
            if (item == null || item.Length == 0)
            {
                return;
            }

            //过期时间计算
            double score = expiry == null ? double.PositiveInfinity : DateTime.Now.Add(expiry.Value).Ticks;

            SortedSetEntry[] values = new SortedSetEntry[item.Length];
            for (int i = 0; i < item.Length; i++)
            {
                values[i] = new SortedSetEntry(item[i], score);
            }
            var key = FromatRosterKey(rosterType, api, policy, policyKey);
            //保存
            await _db.SortedSetAddAsync(key, values);

            //删除过期名单数据
            await _db.SortedSetRemoveRangeByScoreAsync(key, 0, DateTime.Now.Ticks, Exclude.Both);
        }
예제 #15
0
        /// <summary>
        /// 取得名单列表(分页)
        /// </summary>
        /// <param name="rosterType">名单类型</param>
        /// <param name="api">API</param>
        /// <param name="policy">策略</param>
        /// <param name="policyKey">策略Key</param>
        public async Task <(long count, IEnumerable <ListItem> items)> GetRosterListAsync(RosterType rosterType, string api, Policy policy, string policyKey, long skip, long take)
        {
            var key = FromatRosterKey(rosterType, api, policy, policyKey);
            //取得件数
            var count = await _db.ZCardAsync(key);

            if (count == 0)
            {
                return(0, new List <ListItem>());
            }

            //取得数据
            var data = await _db.ZRangeWithScoresAsync(key, 0, -1);

            return(count, data.Select(x => new ListItem {
                Value = x.member, ExpireTicks = x.score
            }));
        }
예제 #16
0
 /// <summary>
 /// 清除名单缓存
 /// </summary>
 /// <returns></returns>
 public async Task ClearRosterListCacheAsync(RosterType rosterType, string api, Policy policy, string policyKey)
 {
     var key = FromatRosterKey(rosterType, api, policy, policyKey);
     await _db.DelAsync(key);
 }
 public Task <IEnumerable <ListItem> > GetRosterListAsync(RosterType rosterType, string api, Policy policy, string policyKey)
 {
     throw new NotImplementedException();
 }
예제 #18
0
 public Task ClearRosterListCacheAsync(RosterType rosterType, string api, Policy policy, string policyKey)
 {
     throw new NotImplementedException();
 }
예제 #19
0
 /// <summary>
 /// 取得全局名单列表(分页)
 /// </summary>
 /// <param name="rosterType">名单类型</param>
 /// <param name="policy">策略</param>
 /// <param name="policyKey">策略Key</param>
 public async Task <(long count, IEnumerable <ListItem> items)> GetGlobalRosterListAsync(RosterType rosterType, Policy policy, string policyKey, long skip, long take)
 {
     return(await GetRosterListAsync(rosterType, CommonHelper.GlobalApiKey, policy, policyKey, skip, take));
 }
        /// <summary>
        /// 取得名单列表(分页)
        /// </summary>
        /// <param name="rosterType">名单类型</param>
        /// <param name="api">API</param>
        /// <param name="policy">策略</param>
        /// <param name="policyKey">策略Key</param>
        public async Task <(long count, IEnumerable <ListItem> items)> GetRosterListAsync(RosterType rosterType, string api, Policy policy, string policyKey, long skip, long take)
        {
            var key = FromatRosterKey(rosterType, api, policy, policyKey);
            //取得件数
            var count = await _db.SortedSetLengthAsync(key, DateTime.Now.Ticks, double.PositiveInfinity, Exclude.Both);

            if (count == 0)
            {
                return(0, new List <ListItem>());
            }

            //取得数据
            var data = await _db.SortedSetRangeByScoreWithScoresAsync(key, start : DateTime.Now.Ticks, stop : double.PositiveInfinity, exclude : Exclude.Both, order : Order.Ascending, skip : skip, take : take);

            return(count, data.Select(x => new ListItem {
                Value = (string)x.Element, ExpireTicks = x.Score
            }));
        }
예제 #21
0
 /// <summary>
 /// 取得名单列表
 /// </summary>
 /// <param name="rosterType">名单类型</param>
 /// <param name="api">API</param>
 /// <param name="policy">策略</param>
 /// <param name="policyKey">策略Key</param>
 public async Task <IEnumerable <ListItem> > GetRosterListAsync(RosterType rosterType, string api, Policy policy, string policyKey)
 {
     return(await _storage.GetRosterListAsync(rosterType, api, policy, policyKey));
 }
예제 #22
0
 /// <summary>
 /// 取得全局名单列表
 /// </summary>
 /// <param name="rosterType">名单类型</param>
 /// <param name="policy">策略</param>
 /// <param name="policyKey">策略Key</param>
 public async Task <IEnumerable <ListItem> > GetGlobalRosterListAsync(RosterType rosterType, Policy policy, string policyKey)
 {
     return(await GetRosterListAsync(rosterType, CommonHelper.GlobalApiKey, policy, policyKey));
 }
예제 #23
0
 /// <summary>
 /// 取得名单列表(分页)
 /// </summary>
 /// <param name="rosterType">名单类型</param>
 /// <param name="api">API</param>
 /// <param name="policy">策略</param>
 /// <param name="policyKey">策略Key</param>
 public async Task <(long count, IEnumerable <ListItem> items)> GetRosterListAsync(RosterType rosterType, string api, Policy policy, string policyKey, long skip, long take)
 {
     return(await _storage.GetRosterListAsync(rosterType, api, policy, policyKey, skip, take));
 }
예제 #24
0
 /// <summary>
 /// 移除全局名单
 /// </summary>
 /// <param name="policy">策略</param>
 /// <param name="item">项目</param>
 public async Task RemoveGlobalRosterAsync(RosterType rosterType, Policy policy, string policyKey, params string[] item)
 {
     await RemoveRosterAsync(rosterType, CommonHelper.GlobalApiKey, policy, policyKey, item);
 }
예제 #25
0
 /// <summary>
 /// 添加全局名单
 /// </summary>
 /// <param name="rosterType">名单类型</param>
 /// <param name="policy">策略</param>
 /// <param name="policyKey">策略Key</param>
 /// <param name="expiry">过期时间</param>
 /// <param name="item">项目</param>
 public async Task AddGlobalRosterAsync(RosterType rosterType, Policy policy, string policyKey, TimeSpan?expiry, params string[] item)
 {
     await AddRosterAsync(rosterType, CommonHelper.GlobalApiKey, policy, policyKey, expiry, item);
 }