/// <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); } }
/// <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); } }
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(); }
/// <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); }
/// <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 })); }
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); }
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); }
/// <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); }
/// <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); }
/// <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); }
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); }
/// <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 })); }
/// <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(); }
public Task ClearRosterListCacheAsync(RosterType rosterType, string api, Policy policy, string policyKey) { throw new NotImplementedException(); }
/// <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 })); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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); }
/// <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); }