CreateEngine() private method

private CreateEngine ( Raven.Abstractions.Data.ScriptedPatchRequest patch ) : JintEngine
patch Raven.Abstractions.Data.ScriptedPatchRequest
return Jint.JintEngine
Exemplo n.º 1
0
        public Jint.JintEngine CheckoutScript(ScriptedPatchRequest request)
        {
            CachedResult value;

            if (cacheDic.TryGetValue(request, out value))
            {
                Interlocked.Increment(ref value.Usage);
                Jint.JintEngine context;
                if (value.Queue.TryDequeue(out context))
                {
                    return(context);
                }
            }
            var result = ScriptedJsonPatcher.CreateEngine(request);

            var cachedResult = new CachedResult
            {
                Usage     = 1,
                Queue     = new ConcurrentQueue <Jint.JintEngine>(),
                Timestamp = SystemTime.UtcNow
            };

            cacheDic.AddOrUpdate(request, cachedResult, (_, existing) =>
            {
                Interlocked.Increment(ref existing.Usage);
                return(existing);
            });
            if (cacheDic.Count > CacheMaxSize)
            {
                foreach (var source in cacheDic
                         .OrderByDescending(x => x.Value.Usage)
                         .ThenBy(x => x.Value.Timestamp)
                         .Skip(CacheMaxSize))
                {
                    if (Equals(source.Key, request))
                    {
                        continue;                         // we don't want to remove the one we just added
                    }
                    CachedResult ignored;
                    cacheDic.TryRemove(source.Key, out ignored);
                }
            }

            return(result);
        }