Exemplo n.º 1
0
        private bool SwapIdentifier(DicomFileMessage msg, string patientId, out string errorReason)
        {
            string to = _swapper.GetSubstitutionFor(patientId, out errorReason);

            if (to == null)
            {
                errorReason = "Swapper " + _swapper + " returned null";
                return(false);
            }

            msg.DicomDataset = msg.DicomDataset.Replace(":[\"" + patientId + "\"]", ":[\"" + to + "\"]");

            return(true);
        }
Exemplo n.º 2
0
        public override string GetSubstitutionFor(string toSwap, out string reason)
        {
            string result;

            reason = null;

            //lookup in memory
            if (!_cache.TryGetValue(toSwap, out result))
            {
                SemaphoreSlim locket = _locks.GetOrAdd(toSwap, k => new SemaphoreSlim(1, 1));
                locket.Wait();
                try
                {
                    if (!_cache.TryGetValue(toSwap, out result))
                    {
                        // Now try Redis cache
                        IDatabase db  = _redis.GetDatabase();
                        var       val = db.StringGet(toSwap);
                        //we have a cached answer (which might be null)
                        if (val.HasValue)
                        {
                            result = val.ToString();
                            Interlocked.Increment(ref CacheHit);
                        }
                        else
                        {
                            //we have no cached answer from Redis
                            Interlocked.Increment(ref CacheMiss);

                            //Go to the hosted swapper
                            lock (_hostedSwapper)
                            {
                                result = _hostedSwapper.GetSubstitutionFor(toSwap, out reason);
                            }

                            //and cache the result (even if it is null - no lookup match found)
                            db.StringSet(toSwap, result ?? NullString, null, When.NotExists);
                        }

                        _cache.Set(toSwap, result ?? NullString, new MemoryCacheEntryOptions()
                        {
                            Size = 1
                        });
                    }
                }
                finally
                {
                    locket.Release();
                }
            }
            else
            {
                Interlocked.Increment(ref CacheHit);
            }

            if (string.Equals(NullString, result))
            {
                result = null;
                reason = $"Value '{toSwap}' was cached in Redis as missing (i.e. no mapping was found)";
            }

            if (result == null)
            {
                Interlocked.Increment(ref Fail);
            }
            else
            {
                int res = Interlocked.Increment(ref Success);
                if (res % 1000 == 0)
                {
                    LogProgress(_logger, LogLevel.Info);
                }
            }

            return(result);
        }