Exemple #1
0
        public Response GetNameFromIp(string ip)
        {
            var  stopwatch   = new Stopwatch();
            var  cacheHit    = false;
            var  obtained    = false;
            long waitingTime = 0;

            if (!_cacheIpToNameToRead.TryGetValue(ip, out var result))
            {
                obtained = true;
                result   = _nsLookup.GetNameFromIpSimple(ip);
                stopwatch.Start();
                lock (_cacheIpToNameToWrite)
                {
                    stopwatch.Stop();
                    waitingTime = stopwatch.ElapsedMilliseconds;
                    _cacheIpToNameToWrite[ip] = result;
                    var copy = _cacheIpToNameToWrite.ToImmutableDictionary();
                    _cacheIpToNameToRead = copy;
                }
            }
            else
            {
                cacheHit = true;
            }
            return(new Response(result, cacheHit, obtained, waitingTime));
        }
Exemple #2
0
        public Response GetNameFromIp(string ip)
        {
            if (!_cacheIpToNameToRead.TryGetValue(ip, out var result))
            {
                object lockObject = null;
                lock (_beingDownloaded)
                {
                    if (!_beingDownloaded.TryGetValue(ip, out lockObject))
                    {
                        _beingDownloaded[ip] = lockObject = new object();
                    }
                }

                lock (lockObject)
                {
                    if (!_cacheIpToNameToRead.TryGetValue(ip, out result))
                    {
                        result = _nsLookup.GetNameFromIpSimple(ip);
                        lock (_cacheIpToNameToWrite)
                        {
                            _cacheIpToNameToWrite[ip] = result;
                            _cacheIpToNameToRead      = _cacheIpToNameToWrite.ToImmutableDictionary();
                        }
                    }
                }
            }
            return(new Response(result));
        }
Exemple #3
0
        public Response GetNameFromIp(string ip)
        {
            var  stopwatch   = new Stopwatch();
            var  cacheHit    = false;
            var  obtained    = false;
            long waitingTime = 0;

            if (!_cacheIpToName.TryGetValue(ip, out var result))
            {
                stopwatch.Start();
                lock (_cacheIpToName)
                {
                    stopwatch.Stop();
                    waitingTime = stopwatch.ElapsedMilliseconds;
                    if (!_cacheIpToName.TryGetValue(ip, out result))
                    {
                        obtained           = true;
                        _cacheIpToName[ip] = result = _nsLookup.GetNameFromIpSimple(ip);
                    }
                    else
                    {
                        cacheHit = true;
                    }
                }
            }
            else
            {
                cacheHit = true;
            }
            return(new Response(result, cacheHit, obtained, waitingTime));
        }
        public Response GetNameFromIp(string ip)
        {
            var  stopwatch        = new Stopwatch();
            var  cacheHit         = false;
            var  obtained         = false;
            long waitingTimeCheck = 0;
            long waitingTimeSet   = 0;

            stopwatch.Start();
            if (!_memoryCache.TryGetValue <string>(ip, out var result))
            {
                stopwatch.Stop();
                waitingTimeCheck = stopwatch.ElapsedMilliseconds;
                result           = _nsLookup.GetNameFromIpSimple(ip);
                obtained         = true;
                stopwatch.Start();
                _memoryCache.Set(ip, result);
                stopwatch.Stop();
                waitingTimeSet = stopwatch.ElapsedMilliseconds;
            }
            else
            {
                cacheHit = true;
            }
            return(new Response(result, cacheHit, obtained, waitingTimeSet + waitingTimeCheck));
        }
 public Response GetNameFromIp(string ip)
 {
     if (!_cacheIpToName.TryGetValue(ip, out var result))
     {
         _cacheIpToName[ip] = result = _nsLookup.GetNameFromIpSimple(ip);
     }
     return(new Response(result));
 }
 public Response GetNameFromIp(string ip)
 {
     if (!_memoryCache.TryGetValue <string>(ip, out var result))
     {
         result = _nsLookup.GetNameFromIpSimple(ip);
         _memoryCache.Set(ip, result);
     }
     return(new Response(result));
 }
        public Response GetNameFromIp(string ip)
        {
            var  stopwatch               = new Stopwatch();
            var  cacheHit                = false;
            var  obtained                = false;
            long initialWaitingTime      = 0;
            long sameKeyWaitingTime      = 0;
            long writeToCacheWaitingTime = 0;

            if (!_cacheIpToNameToRead.TryGetValue(ip, out var result))
            {
                object lockObject = null;
                stopwatch.Start();
                lock (_beingDownloaded)
                {
                    stopwatch.Stop();
                    initialWaitingTime = stopwatch.ElapsedMilliseconds;
                    if (!_beingDownloaded.TryGetValue(ip, out lockObject))
                    {
                        _beingDownloaded[ip] = lockObject = new object();
                    }
                }

                stopwatch.Start();
                lock (lockObject)
                {
                    stopwatch.Stop();
                    sameKeyWaitingTime = stopwatch.ElapsedMilliseconds;
                    if (!_cacheIpToNameToRead.TryGetValue(ip, out result))
                    {
                        obtained = true;
                        result   = _nsLookup.GetNameFromIpSimple(ip);
                        stopwatch.Start();
                        lock (_cacheIpToNameToWrite)
                        {
                            stopwatch.Stop();
                            writeToCacheWaitingTime   = stopwatch.ElapsedMilliseconds;
                            _cacheIpToNameToWrite[ip] = result;
                            _cacheIpToNameToRead      = _cacheIpToNameToWrite.ToImmutableDictionary();
                        }
                    }
                    else
                    {
                        cacheHit = true;
                    }
                }
            }
            else
            {
                cacheHit = true;
            }

            return(new Response(result, cacheHit, obtained, initialWaitingTime + sameKeyWaitingTime + writeToCacheWaitingTime));
        }
