Example #1
0
        public ClusterMessage Map(NetMQMessage source)
        {
            var envelope = new ClusterMessageBuilder().Build();

            envelope.Header = _byteSerializer.Deserialize <MessageHeader>(source.Pop().Buffer);
            if (envelope.Header.PayloadType == MessagePayloadType.Raw)
            {
                envelope.RawFrames = source.Select(f => f.ToByteArray()).ToList();
            }
            else if (envelope.Header.PayloadType == MessagePayloadType.Object || envelope.Header.PayloadType == MessagePayloadType.InternalObject)
            {
                var bytes = source.FirstOrDefault()?.ToByteArray();
                if (bytes != null)
                {
                    envelope.PayloadObject = _byteSerializer.DeserializeObject(bytes);
                }
            }
            else if (envelope.Header.PayloadType == MessagePayloadType.CommandString)
            {
                envelope.CommandStrings = source
                                          .Select(f => f.ToByteArray())
                                          .Select(b => Encoding.Unicode.GetString(b))
                                          .ToList();
            }
            return(envelope);
        }
 public static IBlobSerializer AsBlobSerializer(this IByteSerializer serializer)
 {
     return(new BlobSerializerDelegate(
                serializer.SerializeToBytes,
                (data, type) => serializer.Deserialize(data, type),
                serializer.CanDeserialize));
 }
        public async Task <TResponse> Handle(TRequest request, CancellationToken cancellationToken,
                                             RequestHandlerDelegate <TResponse> next)
        {
            if (!_options.Value.Enabled ||
                !(request is ICacheableQuery cacheableQuery) ||
                cacheableQuery.BypassCache)
            {
                return(await next());
            }

            if (cacheableQuery.ReplaceCachedEntry)
            {
                _logger.LogInformation($"Replacing cache entry for key '{cacheableQuery.CacheKey}'.");
                var response = await next();
                await AddToCache(cancellationToken, response, cacheableQuery);

                return(response);
            }

            byte[]? cachedResponse = await _cache.GetAsync(cacheableQuery.CacheKey, cancellationToken);

            if (cachedResponse == null)
            {
                _logger.LogInformation($"Cache miss for key '{cacheableQuery.CacheKey}'. Adding to cache.");
                var response = await next();
                await AddToCache(cancellationToken, response, cacheableQuery);

                return(response);
            }

            _logger.LogDebug($"Cache hit for key '{cacheableQuery.CacheKey}'.");
            return(_byteSerializer.Deserialize <TResponse>(cachedResponse));
        }
Example #4
0
        public static bool CanSerializeBytes <T>(this IByteSerializer serializer, T item)
        {
            var text = serializer.SerializeToBytes(item);

            var obj = serializer.Deserialize <T>(text);

            return(obj.Equals(item));
        }
        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());
            }
        }
Example #6
0
        public string Decrypt <T>(string json) where T : new()
        {
            var jObject = JObject.Parse(json);
            var prop    = GetPropertiesToEncrypt(typeof(T));

            foreach (var p in prop)
            {
                if (p.CanRead && p.CanWrite)
                {
                    var bytes = Convert.FromBase64String((string)jObject[p.Name]);
                    bytes = _encrypting.Unprotect(bytes);

                    if (bytes == null)
                    {
                        return(null);
                    }

                    dynamic variable = _byteSerializer.Deserialize(bytes, p.PropertyType);
                    jObject[p.Name] = variable;
                }
            }

            return(jObject.ToString());
        }
 /// <summary>
 /// Gets the object from the table.
 /// </summary>
 /// <typeparam name="T">Type of object.</typeparam>
 /// <param name="item">Table item.</param>
 /// <returns>Object T if item found, otherwise default{T}.</returns>
 protected virtual T GetObject <T>(SQliteCacheTable item)
 {
     return((item != null) ? _serializer.Deserialize <T>(item.Blob) : default(T));
 }
 private async Task <WelcomeMessage> ReceiveMessage(ITransportHandler transportHandler)
 {
     return(_byteSerializer.Deserialize <WelcomeMessage>(await transportHandler.ReceiveAllAvailable()));
 }