public void WhenMaxTilesIsExceededTileCountShouldGoToMinTiles() { // arrange var memoryCache = new MemoryCache<DisposableTile>(2, 3); memoryCache.Add(new TileIndex(1, 0, "0"), new DisposableTile()); memoryCache.Add(new TileIndex(2, 0, "0"), new DisposableTile()); memoryCache.Add(new TileIndex(3, 0, "0"), new DisposableTile()); var tileCountBeforeExceedingMax = memoryCache.TileCount; // act memoryCache.Add(new TileIndex(4, 0, "0"), new DisposableTile()); var tileCountAfterExceedingMax = memoryCache.TileCount; // assert Assert.True(tileCountBeforeExceedingMax == 3); Assert.True(tileCountAfterExceedingMax == 2); }
public void Test002_CannotSetThePolicyOfANonEmptyCache() { var nonEmptyCache = new MemoryCache<int, int>(); nonEmptyCache.SetPolicy(typeof(NoEvictionPolicy<,>)); nonEmptyCache.Add(1, 1); nonEmptyCache.Policy = null; }
public void Adding_an_object_to_the_cache_by_key() { ICache caching = new MemoryCache(mockLogger); var mc = new MyClass(); bool added = caching.Add("firstItem", mc); Assert.IsTrue(added); }
public void AddGenericTest() { var adapter = new MemoryCache(); var value = new { Some = "string" }; adapter.Add("asdf", value); adapter.Get<object>("asdf").Should().Be(value); }
public void Test003_CacheAddIgnoresKeyDuplicatesAndReturnsFalse() { var nonEmptyCache = new MemoryCache<int, int>(); nonEmptyCache.Add(3, 3); nonEmptyCache.Add(1, 1); nonEmptyCache.Add(5, 5); nonEmptyCache.Add(4, 4); Assert.AreEqual(nonEmptyCache.Contains(2), false); Assert.AreEqual(nonEmptyCache.Contains(5), true); Assert.AreEqual(nonEmptyCache.Contains(4), true); Assert.AreEqual(nonEmptyCache.Remove(4), true); Assert.AreEqual(nonEmptyCache.Add(5, 123), false); Assert.AreEqual(nonEmptyCache.Add(4, 6), true); Assert.AreEqual(nonEmptyCache.Get(5), 5); Assert.AreEqual(nonEmptyCache.Get(4), 6); }
public void WhenContentIsAddedItShouldBeRetrieved() { // arrange var memoryCache = new MemoryCache<byte[]>(); var tileIndex = new TileIndex(1, 2, 3); var tileBytes = new byte[] { 7, 7, 7 }; // act memoryCache.Add(tileIndex, tileBytes); // assert Assert.AreEqual(tileBytes, memoryCache.Find(tileIndex)); }
public void WhenKeepInMemoryIsUsedItShouldPreserveTilesThatMeetTheCondition() { // arrange Func<TileIndex, bool> keepTileInMemory = index => index.Row == 2; // keep all where Row = 2 var memoryCache = new MemoryCache<byte[]>(1, 2, keepTileInMemory); var tileBytes = new byte[] { 0, 0, 0, 0 }; var tileOne = new TileIndex(0, 2, 0); var tileTwo = new TileIndex(2, 2, 2); var tileThree = new TileIndex(0, 8, 0); // act memoryCache.Add(tileOne, tileBytes); memoryCache.Add(tileTwo, tileBytes); memoryCache.Add(tileThree, tileBytes); // 3th tile causes CleanUp inside Add because max is 2 // normally only the last one would remain. // With this keepTileMemory method the first two remain. // assert Assert.True(memoryCache.Find(tileOne) != null); Assert.True(memoryCache.Find(tileTwo) != null); }
public void UseCachedAggregateIfAvailable() { var aggregate = new FakeAggregate(); var decoratedAggregateStore = new Mock<IStoreAggregates>(); var memoryCache = new MemoryCache(Guid.NewGuid().ToString()); var cachedAggregateStore = new CachedAggregateStore(decoratedAggregateStore.Object, TimeSpan.FromMinutes(1), memoryCache); memoryCache.Add(aggregate.CacheKey, aggregate, new CacheItemPolicy()); Assert.Same(aggregate, cachedAggregateStore.Get(typeof(FakeAggregate), aggregate.Id)); decoratedAggregateStore.Verify(mock => mock.Get(typeof(FakeAggregate), aggregate.Id), Times.Never()); }
public void WhenContentIsDisposableItShouldBeDisposed() { // arrange var memoryCache = new MemoryCache<DisposableTile>(); var tileIndex = new TileIndex(1, 2, "3"); var disposableTile = new DisposableTile(); memoryCache.Add(tileIndex, disposableTile); // act memoryCache.Remove(tileIndex); // assert Assert.True(disposableTile.Disposed); }
private static ITileCache<Feature> PopulateMemoryCache(GlobalSphericalMercator schema, MemoryCache<Feature> cache, int levelId) { for (var i = levelId; i >= 0; i--) { var tiles = schema.GetTileInfos(schema.Extent, i.ToString(CultureInfo.InvariantCulture)); foreach (var tile in tiles) { if ((tile.Index.Col + tile.Index.Row) % 2 == 0) // Add only 50% of the tiles with the arbitrary rule. { cache.Add(tile.Index, new Feature()); } } } return cache; }
public bool AddMinutes(string key, object value, double absMinuteExpiration) { MemoryCache memoryCache = MemoryCache.Default; return(memoryCache.Add(key, value, this.GetMinutes(absMinuteExpiration))); }
/// <summary> /// Reads the specified <paramref name="source"/> /// </summary> /// <param name="source">The source to be read</param> /// <returns>An <see cref="IEnumerable{BaseData}"/> that contains the data in the source</returns> public IEnumerable <BaseData> Read(SubscriptionDataSource source) { List <BaseData> cache; _shouldCacheDataPoints = _shouldCacheDataPoints && // only cache local files source.TransportMedium == SubscriptionTransportMedium.LocalFile; var cacheItem = _shouldCacheDataPoints ? BaseDataSourceCache.GetCacheItem(source.Source + _config.Type) : null; if (cacheItem == null) { cache = new List <BaseData>(); using (var reader = CreateStreamReader(source)) { // if the reader doesn't have data then we're done with this subscription if (reader == null || reader.EndOfStream) { OnCreateStreamReaderError(_date, source); yield break; } // while the reader has data while (!reader.EndOfStream) { // read a line and pass it to the base data factory var line = reader.ReadLine(); BaseData instance = null; try { instance = _factory.Reader(_config, line, _date, _isLiveMode); } catch (Exception err) { OnReaderError(line, err); } if (instance != null && instance.EndTime != default(DateTime)) { if (_shouldCacheDataPoints) { cache.Add(instance); } else { yield return(instance); } } else if (reader.ShouldBeRateLimited) { yield return(instance); } } } if (!_shouldCacheDataPoints) { yield break; } cacheItem = new CacheItem(source.Source + _config.Type, cache); BaseDataSourceCache.Add(cacheItem, CachePolicy); } cache = cacheItem.Value as List <BaseData>; if (cache == null) { throw new InvalidOperationException("CacheItem can not be cast into expected type. " + $"Type is: {cacheItem.Value.GetType()}"); } // Find the first data point 10 days (just in case) before the desired date // and subtract one item (just in case there was a time gap and data.Time is after _date) var frontier = _date.AddDays(-10); var index = cache.FindIndex(data => data.Time > frontier); index = index > 0 ? (index - 1) : 0; foreach (var data in cache.Skip(index)) { var clone = data.Clone(); clone.Symbol = _config.Symbol; yield return(clone); } }
public void Test002_BasicLFUCacheSemantic() { var lfuCache = new MemoryCache<int, string>(); lfuCache.SetPolicy(typeof(LfuEvictionPolicy<,>)); Assert.AreEqual(lfuCache.Capacity, AbstractCache.DefaultCapacity); var data = Enumerable.Range(0, AbstractCache.DefaultCapacity + 1). Aggregate(new StringBuilder(), (sb, i) => sb.Append(i > 0 ? String.Format(",String {0}", i) : String.Empty)). ToString(). Split(','); // Cache all the (16) non-empty strings for (var i = 1; i < data.Length; i++) { lfuCache.Add(i, data[i]); } // Use all the (16) non-empty strings four times... for (var use = 1; use <= 4; use++) { for (var i = 1; i < data.Length; i++) { // ... except for "String 3", used only twice... if (i == 3) { if (use <= 2) { var s = lfuCache.Get(i); } } // ... and for "String 9", used only once else if (i == 9) { if (use <= 1) { var s = lfuCache.Get(i); } } else { var s = lfuCache.Get(i); } } } lfuCache.Add(17, "String 17"); Assert.AreEqual(lfuCache.Contains(9), false); Assert.AreEqual(lfuCache.Contains(17), true); var used4times = lfuCache.Get(17); used4times = lfuCache.Get(17); used4times = lfuCache.Get(17); used4times = lfuCache.Get(17); lfuCache.Put(18, "String 18"); Assert.AreEqual(lfuCache.Contains(3), false); Assert.AreEqual(lfuCache.Contains(17), true); Assert.AreEqual(lfuCache.Contains(18), true); }
public void Test005_BasicThreeWayLRUCacheSemanticExplicitIndexer() { var lruCache = new MemoryCache<int, int>(15, 3, k => k % 3); lruCache.SetPolicy(typeof(LruEvictionPolicy<,>)); Assert.AreEqual(lruCache.Capacity, 15); lruCache.Add(19, 19); // will go in way 1 lruCache.Add(1, 1); // will go in way 1 lruCache.Add(6, 6); // will go in way 0 lruCache.Add(4, 4); // will go in way 1 lruCache.Add(16, 16); // will go in way 1 lruCache.Add(5, 5); // will go in way 2 (should be first to be evicted in that way) lruCache.Add(18, 18); // will go in way 0 lruCache.Add(7, 7); // will go in way 1 lruCache.Add(15, 15); // will go in way 0 lruCache.Add(2, 2); // will go in way 2 lruCache.Add(14, 14); // will go in way 2 lruCache.Add(9, 9); // will go in way 0 lruCache.Add(20, 20); // will go in way 2 lruCache.Add(17, 17); // will go in way 2 lruCache.Add(12, 12); // will go in way 0 Assert.AreEqual(lruCache.Count, 15); Assert.AreEqual(lruCache.Contains(125), false); lruCache.Put(125, 125); // will go (after causing an evict) in way 2 Assert.AreEqual(lruCache.Contains(125), true); Assert.AreEqual(lruCache.Contains(5), false); Assert.AreEqual(lruCache.Count, 15); }
public bool Add <T>(string key, T value) { return(_cache.Add(key, value, _policy)); }
/// <summary> /// Add item to the cache. /// </summary> /// <param name="item">Configuration item to add.</param> /// <returns>true/false based on success.</returns> public bool Add(IConfigItem item) { return(_memoryCache.Add(item.key, item, _cacheItemPolicy)); }
private void GetTileOnThread(CancellationToken cancelToken, ITileProvider tileProvider, TileInfo tileInfo, MemoryCache <Bitmap> bitmaps, bool retry) { byte[] bytes; try { if (cancelToken.IsCancellationRequested) { cancelToken.ThrowIfCancellationRequested(); } bytes = tileProvider.GetTile(tileInfo); if (cancelToken.IsCancellationRequested) { cancelToken.ThrowIfCancellationRequested(); } Bitmap bitmap = new Bitmap(new MemoryStream(bytes)); bitmaps.Add(tileInfo.Index, bitmap); if (_fileCache != null && !_fileCache.Exists(tileInfo.Index)) { AddImageToFileCache(tileInfo, bitmap); } if (cancelToken.IsCancellationRequested) { cancelToken.ThrowIfCancellationRequested(); } OnMapNewTileAvaliable(tileInfo, bitmap); } catch (WebException ex) { if (retry) { GetTileOnThread(cancelToken, tileProvider, tileInfo, bitmaps, false); } else { if (_showErrorInTile) { //an issue with this method is that one an error tile is in the memory cache it will stay even //if the error is resolved. PDD. var bitmap = new Bitmap(_source.Schema.Width, _source.Schema.Height); using (var graphics = Graphics.FromImage(bitmap)) { graphics.DrawString(ex.Message, new Font(FontFamily.GenericSansSerif, 12), new SolidBrush(Color.Black), new RectangleF(0, 0, _source.Schema.Width, _source.Schema.Height)); } //Draw the Timeout Tile OnMapNewTileAvaliable(tileInfo, bitmap); //With timeout we don't add to the internal cache //bitmaps.Add(tileInfo.Index, bitmap); } } } catch (ThreadAbortException tex) { if (Logger.IsInfoEnabled) { Logger.InfoFormat("TileAsyncLayer - Thread aborting: {0}", Thread.CurrentThread.Name); Logger.InfoFormat(tex.Message); } } catch (Exception ex) { Logger.Warn("TileAsyncLayer - GetTileOnThread Exception", ex); } }
public void AddItem(string itemKey, Response itemToAdd) { _cache.Add(new CacheItem(itemKey, itemToAdd), _standardPolicy); }
public void AjouterObjet(string cle, T obj) { _cache.Add(cle, obj, DateTime.Now.AddMinutes(2)); }
private string Extract(Entity entity, string name) { if (!entity.Attributes.ContainsKey(name)) { return(string.Empty); } var attribute = entity.Attributes[name]; if (attribute is DateTime) { return(((DateTime)attribute).ToString("G")); } if (attribute is EntityReference) { return(((EntityReference)attribute).Name); } else if (attribute is Money) { return(((Money)attribute).Value.ToString("C")); } else if (attribute is OptionSetValue) { if (Parent != null) { EnumAttributeMetadata metadata; // Unique key under which information will be associated in memory cache var key = $"{((PluginControlBase)Parent).ConnectionDetail.ConnectionId}@{entity.LogicalName}@{name}"; if (cache.Contains(key)) { metadata = (EnumAttributeMetadata)cache[key]; } else { var service = ((IXrmToolBoxPluginControl)Parent).Service; var retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(new RetrieveAttributeRequest() { EntityLogicalName = entity.LogicalName, LogicalName = name, RetrieveAsIfPublished = true }); metadata = (EnumAttributeMetadata)retrieveAttributeResponse.AttributeMetadata; // Put metadata value to the cache for 10 minutes var policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(10) }; cache.Add(key, metadata, policy); } var actualOption = (OptionSetValue)attribute; foreach (var declaredOption in metadata.OptionSet.Options) { if (declaredOption.Value == actualOption.Value) { return(declaredOption.Label.UserLocalizedLabel.Label); } } return(string.Empty); } return(((OptionSetValue)attribute).Value.ToString()); } return(attribute as string); }
public async static Task <string> Run([EventHubTrigger("%IotHubEventEndpointName%", Connection = "IotHubEventHubConnection", ConsumerGroup = "router")] EventData myEventHubMessage, TraceWriter log) { //section to build up the metadata section var deviceId = GetDeviceId(myEventHubMessage); dynamic metadataMessageSection; if (localCache.Contains(deviceId)) { metadataMessageSection = localCache[deviceId]; } else { metadataMessageSection = await GetTags(System.Environment.GetEnvironmentVariable("IOT_HUB_OWNER_CONNECTION_STRING"), deviceId); localCache.Add(deviceId, metadataMessageSection, policy); } //section to build up the raw section var rawMessageSection = GetPayload(myEventHubMessage.GetBytes()); //routing //Case 1 route to a global specific function string functionUrl = System.Environment.GetEnvironmentVariable(String.Concat("DECODER_URL_", metadataMessageSection.sensorDecoder)); if (String.IsNullOrEmpty(functionUrl)) { //case 2 route to a global default function functionUrl = System.Environment.GetEnvironmentVariable(String.Concat("DECODER_URL_DEFAULT_", metadataMessageSection.sensorDecoder)); if (String.IsNullOrEmpty(functionUrl)) { //case 3 route to the default function functionUrl = String.Format("https://{0}.azurewebsites.net/api/{1}", System.Environment.GetEnvironmentVariable("WEBSITE_CONTENTSHARE"), System.Environment.GetEnvironmentVariable("SensorDecoder")); } } //Section to build up the decoded section HttpWebRequest req = (HttpWebRequest)WebRequest.Create(functionUrl); req.Method = "POST"; req.ContentType = "application/json"; Stream stream = req.GetRequestStream(); string json = JsonConvert.SerializeObject(rawMessageSection); byte[] buffer = Encoding.UTF8.GetBytes(json); stream.Write(buffer, 0, buffer.Length); string decodedSection = ""; try { HttpWebResponse res = (HttpWebResponse)req.GetResponse(); using (var sr = new StreamReader(res.GetResponseStream())) { decodedSection = sr.ReadToEnd(); } } catch (System.Net.WebException exception) { decodedSection = JsonConvert.SerializeObject(new Dictionary <string, string>() { { "error", "The decoder method was not found" } }); } //build the message outputed to the output eventHub ReturnMessage returnMessage = new ReturnMessage(); returnMessage.decoded = JsonConvert.DeserializeObject(decodedSection); returnMessage.raw = rawMessageSection; returnMessage.metadata = metadataMessageSection; string returnString = JsonConvert.SerializeObject(returnMessage); log.Info(returnString); return(returnString); }
public Boolean Add(String key, Object value, DateTimeOffset absExpiration) { MemoryCache memoryCache = MemoryCache.Default; return(memoryCache.Add(key, value, absExpiration)); }
private void XmppOnOnMessage(object sender, Message msg) { try { switch (msg.Type) { case MessageType.normal: if (msg.Subject == "hostgame") { if (msg.HasChildElements == false) { // F it, someone screwed up this year. return; } if (msg.ChildNodes.OfType <HostGameRequest>().Any() == false) { // Again, what the fuuuuu return; } var req = msg.ChildNodes.OfType <HostGameRequest>().First(); Log.InfoFormat("Host game from {0}", msg.From); while (SasUpdater.Instance.IsUpdating) { Thread.Sleep(100); } var id = GameManager.Instance.HostGame(req, new User(msg.From)); if (id != Guid.Empty) { userRequests.Add("hostrequest_" + id, id, DateTimeOffset.UtcNow.AddSeconds(30)); } } else if (msg.Subject == "gamelist") { // If someone tried to refresh their game list too soon, f them if (userRequests.Contains("refreshrequest_" + msg.From.User.ToLower())) { return; } // Mark the user as already requested a list for the next 15 seconds userRequests.Add("refreshrequest_" + msg.From.User.ToLower(), 1, DateTimeOffset.UtcNow.AddSeconds(15)); var list = GameManager.Instance.Games; var m = new Message(msg.From, MessageType.normal, "", "gamelist"); m.GenerateId(); foreach (var a in list) { m.AddChild(a); } Xmpp.Send(m); } else if (msg.Subject == "killgame") { var items = msg.Body.Split(new[] { "#:999:#" }, StringSplitOptions.RemoveEmptyEntries); if (items.Length != 2) { return; } var client = new ApiClient(); var res = client.Login(msg.From.User, items[1]); if (res == LoginResult.Ok) { var id = Guid.Parse(items[0]); GameManager.Instance.KillGame(id); } throw new Exception("Error verifying user " + res); } break; case MessageType.error: break; case MessageType.chat: if (!msg.From.User.Equals("d0c", StringComparison.InvariantCultureIgnoreCase)) { return; } // Keep this around in case we want to add commands at some point, we'll have an idea on how to write the code //if (msg.Body.Equals("pause")) //{ // _isPaused = true; // Log.Warn(":::::: PAUSED ::::::"); // var m = new Message(msg.From, MessageType.chat, "Paused"); // m.GenerateId(); // Xmpp.Send(m); //} break; } } catch (Exception e) { Log.Error("[Bot]XmppOnOnMessage Error", e); } }
/// <summary> /// /// </summary> /// <param name="cancelToken"></param> /// <param name="tileProvider"></param> /// <param name="tileInfo"></param> /// <param name="bitmaps"></param> /// <param name="retry"></param> /// <returns>true if thread finished without getting cancellation signal, false = cancelled</returns> private bool GetTileOnThread(CancellationToken cancelToken, ITileProvider tileProvider, TileInfo tileInfo, MemoryCache<Bitmap> bitmaps, bool retry) { byte[] bytes; try { if (cancelToken.IsCancellationRequested) cancelToken.ThrowIfCancellationRequested(); //We may have gotten the tile from another thread now.. if (bitmaps.Find(tileInfo.Index) != null) { return true; } if (Logger.IsDebugEnabled) Logger.DebugFormat("Calling gettile on provider for tile {0},{1},{2}", tileInfo.Index.Level, tileInfo.Index.Col, tileInfo.Index.Row); bytes = tileProvider.GetTile(tileInfo); if (cancelToken.IsCancellationRequested) cancelToken.ThrowIfCancellationRequested(); using (var ms = new MemoryStream(bytes)) { Bitmap bitmap = new Bitmap(ms); bitmaps.Add(tileInfo.Index, bitmap); if (_fileCache != null && !_fileCache.Exists(tileInfo.Index)) { AddImageToFileCache(tileInfo, bitmap); } if (cancelToken.IsCancellationRequested) cancelToken.ThrowIfCancellationRequested(); OnMapNewTileAvaliable(tileInfo, bitmap); } return true; } catch (WebException ex) { if (Logger.IsDebugEnabled) Logger.DebugFormat("Exception downloading tile {0},{1},{2} {3}", tileInfo.Index.Level, tileInfo.Index.Col, tileInfo.Index.Row, ex.Message); if (retry) { return GetTileOnThread(cancelToken, tileProvider, tileInfo, bitmaps, false); } else { if (_showErrorInTile) { var tileWidth = _source.Schema.GetTileWidth(tileInfo.Index.Level); var tileHeight = _source.Schema.GetTileHeight(tileInfo.Index.Level); //an issue with this method is that one an error tile is in the memory cache it will stay even //if the error is resolved. PDD. var bitmap = new Bitmap(tileWidth, tileHeight); using (var graphics = Graphics.FromImage(bitmap)) { graphics.DrawString(ex.Message, new Font(FontFamily.GenericSansSerif, 12), new SolidBrush(Color.Black), new RectangleF(0, 0, tileWidth, tileHeight)); } //Draw the Timeout Tile OnMapNewTileAvaliable(tileInfo, bitmap); //With timeout we don't add to the internal cache //bitmaps.Add(tileInfo.Index, bitmap); } return true; } } catch (System.OperationCanceledException tex) { if (Logger.IsInfoEnabled) { Logger.InfoFormat("TileAsyncLayer - Thread aborting: {0}", Thread.CurrentThread.Name); Logger.InfoFormat(tex.Message); } return false; } catch (Exception ex) { Logger.Warn("TileAsyncLayer - GetTileOnThread Exception", ex); //This is not due to cancellation so return true return true; } }
public void Test003_BasicLRUCacheSemantic() { var lruCache = new MemoryCache<int, int>(); lruCache.SetPolicy(typeof(LruEvictionPolicy<,>)); Assert.AreEqual(lruCache.Capacity, AbstractCache.DefaultCapacity); lruCache.Add(19, 19); lruCache.Add(1, 1); lruCache.Add(6, 6); lruCache.Add(2, 2); lruCache.Add(16, 16); lruCache.Add(5, 5); lruCache.Add(18, 18); lruCache.Add(7, 7); lruCache.Add(15, 15); lruCache.Add(4, 4); lruCache.Add(14, 14); lruCache.Add(9, 9); lruCache.Add(13, 13); lruCache.Add(17, 17); lruCache.Add(12, 12); lruCache.Add(3, 3); Assert.AreEqual(lruCache.Count, 16); Assert.AreEqual(lruCache.Contains(123), false); lruCache.Add(123, 123); Assert.AreEqual(lruCache.Contains(123), true); Assert.AreEqual(lruCache.Contains(19), false); Assert.AreEqual(lruCache.Count, 16); }
public async Task RunAsync() { LoadingResult loadingResult = LoadingResult.Failed; bool success = false; try { if (IsCompleted || IsCancelled || ImageService.ExitTasksEarly) { throw new OperationCanceledException(); } ThrowIfCancellationRequested(); // LOAD IMAGE if (!(await TryLoadFromMemoryCacheAsync().ConfigureAwait(false))) { Logger.Debug(string.Format("Generating/retrieving image: {0}", Key)); var resolver = Parameters.CustomDataResolver ?? DataResolverFactory.GetResolver(Parameters.Path, Parameters.Source, Parameters, Configuration); resolver = new WrappedDataResolver(resolver); var imageData = await resolver.Resolve(Parameters.Path, Parameters, CancellationTokenSource.Token).ConfigureAwait(false); loadingResult = imageData.Item2; using (imageData.Item1) { ImageInformation = imageData.Item3; ImageInformation.SetKey(Key, Parameters.CustomCacheKey); ImageInformation.SetPath(Parameters.Path); ThrowIfCancellationRequested(); // Preload if (Parameters.Preload && Parameters.CacheType.HasValue && Parameters.CacheType.Value == CacheType.Disk) { if (loadingResult == LoadingResult.Internet) { Logger?.Debug(string.Format("DownloadOnly success: {0}", Key)); } success = true; return; } ThrowIfCancellationRequested(); var image = await GenerateImageAsync(Parameters.Path, Parameters.Source, imageData.Item1, imageData.Item3, true, false).ConfigureAwait(false); ThrowIfCancellationRequested(); try { BeforeLoading(image, false); if (image != default(TImageContainer) && CanUseMemoryCache) { MemoryCache.Add(Key, imageData.Item3, image); } ThrowIfCancellationRequested(); bool isFadeAnimationEnabled = Parameters.FadeAnimationEnabled ?? Configuration.FadeAnimationEnabled; await SetTargetAsync(image, isFadeAnimationEnabled).ConfigureAwait(false); } finally { AfterLoading(image, false); } } } success = true; } catch (Exception ex) { if (ex is OperationCanceledException || ex is ObjectDisposedException) { if (Configuration.VerboseLoadingCancelledLogging) { Logger.Debug(string.Format("Image loading cancelled: {0}", Key)); } } else { if (_clearCacheOnOutOfMemory && ex is OutOfMemoryException) { MemoryCache.Clear(); } Logger.Error(string.Format("Image loading failed: {0}", Key), ex); if (Configuration.ExecuteCallbacksOnUIThread && Parameters?.OnError != null) { await MainThreadDispatcher.PostAsync(() => { Parameters?.OnError?.Invoke(ex); }).ConfigureAwait(false); } else { Parameters?.OnError?.Invoke(ex); } try { // Error placeholder if enabled if (!Parameters.Preload && !string.IsNullOrWhiteSpace(Parameters.ErrorPlaceholderPath)) { await ShowPlaceholder(Parameters.ErrorPlaceholderPath, KeyForErrorPlaceholder, Parameters.ErrorPlaceholderSource, false).ConfigureAwait(false); } } catch (Exception ex2) { if (!(ex2 is OperationCanceledException)) { Logger.Error(string.Format("Image loading failed: {0}", Key), ex); } } } } finally { try { if (CancellationTokenSource?.IsCancellationRequested == false) { CancellationTokenSource.Cancel(); } } catch (Exception) { } IsCompleted = true; using (Parameters) { if (Configuration.ExecuteCallbacksOnUIThread && Parameters?.OnFinish != null) { await MainThreadDispatcher.PostAsync(() => { if (success) { Parameters?.OnSuccess?.Invoke(ImageInformation, loadingResult); } Parameters?.OnFinish?.Invoke(this); }).ConfigureAwait(false); } else { if (success) { Parameters?.OnSuccess?.Invoke(ImageInformation, loadingResult); } Parameters?.OnFinish?.Invoke(this); } ImageService.RemovePendingTask(this); } } }
protected virtual async Task ShowPlaceholder(string path, string key, ImageSource source, bool isLoadingPlaceholder) { if (Parameters.Preload) { return; } if (!await TryLoadFromMemoryCacheAsync(key, false, false, isLoadingPlaceholder).ConfigureAwait(false)) { try { var customResolver = isLoadingPlaceholder ? Parameters.CustomLoadingPlaceholderDataResolver : Parameters.CustomErrorPlaceholderDataResolver; var loadResolver = customResolver ?? DataResolverFactory.GetResolver(path, source, Parameters, Configuration); loadResolver = new WrappedDataResolver(loadResolver); DataResolverResult loadImageData; TImageContainer loadImage; if (!await _placeholdersResolveLock.WaitAsync(TimeSpan.FromSeconds(10), CancellationTokenSource.Token).ConfigureAwait(false)) { return; } try { ThrowIfCancellationRequested(); if (await TryLoadFromMemoryCacheAsync(key, false, false, isLoadingPlaceholder).ConfigureAwait(false)) { if (isLoadingPlaceholder) { _isLoadingPlaceholderLoaded = true; Parameters.OnLoadingPlaceholderSet?.Invoke(); } return; } ThrowIfCancellationRequested(); loadImageData = await loadResolver.Resolve(path, Parameters, CancellationTokenSource.Token).ConfigureAwait(false); ThrowIfCancellationRequested(); if (loadImageData.Stream != null) { using (loadImageData.Stream) { loadImage = await GenerateImageAsync(path, source, loadImageData.Stream, loadImageData.ImageInformation, TransformPlaceholders, true).ConfigureAwait(false); } } else { loadImage = loadImageData.ImageContainer as TImageContainer; } if (loadImage != default(TImageContainer)) { MemoryCache.Add(key, loadImageData.ImageInformation, loadImage); } } finally { _placeholdersResolveLock.Release(); } ThrowIfCancellationRequested(); if (isLoadingPlaceholder) { PlaceholderWeakReference = new WeakReference <TImageContainer>(loadImage); } if (Target != null) { await SetTargetAsync(loadImage, false).ConfigureAwait(false); } if (isLoadingPlaceholder) { _isLoadingPlaceholderLoaded = true; Parameters.OnLoadingPlaceholderSet?.Invoke(); } } catch (Exception ex) { if (ex is OperationCanceledException) { throw; } Logger.Error("Setting placeholder failed", ex); } } else if (isLoadingPlaceholder) { _isLoadingPlaceholderLoaded = true; Parameters.OnLoadingPlaceholderSet?.Invoke(); } }
/// <summary> /// /// </summary> /// <param name="cancelToken"></param> /// <param name="tileProvider"></param> /// <param name="tileInfo"></param> /// <param name="bitmaps"></param> /// <param name="retry"></param> /// <returns>true if thread finished without getting cancellation signal, false = cancelled</returns> private bool GetTileOnThread(CancellationToken cancelToken, ITileProvider tileProvider, TileInfo tileInfo, MemoryCache <Bitmap> bitmaps, bool retry) { byte[] bytes; try { if (cancelToken.IsCancellationRequested) { cancelToken.ThrowIfCancellationRequested(); } //We may have gotten the tile from another thread now.. if (bitmaps.Find(tileInfo.Index) != null) { return(true); } if (Logger.IsDebugEnabled) { Logger.DebugFormat("Calling gettile on provider for tile {0},{1},{2}", tileInfo.Index.Level, tileInfo.Index.Col, tileInfo.Index.Row); } bytes = tileProvider.GetTile(tileInfo); if (cancelToken.IsCancellationRequested) { cancelToken.ThrowIfCancellationRequested(); } using (var ms = new MemoryStream(bytes)) { Bitmap bitmap = new Bitmap(ms); bitmaps.Add(tileInfo.Index, bitmap); if (_fileCache != null && !_fileCache.Exists(tileInfo.Index)) { AddImageToFileCache(tileInfo, bitmap); } if (cancelToken.IsCancellationRequested) { cancelToken.ThrowIfCancellationRequested(); } OnMapNewTileAvaliable(tileInfo, bitmap); } return(true); } catch (WebException ex) { if (Logger.IsDebugEnabled) { Logger.DebugFormat("Exception downloading tile {0},{1},{2} {3}", tileInfo.Index.Level, tileInfo.Index.Col, tileInfo.Index.Row, ex.Message); } if (retry) { return(GetTileOnThread(cancelToken, tileProvider, tileInfo, bitmaps, false)); } else { if (_showErrorInTile) { var tileWidth = _source.Schema.GetTileWidth(tileInfo.Index.Level); var tileHeight = _source.Schema.GetTileHeight(tileInfo.Index.Level); //an issue with this method is that one an error tile is in the memory cache it will stay even //if the error is resolved. PDD. var bitmap = new Bitmap(tileWidth, tileHeight); using (var graphics = Graphics.FromImage(bitmap)) { graphics.DrawString(ex.Message, new Font(FontFamily.GenericSansSerif, 12), new SolidBrush(Color.Black), new RectangleF(0, 0, tileWidth, tileHeight)); } //Draw the Timeout Tile OnMapNewTileAvaliable(tileInfo, bitmap); //With timeout we don't add to the internal cache //bitmaps.Add(tileInfo.Index, bitmap); } return(true); } } catch (System.OperationCanceledException tex) { if (Logger.IsInfoEnabled) { Logger.InfoFormat("TileAsyncLayer - Thread aborting: {0}", Thread.CurrentThread.Name); Logger.InfoFormat(tex.Message); } return(false); } catch (Exception ex) { Logger.Warn("TileAsyncLayer - GetTileOnThread Exception", ex); //This is not due to cancellation so return true return(true); } }
public bool Add(CacheDTO value) { MemoryCache memoryCache = MemoryCache.Default; return(memoryCache.Add(value.UserId.ToString(), value, DateTime.Now.AddDays(10))); }
/// <summary> /// /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="absExpiration"></param> public static void Add(string key, object value, DateTimeOffset absExpiration) { MemoryCache memoryCache = MemoryCache.Default; memoryCache.Add(key, value, absExpiration); }
private void CheckDnsSettings(ManualResetEvent resetEvent) { var dnsTasks = GetTasks(); foreach (var dnsTask in dnsTasks) { try { bool contains; lock (_locker) { contains = _tenantMemCache.Contains(dnsTask.tenant.ToString(CultureInfo.InvariantCulture)); } if (!contains) { _log.Debug("Tenant {0} isn't in cache", dnsTask.tenant); CoreContext.TenantManager.SetCurrentTenant(dnsTask.tenant); var tenantInfo = CoreContext.TenantManager.GetCurrentTenant(); SecurityContext.AuthenticateMe(tenantInfo.OwnerId); Defines.TariffType type; try { type = _apiHelper.GetTenantTariff(TENANT_OVERDUE_DAYS); } catch (Exception ex) { _log.Error("GetTenantStatus() Exception:\r\n{0}\r\n", ex.ToString()); type = Defines.TariffType.Active; } switch (type) { case Defines.TariffType.LongDead: _log.Info( "Tenant {0} is not paid too long. Removing domain with mailboxes, aliases and groups.", dnsTask.tenant); RemoveDomain(dnsTask.tenant, dnsTask.user, dnsTask.domain_id); continue; case Defines.TariffType.Overdue: _log.Info("Tenant {0} is not paid. Stop processing domain.", dnsTask.tenant); DisableDomain(dnsTask.domain_id); continue; default: _log.Info("Tenant {0} is paid.", dnsTask.tenant); var cacheItem = new CacheItem(dnsTask.tenant.ToString(CultureInfo.InvariantCulture), type); var cacheItemPolicy = new CacheItemPolicy { RemovedCallback = CacheEntryRemove, AbsoluteExpiration = DateTimeOffset.UtcNow.Add(_tenantCachingPeriod) }; lock (_locker) { _tenantMemCache.Add(cacheItem, cacheItemPolicy); } break; } } else { _log.Debug("Tenant {0} is in cache", dnsTask.tenant); } var isVerified = CheckDomainDns(dnsTask); if (isVerified != dnsTask.domain_is_verified) { _log.Info("Domain '{0}' dns-records changed: they are {1} now.", dnsTask.domain_name, isVerified ? "verified" : "unverified"); SetDomainVerifiedAndChecked(dnsTask.domain_id, isVerified); } else { _log.Info("Domain '{0}' dns-records not changed.", dnsTask.domain_name); SetDomainChecked(dnsTask.domain_id); } if (resetEvent.WaitOne(_waitBeforeNextTaskCheckInMilliseconds)) { break; } } catch (Exception ex) { _log.Error("Unable to check tasks. Exception:\r\n{0}", ex.ToString()); } } }
public object Post(EmailMessage message) { try { using (var db = new Email2SmsContext()) { var msgLog = GetMessageLog(message); db.MessageLog.Add(msgLog); db.SaveChanges(); string plainMessage = message.plain; var locationMatch = Regex.Match(plainMessage, "(4\\d\\.\\d+)[N ,]+[W\\- ]?(12\\d+\\.\\d+)", RegexOptions.IgnoreCase); if (locationMatch.Success) { plainMessage += string.Format(" http://maps.google.com/?q={0},-{1}", locationMatch.Groups[1].Value, locationMatch.Groups[2].Value); } // A quick in-memory duplicate check backed up by a database dupe check in case we've been recycled. DateTime duplicateTime = DateTime.UtcNow.AddMinutes(-5); lock (cacheLock) { if (messageCache.Contains(plainMessage)) { return("Duplicate"); } else { messageCache.Add(plainMessage, DateTime.UtcNow, DateTimeOffset.UtcNow.AddMinutes(5)); } } if (db.InvoiceItems.Any(f => f.Message.Text == plainMessage && f.SendTime > duplicateTime)) { return("Duplicate"); } var list = db.Phones.Where(f => f.Active).ToList(); if (TwilioProvider.HasTwilio()) { var twilioClient = TwilioProvider.GetTwilio(); var twilioFroms = TwilioProvider.GetNumbers(); var fromIndex = 0; var twilioCallback = ConfigurationManager.AppSettings["twilio:callback"]; foreach (var item in list) { var twilioMsg = twilioClient.SendMessage(twilioFroms[fromIndex], item.Address, plainMessage, twilioCallback); fromIndex = (fromIndex + 1) % twilioFroms.Length; db.InvoiceItems.Add(new InvoiceLog { SendTo = item, Sid = twilioMsg.Sid, SendTime = DateTime.UtcNow, Message = msgLog }); db.SaveChanges(); } } } } catch (Exception ex) { Metrics.Exception(ex); } return("OK"); }
public void Test002_NoEvictionCacheJustKeepsGrowing() { var noEvictionCache = new MemoryCache<int, int>(); Assert.AreEqual(noEvictionCache.Capacity, AbstractCache.DefaultCapacity); noEvictionCache.Add(19, 19); noEvictionCache.Add(1, 1); noEvictionCache.Add(6, 6); noEvictionCache.Add(2, 2); noEvictionCache.Add(16, 16); noEvictionCache.Add(5, 5); noEvictionCache.Add(18, 18); noEvictionCache.Add(7, 7); noEvictionCache.Add(15, 15); noEvictionCache.Add(4, 4); noEvictionCache.Add(14, 14); noEvictionCache.Add(9, 9); noEvictionCache.Add(13, 13); noEvictionCache.Add(17, 17); noEvictionCache.Add(12, 12); noEvictionCache.Add(3, 3); Assert.AreEqual(noEvictionCache.Count, 16); noEvictionCache.Add(10, 10); noEvictionCache.Add(11, 11); noEvictionCache.Add(8, 8); Assert.AreEqual(noEvictionCache.Count, 19); var expectedArray = Enumerable.Range(1, 19).ToArray(); var cacheArray = noEvictionCache.OrderBy(i => i).ToArray(); Assert.AreEqual(expectedArray, cacheArray); }
public void Test001_AdhocHitFrequencyRecordingPolicy() { var data = Enumerable.Range(0, AbstractCache.DefaultCapacity + 1). Aggregate(new StringBuilder(), (sb, i) => sb.Append(i > 0 ? String.Format(",String {0}", i) : String.Empty)). ToString(). Split(','); var hits = new int[data.Length]; var expected = new int[data.Length]; for (var i = 1; i < data.Length; i++) { expected[i] = 4; } expected[3] = 3; expected[9] = 2; var hfrCache = new MemoryCache<int, string>(); hfrCache.SetPolicy(typeof(NoEvictionPolicy<,>)); hfrCache.Policy.OnGet = delegate(IManagedCache<int, string> source, int key, string value) { hits[key]++; }; Assert.AreEqual(hfrCache.Capacity, AbstractCache.DefaultCapacity); // Cache all the (16) non-empty strings for (var i = 1; i < data.Length; i++) { Assert.AreEqual(hfrCache.Add(i, data[i]), true); } // Use all the (16) non-empty strings thrice... for (var use = 1; use <= 3; use++) { for (var i = 1; i < data.Length; i++) { // ... except for "String 3", used only twice... if (i == 3) { if (use <= 2) { var s = hfrCache.Get(i); } } // ... and for "String 9", used only once else if (i == 9) { if (use <= 1) { var s = hfrCache.Get(i); } } else { var s = hfrCache.Get(i); } } } foreach (var key in hfrCache) { Console.WriteLine("{0}'s # hits so far: {1}...", key, hits[key]); } Assert.AreEqual(hits, expected); }
protected virtual async Task ShowPlaceholder(string path, string key, ImageSource source, bool isLoadingPlaceholder) { if (Parameters.Preload) { return; } if (!await TryLoadFromMemoryCacheAsync(key, false, false, isLoadingPlaceholder).ConfigureAwait(false)) { try { var customResolver = isLoadingPlaceholder ? Parameters.CustomLoadingPlaceholderDataResolver : Parameters.CustomErrorPlaceholderDataResolver; var loadResolver = customResolver ?? DataResolverFactory.GetResolver(path, source, Parameters, Configuration); loadResolver = new WrappedDataResolver(loadResolver); Tuple <Stream, LoadingResult, ImageInformation> loadImageData; bool hasMutex = false; TImageContainer loadImage; try { hasMutex = await _placeholdersResolveLock.WaitAsync(TimeSpan.FromSeconds(3), CancellationTokenSource.Token).ConfigureAwait(false); ThrowIfCancellationRequested(); if (await TryLoadFromMemoryCacheAsync(key, false, false, isLoadingPlaceholder).ConfigureAwait(false)) { if (isLoadingPlaceholder) { _isLoadingPlaceholderLoaded = true; } return; } ThrowIfCancellationRequested(); loadImageData = await loadResolver.Resolve(path, Parameters, CancellationTokenSource.Token).ConfigureAwait(false); ThrowIfCancellationRequested(); using (loadImageData.Item1) { loadImage = await GenerateImageAsync(path, source, loadImageData.Item1, loadImageData.Item3, TransformPlaceholders, true).ConfigureAwait(false); if (loadImage != default(TImageContainer)) { MemoryCache.Add(key, loadImageData.Item3, loadImage); } } } finally { if (hasMutex) { _placeholdersResolveLock.Release(); } } ThrowIfCancellationRequested(); if (isLoadingPlaceholder) { PlaceholderWeakReference = new WeakReference <TImageContainer>(loadImage); } await SetTargetAsync(loadImage, false).ConfigureAwait(false); if (isLoadingPlaceholder) { _isLoadingPlaceholderLoaded = true; } } catch (Exception ex) { Logger.Error("Setting placeholder failed", ex); } } else if (isLoadingPlaceholder) { _isLoadingPlaceholderLoaded = true; } }
public void UpdateCacheOnSuccessfulSave() { var aggregate = new FakeAggregate(); var decoratedAggregateStore = new Mock<IStoreAggregates>(); var memoryCache = new MemoryCache(Guid.NewGuid().ToString()); var cachedAggregateStore = new CachedAggregateStore(decoratedAggregateStore.Object, TimeSpan.FromMinutes(1), memoryCache); memoryCache.Add(aggregate.CacheKey, aggregate, new CacheItemPolicy()); using (var context = new CommandContext(GuidStrategy.NewGuid(), HeaderCollection.Empty, CommandEnvelope.Empty)) cachedAggregateStore.Save(aggregate, context); Assert.NotSame(aggregate, memoryCache.Get(aggregate.CacheKey)); }
public override void Render(Graphics graphics, Map map) { if (!map.Size.IsEmpty && map.Size.Width > 0 && map.Size.Height > 0) { Bitmap bmp = new Bitmap(map.Size.Width, map.Size.Height, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bmp); g.InterpolationMode = InterpolationMode; g.Transform = graphics.Transform.Clone(); Extent extent = new Extent(map.Envelope.Min.X, map.Envelope.Min.Y, map.Envelope.Max.X, map.Envelope.Max.Y); int level = BruTile.Utilities.GetNearestLevel(_source.Schema.Resolutions, map.PixelSize); IList <TileInfo> tiles = _source.Schema.GetTilesInView(extent, level); IList <WaitHandle> waitHandles = new List <WaitHandle>(); foreach (TileInfo info in tiles) { if (_bitmaps.Find(info.Index) != null) { continue; } if (_fileCache != null && _fileCache.Exists(info.Index)) { _bitmaps.Add(info.Index, GetImageFromFileCache(info) as Bitmap); continue; } AutoResetEvent waitHandle = new AutoResetEvent(false); waitHandles.Add(waitHandle); ThreadPool.QueueUserWorkItem(GetTileOnThread, new object[] { _source.Provider, info, _bitmaps, waitHandle }); } foreach (WaitHandle handle in waitHandles) { handle.WaitOne(); } foreach (TileInfo info in tiles) { Bitmap bitmap = _bitmaps.Find(info.Index); if (bitmap == null) { continue; } PointF min = map.WorldToImage(new Geometries.Point(info.Extent.MinX, info.Extent.MinY)); PointF max = map.WorldToImage(new Geometries.Point(info.Extent.MaxX, info.Extent.MaxY)); min = new PointF((float)Math.Round(min.X), (float)Math.Round(min.Y)); max = new PointF((float)Math.Round(max.X), (float)Math.Round(max.Y)); try { g.DrawImage(bitmap, new Rectangle((int)min.X, (int)max.Y, (int)(max.X - min.X), (int)(min.Y - max.Y)), 0, 0, _source.Schema.Width, _source.Schema.Height, GraphicsUnit.Pixel, _imageAttributes); } catch (Exception ee) { /*GDI+ Hell*/ } } graphics.Transform = new Matrix(); graphics.DrawImageUnscaled(bmp, 0, 0); graphics.Transform = g.Transform; g.Dispose(); } }
public void RemoveAggregateFromCacheOnConcurrencyException() { var aggregate = new FakeAggregate(); var decoratedAggregateStore = new Mock<IStoreAggregates>(); var memoryCache = new MemoryCache(Guid.NewGuid().ToString()); var cachedAggregateStore = new CachedAggregateStore(decoratedAggregateStore.Object, TimeSpan.FromMinutes(1), memoryCache); // ReSharper disable AccessToDisposedClosure using (var context = new CommandContext(GuidStrategy.NewGuid(), HeaderCollection.Empty, CommandEnvelope.Empty)) { memoryCache.Add(aggregate.CacheKey, aggregate, new CacheItemPolicy()); decoratedAggregateStore.Setup(mock => mock.Save(It.Is<Aggregate>(copy => !ReferenceEquals(aggregate, copy)), context)).Throws<ConcurrencyException>(); Assert.Throws<ConcurrencyException>(() => cachedAggregateStore.Save(aggregate, context)); Assert.False(memoryCache.Contains(aggregate.CacheKey)); } // ReSharper restore AccessToDisposedClosure }
public async static Task Run([EventHubTrigger("%IOT_HUB_NAME%", Connection = "EVENT_HUB_ROUTER_INPUT", ConsumerGroup = "router")] EventData[] myEventHubMessageInput, [EventHub("outputEventHubMessage", Connection = "EVENT_HUB_ROUTER_OUTPUT")] IAsyncCollector <String> output, TraceWriter log) { foreach (var myEventHubMessage in myEventHubMessageInput) { //section to build up the metadata section var deviceId = GetDeviceId(myEventHubMessage); dynamic metadataMessageSection; //retry logic to avoid the initial message rush to be declined by the IoT hub. int retryCount = 0; string sensorDecoder = null; while (true) { try { if (localCache.Contains(deviceId)) { metadataMessageSection = localCache[deviceId]; sensorDecoder = ((Newtonsoft.Json.Linq.JObject)metadataMessageSection)["sensorDecoder"]?.ToString() ?? string.Empty; } else { metadataMessageSection = await GetTags(System.Environment.GetEnvironmentVariable("IOT_HUB_OWNER_CONNECTION_STRING"), deviceId); sensorDecoder = ((Newtonsoft.Json.Linq.JObject)metadataMessageSection)["sensorDecoder"]?.ToString() ?? string.Empty; if (!string.IsNullOrEmpty(sensorDecoder)) { localCache.Add(deviceId, metadataMessageSection, policy); } } break; } catch (Exception ex) { retryCount++; if (retryCount > 5) { throw new Exception("Could not connect with the IoT Hub device manager", ex); } await Task.Delay(random.Next(1000, 2000)); } } //section to build up the raw section var rawMessageSection = GetPayload(myEventHubMessage.GetBytes()); //routing //Case 1 route to a global specific function var decodedMessageContents = new Dictionary <string, string>(); string decodedSection = null; if (string.IsNullOrEmpty(sensorDecoder)) { decodedMessageContents.Add("error", "Could not resolve decoder"); decodedMessageContents.Add("details", $"Verify that the device twin has been properly configured for deviceId {deviceId} "); } else { string functionUrl = System.Environment.GetEnvironmentVariable(String.Concat("DECODER_URL_", sensorDecoder)); if (String.IsNullOrEmpty(functionUrl)) { //case 2 route to a global default function functionUrl = System.Environment.GetEnvironmentVariable(String.Concat("DECODER_URL_DEFAULT_", sensorDecoder)); if (String.IsNullOrEmpty(functionUrl)) { //case 3 route to the default function functionUrl = String.Format("https://{0}.azurewebsites.net/api/{1}", System.Environment.GetEnvironmentVariable("WEBSITE_CONTENTSHARE"), metadataMessageSection.sensorDecoder); } } //Section to build up the decoded section HttpWebRequest req = (HttpWebRequest)WebRequest.Create(functionUrl); req.Method = "POST"; req.ContentType = "application/json"; Stream stream = await req.GetRequestStreamAsync(); string json = JsonConvert.SerializeObject(rawMessageSection); byte[] buffer = Encoding.UTF8.GetBytes(json); await stream.WriteAsync(buffer, 0, buffer.Length); try { var res = await req.GetResponseAsync(); using (var sr = new StreamReader(res.GetResponseStream())) { decodedSection = await sr.ReadToEndAsync(); } } catch (System.Net.WebException exception) { decodedMessageContents.Add("error", "The decoder method was not found"); decodedMessageContents.Add("details", exception.Message); decodedMessageContents.Add(nameof(functionUrl), functionUrl); decodedMessageContents.Add(nameof(sensorDecoder), sensorDecoder); } } //build the message outputed to the output eventHub ReturnMessage returnMessage = new ReturnMessage(); if (decodedMessageContents.Count > 0) { returnMessage.decoded = decodedMessageContents; } else if (!string.IsNullOrEmpty(decodedSection)) { returnMessage.decoded = JsonConvert.DeserializeObject(decodedSection); } returnMessage.raw = rawMessageSection; returnMessage.metadata = metadataMessageSection; string returnString = JsonConvert.SerializeObject(returnMessage); log.Info(returnString); await output.AddAsync(returnString); } await output.FlushAsync(); }
public void Put(object key, object value) { store.Add(key.ToString(), value, DateTimeOffset.Now.AddMilliseconds(Timeout)); }
private bool TryLockMailbox(MailBox mailbox) { _log.Debug("TryLockMailbox(MailboxId={0} is {1})", mailbox.MailBoxId, mailbox.Active ? "active" : "inactive"); try { bool contains; lock (_locker) { contains = _tenantMemCache.Contains(mailbox.TenantId.ToString(CultureInfo.InvariantCulture)); } if (!contains) { _log.Debug("Tenant {0} isn't in cache", mailbox.TenantId); try { var type = mailbox.GetTenantStatus(_tasksConfig.TenantOverdueDays, _log); switch (type) { case Defines.TariffType.LongDead: _log.Info("Tenant {0} is not paid. Disable mailboxes.", mailbox.TenantId); _manager.DisableMailboxesForTenant(mailbox.TenantId); RemoveFromQueue(mailbox.TenantId); return(false); case Defines.TariffType.Overdue: _log.Info("Tenant {0} is not paid. Stop processing mailboxes.", mailbox.TenantId); _manager.SetNextLoginDelayedForTenant(mailbox.TenantId, _tasksConfig.OverdueAccountDelay); RemoveFromQueue(mailbox.TenantId); return(false); default: _log.Info("Tenant {0} is paid.", mailbox.TenantId); if (mailbox.IsUserTerminated() || mailbox.IsUserRemoved()) { _log.Info("User '{0}' was terminated. Tenant = {1}. Disable mailboxes for user.", mailbox.UserId, mailbox.TenantId); _manager.DisableMailboxesForUser(mailbox.TenantId, mailbox.UserId); RemoveFromQueue(mailbox.TenantId, mailbox.UserId); return(false); } var cacheItem = new CacheItem(mailbox.TenantId.ToString(CultureInfo.InvariantCulture), type); var cacheItemPolicy = new CacheItemPolicy { RemovedCallback = CacheEntryRemove, AbsoluteExpiration = DateTimeOffset.UtcNow.Add(_tasksConfig.TenantCachingPeriod) }; lock (_locker) { _tenantMemCache.Add(cacheItem, cacheItemPolicy); } break; } } catch (Exception e) { _log.Error("TryLockMailbox() -> GetTariffType Exception:\r\n{0}\r\n", e.ToString()); } } else { _log.Debug("Tenant {0} is in cache", mailbox.TenantId); } if (mailbox.IsTenantQuotaEnded(_tasksConfig.MinQuotaBalance, _log)) { _log.Info("Tenant = {0} User = {1}. Quota is ended.", mailbox.TenantId, mailbox.UserId); if (!mailbox.QuotaError) { _manager.CreateQuotaErrorWarningAlert(mailbox.TenantId, mailbox.UserId); } _manager.SetNextLoginDelayedForUser(mailbox.TenantId, mailbox.UserId, _tasksConfig.QuotaEndedDelay); RemoveFromQueue(mailbox.TenantId, mailbox.UserId); return(false); } return(_manager.LockMailbox(mailbox.MailBoxId, true)); } catch (Exception ex) { _log.Error("TryLockMailbox(MailboxId={0} is {1}) Exception:\r\n{2}\r\n", mailbox.MailBoxId, mailbox.Active ? "active" : "inactive", ex.ToString()); return(false); } }
/// <summary> /// Create a new instance /// </summary> public BaseInProcessAlgorithm(IEnumerable <RateLimitRule> rules, ITimeProvider timeProvider = null, bool updatable = false) : base(rules, timeProvider, updatable) { _cache = new MemoryCache("IPMS-" + Guid.NewGuid().ToString()); _cache.Add("IPMS", 1, DateTimeOffset.MaxValue); }
/// <summary> /// Renders the layer /// </summary> /// <param name="graphics">Graphics object reference</param> /// <param name="map">Map which is rendered</param> public override void Render(Graphics graphics, Map map) { if (!map.Size.IsEmpty && map.Size.Width > 0 && map.Size.Height > 0) { var bmp = new Bitmap(map.Size.Width, map.Size.Height, PixelFormat.Format32bppArgb); using (var g = Graphics.FromImage(bmp)) { g.InterpolationMode = InterpolationMode; g.Transform = graphics.Transform.Clone(); var extent = new Extent(map.Envelope.MinX, map.Envelope.MinY, map.Envelope.MaxX, map.Envelope.MaxY); var level = BruTile.Utilities.GetNearestLevel(_source.Schema.Resolutions, map.PixelSize); var tiles = new List<TileInfo>(_source.Schema.GetTilesInView(extent, level)); var tileWidth = _source.Schema.GetTileWidth(level); var tileHeight = _source.Schema.GetTileWidth(level); IList<WaitHandle> waitHandles = new List<WaitHandle>(); var toRender = new Dictionary<TileIndex, Bitmap>(); var takenFromCache = new Dictionary<TileIndex,bool>(); foreach (TileInfo info in tiles) { var image = _bitmaps.Find(info.Index); if (image != null) { toRender.Add(info.Index, image); takenFromCache.Add(info.Index,true); continue; } if (_fileCache != null && _fileCache.Exists(info.Index)) { _bitmaps.Add(info.Index, GetImageFromFileCache(info) as Bitmap); toRender.Add(info.Index, _bitmaps.Find(info.Index)); takenFromCache.Add(info.Index,true); continue; } var waitHandle = new AutoResetEvent(false); waitHandles.Add(waitHandle); ThreadPool.QueueUserWorkItem(GetTileOnThread, new object[] { _source.Provider, info, toRender, waitHandle, true }); } foreach (var handle in waitHandles) handle.WaitOne(); using (var ia = new ImageAttributes()) { if (!_transparentColor.IsEmpty) ia.SetColorKey(_transparentColor, _transparentColor); #if !PocketPC ia.SetWrapMode(WrapMode.TileFlipXY); #endif foreach (var info in tiles) { if (!toRender.ContainsKey(info.Index)) continue; var bitmap = toRender[info.Index];//_bitmaps.Find(info.Index); if (bitmap == null) continue; var min = map.WorldToImage(new Coordinate(info.Extent.MinX, info.Extent.MinY)); var max = map.WorldToImage(new Coordinate(info.Extent.MaxX, info.Extent.MaxY)); min = new PointF((float) Math.Round(min.X), (float) Math.Round(min.Y)); max = new PointF((float) Math.Round(max.X), (float) Math.Round(max.Y)); try { g.DrawImage(bitmap, new Rectangle((int) min.X, (int) max.Y, (int) (max.X - min.X), (int) (min.Y - max.Y)), 0, 0, tileWidth, tileHeight, GraphicsUnit.Pixel, ia); } catch (Exception ee) { Logger.Error(ee.Message); } } } //Add rendered tiles to cache foreach (var kvp in toRender) { if (takenFromCache.ContainsKey(kvp.Key) && !takenFromCache[kvp.Key]) { _bitmaps.Add(kvp.Key, kvp.Value); } } graphics.Transform = new Matrix(); graphics.DrawImageUnscaled(bmp, 0, 0); graphics.Transform = g.Transform; } } }
public void Set(string key, object obj, DateTime expireDate) { cache.Add(key, obj, expireDate); }
public bool Add(string key, object value, DateTimeOffset absExpiration) { MemoryCache memoryCache = MemoryCache.Default; return(memoryCache.Add(key, value, absExpiration)); }
public void SetCache(string key, object value) { _memoryCache.Add(key, value, DateTimeOffset.Now.AddHours(1)); }
public bool Add <T>(string key, T value, int seconds) { return(_cache.Add(key, value, DateTime.Now.AddSeconds(seconds))); }
private DataTable GetData(AdoDataConnection connection, DateTime startDate, DateTime endDate, string sortField, bool ascending) { DataTable table; string target = "Data" + startDate.ToString() + endDate.ToString(); if (s_memoryCache.Contains(target)) { table = (DataTable)s_memoryCache.Get(target); } else { table = connection.RetrieveData(@" DECLARE @PivotColumns NVARCHAR(MAX) = N'' DECLARE @ReturnColumns NVARCHAR(MAX) = N'' DECLARE @SQLStatement NVARCHAR(MAX) = N'' DECLARE @StartDateParam Date = {0} DECLARE @EndDateParam Date = {1} SELECT @PivotColumns = @PivotColumns + '[' + t.Name + '],' FROM (SELECT Name FROM StepChangeMeasurement JOIN PQMeasurement ON StepChangeMeasurement.PQMeasurementID = PQMeasurement.ID) AS t SELECT @ReturnColumns = @ReturnColumns + ' COALESCE([' + t.Name + '], 0) AS [' + t.Name + '],' FROM (SELECT Name FROM StepChangeMeasurement JOIN PQMeasurement ON StepChangeMeasurement.PQMeasurementID = PQMeasurement.ID) AS t SET @SQLStatement = ' DECLARE @startDate Date = @StartDateParam DECLARE @endDate Date = @EndDateParam SELECT MeterID, Name, ' + SUBSTRING(@ReturnColumns,0, LEN(@ReturnColumns)) + ' FROM ( SELECT Meter.ID as MeterID, Meter.Name, PQMeasurement.Name as Measurement, StepChangeStat.Value FROM Meter JOIN StepChangeStat ON Meter.ID = StepChangeStat.MeterID JOIN StepChangeMeasurement ON StepChangeStat.StepChangeMeasurementID = StepChangeMeasurement.ID JOIN PQMeasurement ON StepChangeMeasurement.PQMeasurementID = PQMeasurement.ID WHERE Date BETWEEN @startDate AND @endDate ) as ed PIVOT( Avg(ed.Value) FOR ed.Measurement IN(' + SUBSTRING(@PivotColumns,0, LEN(@PivotColumns)) + ') ) as pvt ORDER BY Name ' print @sqlstatement exec sp_executesql @SQLStatement, N'@StartDateParam Date, @EndDateParam Date', @StartDateParam = @StartDateParam, @EndDateParam = @EndDateParam ", startDate, endDate); s_memoryCache.Add(target, table, new CacheItemPolicy { SlidingExpiration = TimeSpan.FromMinutes(10.0D) }); } if (!table.Select().Any()) { return(table); } else if (ascending) { return(table.Select().OrderBy(row => row[sortField]).CopyToDataTable()); } else { return(table.Select().OrderByDescending(row => row[sortField]).CopyToDataTable()); } }
public void Test004_CachePutIgnoresKeyDuplicatesAndAllowsOverwrite() { var nonEmptyCache = new MemoryCache<int, int>(); nonEmptyCache.Add(3, 3); nonEmptyCache.Add(1, 1); nonEmptyCache.Add(5, 5); nonEmptyCache.Add(4, 4); Assert.AreEqual(nonEmptyCache.Contains(2), false); Assert.AreEqual(nonEmptyCache.Contains(5), true); Assert.AreEqual(nonEmptyCache.Contains(4), true); Assert.AreEqual(nonEmptyCache.Remove(4), true); nonEmptyCache.Put(5, 123); Assert.AreEqual(nonEmptyCache[5], 123); Assert.AreEqual(nonEmptyCache.Add(4, 6), true); Assert.AreEqual(nonEmptyCache[4], 6); nonEmptyCache.Put(4, 7); Assert.AreEqual(nonEmptyCache[4], 7); }
private void GetTileOnThread(CancellationToken cancelToken, ITileProvider tileProvider, TileInfo tileInfo, MemoryCache<Bitmap> bitmaps, bool retry) { byte[] bytes; try { if (cancelToken.IsCancellationRequested) cancelToken.ThrowIfCancellationRequested(); bytes = tileProvider.GetTile(tileInfo); if (cancelToken.IsCancellationRequested) cancelToken.ThrowIfCancellationRequested(); Bitmap bitmap = new Bitmap(new MemoryStream(bytes)); bitmaps.Add(tileInfo.Index, bitmap); if (_fileCache != null && !_fileCache.Exists(tileInfo.Index)) { AddImageToFileCache(tileInfo, bitmap); } if (cancelToken.IsCancellationRequested) cancelToken.ThrowIfCancellationRequested(); OnMapNewTileAvaliable(tileInfo, bitmap); } catch (WebException ex) { if (retry) { GetTileOnThread(cancelToken, tileProvider, tileInfo, bitmaps, false); } else { if (_showErrorInTile) { //an issue with this method is that one an error tile is in the memory cache it will stay even //if the error is resolved. PDD. var bitmap = new Bitmap(_source.Schema.Width, _source.Schema.Height); using (var graphics = Graphics.FromImage(bitmap)) { graphics.DrawString(ex.Message, new Font(FontFamily.GenericSansSerif, 12), new SolidBrush(Color.Black), new RectangleF(0, 0, _source.Schema.Width, _source.Schema.Height)); } //Draw the Timeout Tile OnMapNewTileAvaliable(tileInfo, bitmap); //With timeout we don't add to the internal cache //bitmaps.Add(tileInfo.Index, bitmap); } } } catch (ThreadAbortException tex) { if (Logger.IsInfoEnabled) { Logger.InfoFormat("TileAsyncLayer - Thread aborting: {0}", Thread.CurrentThread.Name); Logger.InfoFormat(tex.Message); } } catch (Exception ex) { Logger.Warn("TileAsyncLayer - GetTileOnThread Exception", ex); } }