Пример #1
0
        public AdDTO Get()
        {
            MemoryCacheEntryOptions cacheExpirationOptions = new MemoryCacheEntryOptions();

            cacheExpirationOptions.SetAbsoluteExpiration(TimeSpan.FromMinutes(2));
            string key = "ad";
            AdDTO  ad;

            if (!cache.TryGetValue <AdDTO>(key, out ad))
            {
                do
                {
                    ad = AdMapping.MapDomainToDTOObject(adMicroService.Get());
                }while (ad.AdEncoding.Length < 100);
                cache.Set <AdDTO>(key, ad, cacheExpirationOptions);
            }
            _logger.LogInformation("Ad request ," + DateTime.UtcNow);
            return((AdDTO)cache.Get("ad"));
        }
Пример #2
0
        private void InitializeConsumer()
        {
            EventingBasicConsumer eventingBasicConsumer = new EventingBasicConsumer(channel);

            eventingBasicConsumer.Received += (sender, basicDeliveryEventArgs) =>
            {
                // Get message from sender
                string message = Encoding.UTF8.GetString(basicDeliveryEventArgs.Body);
                channel.BasicAck(basicDeliveryEventArgs.DeliveryTag, false);

                // Get add from ad microservice
                AdMicroService adMicroService = new AdMicroService();
                Ad             ad             = adMicroService.Get();

                // Send response message
                IBasicProperties replyBasicProperties = channel.CreateBasicProperties();
                replyBasicProperties.CorrelationId = basicDeliveryEventArgs.BasicProperties.CorrelationId;
                byte[] responseBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(ad));
                channel.BasicPublish("", basicDeliveryEventArgs.BasicProperties.ReplyTo, replyBasicProperties, responseBytes);
            };

            channel.BasicConsume("ad.queue", false, eventingBasicConsumer);
        }