예제 #1
0
        private IDnsQueryResponse Query(DnsQuestion question)
        {
            if (question == null)
            {
                throw new ArgumentNullException(nameof(question));
            }

            var head    = new DnsRequestHeader(GetNextUniqueId(), Recursion, DnsOpCode.Query);
            var request = new DnsRequestMessage(head, question);
            var handler = UseTcpOnly ? _tcpFallbackHandler : _messageHandler;

            if (_cache.Enabled)
            {
                var cacheKey = ResponseCache.GetCacheKey(question);
                var item     = _cache.Get(cacheKey);
                if (item == null)
                {
                    item = ResolveQuery(handler, request);
                    _cache.Add(cacheKey, item);
                }

                return(item);
            }
            else
            {
                return(ResolveQuery(handler, request));
            }
        }
예제 #2
0
        private async Task <IDnsQueryResponse> QueryAsync(DnsQuestion question, CancellationToken cancellationToken)
        {
            if (question == null)
            {
                throw new ArgumentNullException(nameof(question));
            }

            var head    = new DnsRequestHeader(GetNextUniqueId(), Recursion, DnsOpCode.Query);
            var request = new DnsRequestMessage(head, question);
            var handler = UseTcpOnly ? _tcpFallbackHandler : _messageHandler;

            if (_cache.Enabled)
            {
                var cacheKey = ResponseCache.GetCacheKey(question);
                var item     = _cache.Get(cacheKey);
                if (item == null)
                {
                    item = await ResolveQueryAsync(handler, request, cancellationToken).ConfigureAwait(false);

                    _cache.Add(cacheKey, item);
                }

                return(item);
            }
            else
            {
                return(await ResolveQueryAsync(handler, request, cancellationToken).ConfigureAwait(false));
            }
        }
예제 #3
0
        private static void DoCleanup(ResponseCache cache)
        {
            cache._cleanupRunning = true;

            var now = DateTimeOffset.UtcNow;
            foreach (var entry in cache._cache)
            {
                if (entry.Value.IsExpiredFor(now))
                {
                    cache._cache.TryRemove(entry.Key, out ResponseEntry o);
                }
            }

            cache._cleanupRunning = false;
        }