private void btnAdd_Click(object sender, RoutedEventArgs e) { try { if (this._firstAdd) { MemoryCacheEx.Add(1, TimeEx.GetTimestamp()); this._firstAdd = false; } MemoryCacheEx.Add(2, TimeEx.GetTimestamp(), this._expiration, this.CacheItemRemovedCallback); MemoryCacheEx.Add(3, TimeEx.GetTimestamp(), TimeSpan.FromMilliseconds(this._expiration), this.CacheItemRemovedCallback); MemoryCacheEx.Add(4, TimeEx.GetTimestamp(), (t) => { return((DateTimeOffset.Now - t.AddTime).TotalMilliseconds > this._expiration * 2); }, this.CacheItemRemovedCallback); CacheItem cacheItem = new CacheItem(5, TimeEx.GetTimestamp()); cacheItem.AbsoluteExpiration = DateTimeOffset.Now.AddMilliseconds(this._expiration); //cacheItem.SlidingExpiration = slidingExpiration; //cacheItem.CustomerExpiration = customerExpiration; cacheItem.RemovedCallback = this.CacheItemRemovedCallback; //cacheItem.InnerRemovedCallback = true; MemoryCacheEx.Add(cacheItem); } catch (Exception ex) { Loger.Error(ex); } }
public UserInfoService(IMapper mapper, IUserInfoRepository repository, IErrorHandler error, IValidator <UserInfoInputDto> validator, MemoryCacheEx memorycache, IMenuInfoRepository menuInfoRepository, IRoleInfoRepository roleInfoRepository, IPowerInfoRepository powerInfoRepository, IRoleGroupRepository roleGroupRepository, IUserGroupRepository userGroupRepository, IPowerGroupRepository powerGroupRepository, IOperateInfoRepository operateInfoRepository, IUserGroupRelationRepository userGroupRelationRepository ) : base(mapper, repository, error, validator) { _memorycache = memorycache; _userinfoRepository = repository; _menuInfoRepository = menuInfoRepository; _roleInfoRepository = roleInfoRepository; _powerInfoRepository = powerInfoRepository; _userGroupRepository = userGroupRepository; _roleGroupRepository = roleGroupRepository; _powerGroupRepository = powerGroupRepository; _operateInfoRepository = operateInfoRepository; _userGroupRelationRepository = userGroupRelationRepository; }
private Dictionary <string, DisplayNameExAttribute> GetPropertyDisplayOrderInfo(Type type) { var proOrderDic = MemoryCacheEx.Get(type.FullName) as Dictionary <string, DisplayNameExAttribute>; if (proOrderDic == null) { proOrderDic = new Dictionary <string, DisplayNameExAttribute>(); var proInfos = type.GetProperties(); Type displayOrderType = typeof(DisplayNameExAttribute); var noneOrderProInfos = new List <Tuple <DisplayNameExAttribute, PropertyInfo> >(); foreach (var proInfo in proInfos) { object[] objs = proInfo.GetCustomAttributes(displayOrderType, true); if (objs != null && objs.Length > 0) { noneOrderProInfos.Add(new Tuple <DisplayNameExAttribute, PropertyInfo>((DisplayNameExAttribute)objs[0], proInfo)); } else { //noneOrderProInfos.Add(new Tuple<DisplayNameExAttribute, PropertyInfo>(new DisplayOrderAttribute(int.MaxValue), proInfo)); } } var orderProInfos = noneOrderProInfos.OrderBy((p) => { return(p.Item1.OrderIndex); }); foreach (var item in orderProInfos) { proOrderDic.Add(item.Item2.Name, item.Item1); } MemoryCacheEx.Set(type.FullName, proOrderDic, 10 * 60 * 1000);//10分钟过期 } return(proOrderDic); }
private void ProTransferCompleted(TransferCompletedMessage message) { var rid = message.Header.Rid; string transferCompletedMessageCacheKey = CacheKeyGenerator.GenerateTransferCompletedMessageCacheKey(message); const int revTransferCompletedMessageCacheExpire = 60000; TransferSender sender; if (this._senderDic.TryGetValue(rid, out sender)) { sender.ProResourceCompleted(); MemoryCacheEx.Set(transferCompletedMessageCacheKey, rid, revTransferCompletedMessageCacheExpire); this.SendTransferCompletedAckMessage(message); } else { if (MemoryCacheEx.Get(transferCompletedMessageCacheKey) == null) { //Loger.Warn($"未知的资源ID:{rid}"); } else { MemoryCacheEx.Set(transferCompletedMessageCacheKey, rid, revTransferCompletedMessageCacheExpire); this.SendTransferCompletedAckMessage(message); } } }
/// <summary> /// /// </summary> /// <param name="id"></param> /// <param name="expirationMilliseconds">缓存项有效时间,小于等于0永不过期,单位/毫秒</param> private static void AddIdToCache(object id, int expirationMilliseconds) { if (expirationMilliseconds <= 0) { return; } string key = GetCacheKeyFromObjectId(id); MemoryCacheEx.Set(key, id, expirationMilliseconds, new CacheEntryRemovedCallback(ExpirationCacheEntryRemovedCallback)); }
private void btnSet_Click(object sender, RoutedEventArgs e) { MemoryCacheEx.Set(1, TimeEx.GetTimestamp()); MemoryCacheEx.Set(2, TimeEx.GetTimestamp(), this._expiration, this.CacheItemRemovedCallback); MemoryCacheEx.Set(3, TimeEx.GetTimestamp(), TimeSpan.FromMilliseconds(this._expiration), this.CacheItemRemovedCallback); MemoryCacheEx.Set(4, TimeEx.GetTimestamp(), (t) => { return((DateTimeOffset.Now - t.AddTime).TotalMilliseconds > this._expiration * 2); }, this.CacheItemRemovedCallback); CacheItem cacheItem = new CacheItem(5, TimeEx.GetTimestamp()); cacheItem.AbsoluteExpiration = DateTimeOffset.Now.AddMilliseconds(this._expiration); //cacheItem.SlidingExpiration = slidingExpiration; //cacheItem.CustomerExpiration = customerExpiration; cacheItem.RemovedCallback = this.CacheItemRemovedCallback; //cacheItem.InnerRemovedCallback = true; MemoryCacheEx.Set(cacheItem); }
private void btnGet_Click(object sender, RoutedEventArgs e) { Loger.Info($"------------------------------------------------------------"); List <int> keyList = new List <int>() { 1, 2, 3, 4, 5, 6 }; foreach (var key in keyList) { object value = MemoryCacheEx.Get(key); if (value != null) { Loger.Info($"缓存Key:{key}对应值为:{value}"); } else { Loger.Warn($"缓存Key:{key}不存在"); } } Loger.Info($"------------------------------------------------------------"); }
private void RevTimeoutCheckThreadMethod(ThreadExPara para) { const int defaultTimeoutCheckInveral = 100; int timeoutCheckInveral = defaultTimeoutCheckInveral; ReceiveDataItem[] revItems; SendDataNotifyMessage notifyMessage; while (!para.Token.IsCancellationRequested) { try { revItems = this._revItemDic.Values.ToArray(); if (revItems.Length == 0) { try { this._revNewDataNoyifyEventHandle.WaitOne(30000); } catch (ObjectDisposedException) { } continue; } timeoutCheckInveral = defaultTimeoutCheckInveral; foreach (var revItem in revItems) { try { notifyMessage = revItem.NotifyMessage; if (revItem.IsTimeout()) { //Loger.Warn($"RID:[{notifyMessage.Header.Rid}]传输超时,最后一次收到数据时间[{TimeEx.TimestampToDateTime(revItem.LastAccessTimestamp).AddHours(8).ToString("HH:mm:ss.fffffff")}]从传输列表中移除该项"); MemoryCacheEx.Set(CacheKeyGenerator.GenerateRevTimeoutKey(notifyMessage.Header.Rid), notifyMessage.Header.Rid, revItem.MillisecondsTimeout); this.RemoveTransferItem(notifyMessage.Header.Rid, false); } else { if (timeoutCheckInveral > notifyMessage.Timeout) { timeoutCheckInveral = notifyMessage.Timeout; } } } catch (Exception exi) { Loger.Error(exi); } } notifyMessage = null; } catch (ObjectDisposedException) { } catch (Exception ex) { Loger.Error(ex, "RevTimeoutCheckThreadMethod异常"); } try { this._revTimeoutCheckEventHandle.WaitOne(timeoutCheckInveral); } catch (ObjectDisposedException) { } } }
private bool ProResourceMode(SendDataNotifyMessage message) { lock (this._revItemDicLock) { var rid = message.Header.Rid; ReceiveDataItem receiveDataItem; if (this._revItemDic.TryGetValue(rid, out receiveDataItem)) { receiveDataItem.UpdateLastAccessTimestamp(); //Loger.Warn($"[RID:{rid}]的接收项已存在,忽略"); return(false); } if (MemoryCacheEx.Get(CacheKeyGenerator.GenerateRevTimeoutKey(rid)) != null) { //Loger.Warn($"[RID:{rid}]接收已超时,忽略"); return(false); } //Loger.Warn($"处理RID[{rid}]发送通知"); receiveDataItem = new ReceiveDataItem(message, this._config.LocalFileDirectory, this._config.TransferThreadCount); if (!this._revItemDic.TryAdd(rid, receiveDataItem)) { receiveDataItem.Close(true); Loger.Error("this._revItemDic.TryAdd失败,原因未知"); return(false); } int millisecondsTimeout; var revItemCount = this._revItemDic.Count; if (revItemCount < 1) { millisecondsTimeout = message.Timeout / this._config.TimeoutHeartMul; } else { millisecondsTimeout = message.Timeout / (revItemCount * this._config.TimeoutHeartMul); } var reqDataSchduleInfoManager = new TransferReqDataSchduleInfoManager(message, this._reqDataThreads, millisecondsTimeout); TransferResourceManager resourceTransferManager; var priority = message.Priority; if (this._priorityResourceTransferManagerSortedList.ContainsKey(priority)) { resourceTransferManager = this._priorityResourceTransferManagerSortedList[priority]; } else { lock (this._priorityResourceTransferManagerSortedListLock) { if (this._priorityResourceTransferManagerSortedList.ContainsKey(priority)) { resourceTransferManager = this._priorityResourceTransferManagerSortedList[priority]; } else { resourceTransferManager = new TransferResourceManager(); this._priorityResourceTransferManagerSortedList.Add(priority, resourceTransferManager); } } } if (resourceTransferManager.AddReqDataSchduleInfoManager(reqDataSchduleInfoManager)) { this.SendReqDataThreadsEventHandleNotify(); return(true); } else { Loger.Error("AddReqDataSchduleInfoManager失败,原因未知"); if (!this._revItemDic.TryRemove(rid, out receiveDataItem)) { receiveDataItem.Close(true); } return(false); } } }
private void btnClear_Click(object sender, RoutedEventArgs e) { MemoryCacheEx.Clear(); }
private void btnRemove_Click(object sender, RoutedEventArgs e) { MemoryCacheEx.Remove(3); }
private static void FromCacheRemoveId(object id) { string key = GetCacheKeyFromObjectId(id); MemoryCacheEx.Remove(key); }
public LoginMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, MemoryCacheEx memorycache) { _next = next; _memorycache = memorycache; _logger = loggerFactory.CreateLogger <ApiExceptionMiddleware>(); }