public NetMQMessage Map(ClusterMessage envelope) { var message = new NetMQMessage(); byte[] headerFrame = _byteSerializer.Serialize(envelope.Header); message.Append(headerFrame); if (envelope.Header.PayloadType == MessagePayloadType.CommandString) { foreach (var command in envelope.CommandStrings) { var bytes = Encoding.Unicode.GetBytes(command); message.Append(bytes); } } if (envelope.Header.PayloadType == MessagePayloadType.Object || envelope.Header.PayloadType == MessagePayloadType.InternalObject) { var bytes = _byteSerializer.Serialize(envelope.PayloadObject); message.Append(bytes); } else if (envelope.Header.PayloadType == MessagePayloadType.Raw) { foreach (var payload in envelope.RawFrames) { message.Append(payload); } } return(message); }
public async Task <TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate <TResponse> next) { if (request is ICacheableQuery cacheableQuery) { TResponse response; async Task <TResponse> GetResponseAndAddToCache() { response = await next(); var options = cacheableQuery.SlidingExpiration != null ? new DistributedCacheEntryOptions { SlidingExpiration = cacheableQuery.SlidingExpiration } : defaultCacheOptions; await cache.SetAsync(cacheableQuery.CacheKey, byteSerializer.Serialize(response), options, cancellationToken); return(response); } if (cacheableQuery.ReplaceCachedEntry) { logger.LogInformation($"Replacing cache entry for key '{cacheableQuery.CacheKey}'."); response = await GetResponseAndAddToCache(); } else { var cachedResponse = await cache.GetAsync(cacheableQuery.CacheKey, cancellationToken); if (cachedResponse != null) { logger.LogInformation($"Cache hit for key '{cacheableQuery.CacheKey}'."); response = byteSerializer.Deserialize <TResponse>(cachedResponse); } else { logger.LogInformation($"Cache miss for key '{cacheableQuery.CacheKey}'. Adding to cache."); response = await GetResponseAndAddToCache(); } } if (cacheableQuery.RefreshCachedEntry) { logger.LogInformation($"Cache refreshed for key '{cacheableQuery.CacheKey}'."); await cache.RefreshAsync(cacheableQuery.CacheKey, cancellationToken); } return(response); } else { return(await next()); } }
/// <summary> /// Validates if practical assignment is successfully implemented /// </summary> /// <returns>Validation result</returns> public bool Validate() { // Sample data to be serialized var sampleData = new SampleData { Id = 1, Name = "Test", CreatedOn = new DateTime(2020, 08, 18, 13, 50, 0), Features = SampleFlags.A | SampleFlags.C }; // Serializes sample data and compares result return(_byteSerializer?. Serialize(sampleData). SequenceEqual(EXPECTED_RESULT) ?? false); }
public string Encrypt <T>(T obj) where T : new() { var prop = GetPropertiesToEncrypt(typeof(T)); var jObject = JObject.FromObject(obj); foreach (var p in prop) { if (p.CanRead && p.CanWrite) { var value = p.GetValue(obj); var bytes = _byteSerializer.Serialize(value); bytes = _encrypting.Protect(bytes); jObject[p.Name] = bytes; } } return(jObject.ToString(Formatting.Indented)); }
private Task AddToCache(CancellationToken cancellationToken, TResponse response, ICacheableQuery cacheableQuery) { var cacheOptions = new DistributedCacheEntryOptions { SlidingExpiration = cacheableQuery.SlidingExpirationMinutes.HasValue ? TimeSpan.FromMinutes(cacheableQuery.SlidingExpirationMinutes.Value) : _options.Value.SlidingExpirationMilliseconds.HasValue ? (TimeSpan?)TimeSpan.FromMilliseconds(_options.Value.SlidingExpirationMilliseconds.Value) : null, AbsoluteExpiration = cacheableQuery.AbsoluteExpirationMinutes.HasValue ? _dateTimeService.UtcNowOffset.AddMinutes(cacheableQuery.AbsoluteExpirationMinutes .Value) : _options.Value.AbsoluteExpirationMilliseconds.HasValue ? (DateTimeOffset?)_dateTimeService.UtcNowOffset.AddMilliseconds(_options.Value .AbsoluteExpirationMilliseconds .Value) : null }; return(_cache.SetAsync(cacheableQuery.CacheKey, _byteSerializer.Serialize(response), cacheOptions, cancellationToken)); }