public override async Task InsertStageData <T>(int stageMinutes, T doc, string createdDateField) { string cacheKey = typeof(T).Name + "_" + stageMinutes; var list = _cacheManager.GetCachedData(new CacheItemArgs(cacheKey, ProviderLevel.High), (args) => { return(new List <T>()); }); if (stageMinutes > 0 && list.Count > 0) { var now = DateTimeOffset.Now; string cachePugeKey = "StageData_PurgeDate_M" + stageMinutes; var lastPurgeTime = _cacheManager.GetCache <DateTimeOffset?>(cachePugeKey); if (lastPurgeTime == null || now.Subtract(lastPurgeTime.Value).TotalMinutes > stageMinutes) { var olderThanDate = now.AddMinutes(0 - stageMinutes); await Task.Run(() => { var toRemoveList = list.AsQueryable().Where($"{createdDateField}<DateTimeOffset.Parse(\"{olderThanDate}\")").ToList(); toRemoveList.ForEach(t => list.Remove(t)); _logger.LogInformation($"InsertStageData:Purge:Minutes:{stageMinutes}, Count:{toRemoveList.Count}"); }); _cacheManager.SetCache(cachePugeKey, now); } } list.Add(doc); }
private async Task TestStage() { var id = _dataCacheManager.IncrementValue("NewsFeed", () => { return(_dataStoreManager.Queryable <NewsFeed>().OrderByDescending(c => c.Id).FirstOrDefault()?.Id ?? 0); }); var now = DateTime.Now; var newsfeed = new NewsFeed() { Id = id, Message = "New Message", CreatedDate = now }; await _dataStoreManager.InsertStageData <NewsFeed>(newsfeed); _logger.LogInformation($"insert stage one:id: {newsfeed.Id},msg:{newsfeed.Message},create date:{newsfeed.CreatedDate.LocalDateTime}"); var lastFetchDate = _dataCacheManager.GetCache <DateTime?>("News Fetch Date"); int lastOnStageMinutes = lastFetchDate == null ? -1 : (int)now.Subtract(lastFetchDate.Value).TotalMinutes; var lastestQuery = _dataStoreManager.StageQueryable <NewsFeed>(lastOnStageMinutes); if (lastFetchDate != null) { lastestQuery = lastestQuery.Where(c => c.CreatedDate >= lastFetchDate); } var nNewCount = lastestQuery.Count(); _logger.LogInformation($"lasted count :{nNewCount}"); if (nNewCount > 0) { var lastone = lastestQuery.OrderByDescending(c => c.Id).First(); _logger.LogInformation($"last one:id: {lastone.Id},msg:{lastone.Message},create date:{lastone.CreatedDate.LocalDateTime}"); } _dataCacheManager.SetCache("News Fetch Date", now); }
public Task Handle(string eventName, dynamic eventData) { var publishedEvent = _IExternalSubscriptionsManager.GetPublishedEvent(eventName); if (publishedEvent.EventThresholdSeconds > 0) { var cachekey = eventName + "_LastHandleTime"; var lastHandleTime = _IDataCacheManager.GetCache <DateTime?>(cachekey); if (lastHandleTime != null && DateTime.Now.Subtract(lastHandleTime.Value).TotalSeconds < publishedEvent.EventThresholdSeconds) { return(Task.FromResult(0)); } _IDataCacheManager.SetCache(cachekey, DateTime.Now); } var subscribers = _IExternalSubscriptionsManager.GetPublicSubscribersByEventName(eventName); var resilientHttpClient = new ResilientHttpClient((c) => CreatePolicies(), _logger); foreach (var subscriber in subscribers) { string strNewAddress = subscriber.Address + (subscriber.Address.IndexOf("?") == -1 ? "?" : "&"); strNewAddress += "eventname=" + eventName; if (String.IsNullOrEmpty(subscriber.PrivateKey)) { resilientHttpClient.PostAsync(strNewAddress, eventData); } else { var message = JsonConvert.SerializeObject(eventData); var encryptMessage = AESCryptor.EncryptStringAES(message, subscriber.PrivateKey); resilientHttpClient.PostAsync(strNewAddress, encryptMessage); } } return(Task.FromResult(1)); }