Exemple #8
0
        public Response GetNameFromIp(string ip)
        {
            bool cacheHit = true;
            var  result   = _cacheIpToName.GetOrAdd(
                ip,
                new Lazy <string>(() =>
            {
                cacheHit = false;  return(_nsLookup.GetNameFromIpSimple(ip));
            }));

            return(new Response(result.Value, cacheHit, !cacheHit, 0));
        }
Exemple #9
0
 public Response GetNameFromIp(string ip)
 {
     if (!_cacheIpToNameToRead.TryGetValue(ip, out var result))
     {
         result = _nsLookup.GetNameFromIpSimple(ip);
         lock (_cacheIpToNameToWrite)
         {
             _cacheIpToNameToWrite[ip] = result;
             var copy = _cacheIpToNameToWrite.ToImmutableDictionary();
             _cacheIpToNameToRead = copy;
         }
     }
     return(new Response(result));
 }
Exemple #10
0
        public Response GetNameFromIp(string ip)
        {
            if (!_cacheIpToName.TryGetValue(ip, out var result))
            {
                lock (_cacheIpToName) // Add a Lazy instance to the dictionary only one at the time
                {
                    if (!_cacheIpToName.TryGetValue(ip, out result))
                    {
                        _cacheIpToName[ip] = result = new Lazy <string>(() => _nsLookup.GetNameFromIpSimple(ip));
                    }
                }
            }

            return(new Response(result.Value));
        }
        public Response GetNameFromIp(string ip)
        {
            bool cacheHit = true;

            if (!_cacheIpToName.TryGetValue(ip, out var result))
            {
                lock (_cacheIpToName)
                {
                    if (!_cacheIpToName.TryGetValue(ip, out result))
                    {
                        _cacheIpToName[ip] = result = new Lazy <string>(() =>
                        {
                            cacheHit = false;
                            return(_nsLookup.GetNameFromIpSimple(ip));
                        });
                    }
                }
            }

            return(new Response(result.Value, cacheHit, !cacheHit, 0));
        }
 private string ObtainUncachedValue(string ip)
 {
     return(_nsLookup.GetNameFromIpSimple(ip));
 }
 public Response GetNameFromIp(string ip)
 {
     return(new Response(_nsLookup.GetNameFromIpSimple(ip)));
 }
        public Response GetNameFromIp(string ip)
        {
            var result = _cacheIpToName.GetOrAdd(ip, new Lazy <string>(() => _nsLookup.GetNameFromIpSimple(ip)));

            return(new Response(result.Value));
        }