Ejemplo n.º 1
0
        public void CheckinScript(ScriptedPatchRequest request, Engine context, RavenJObject customFunctions)
        {
            CachedResult cacheByCustomFunctions;

            var patchRequestAndCustomFunctionsTuple = new ScriptedPatchRequestAndCustomFunctionsToken(request, customFunctions);
            if (cacheDic.TryGetValue(patchRequestAndCustomFunctionsTuple, out cacheByCustomFunctions))
            {
                if (cacheByCustomFunctions.Queue.Count > 20)
                    return;
                cacheByCustomFunctions.Queue.Enqueue(context);
                return;
            }
            cacheDic.AddOrUpdate(patchRequestAndCustomFunctionsTuple, patchRequest =>
            {
                var queue = new ConcurrentQueue<Engine>();

                return new CachedResult
                {
                    Queue = queue,
                    Timestamp = SystemTime.UtcNow,
                    Usage = 1
                };
            }, (patchRequest, result) =>
            {
                result.Queue.Enqueue(context);
                return result;
            });
        }
Ejemplo n.º 2
0
        public void CheckinScript(ScriptedPatchRequest request, Engine context, RavenJObject customFunctions)
        {
            CachedResult cacheByCustomFunctions;

            var patchRequestAndCustomFunctionsTuple = new ScriptedPatchRequestAndCustomFunctionsToken(request, customFunctions);

            if (cacheDic.TryGetValue(patchRequestAndCustomFunctionsTuple, out cacheByCustomFunctions))
            {
                if (cacheByCustomFunctions.Queue.Count > 20)
                {
                    return;
                }
                cacheByCustomFunctions.Queue.Enqueue(context);
                return;
            }
            cacheDic.AddOrUpdate(patchRequestAndCustomFunctionsTuple, patchRequest =>
            {
                var queue = new ConcurrentQueue <Engine>();

                return(new CachedResult
                {
                    Queue = queue,
                    Timestamp = SystemTime.UtcNow,
                    Usage = 1
                });
            }, (patchRequest, result) =>
            {
                result.Queue.Enqueue(context);
                return(result);
            });
        }
Ejemplo n.º 3
0
        public Engine GetEngine(Func <PatchRequest, Engine> createEngine, PatchRequest request, string customFunctions)
        {
            CachedResult value;
            var          patchRequestAndCustomFunctionsTuple = new ScriptedPatchRequestAndCustomFunctionsToken(request, customFunctions);

            if (_cache.TryGetValue(patchRequestAndCustomFunctionsTuple, out value))
            {
                value.Usage++;
                return(value.Engine);
            }
            var result = createEngine(request);

            if (string.IsNullOrWhiteSpace(customFunctions) == false)
            {
                result.Execute(string.Format(@"var customFunctions = function() {{  var exports = {{ }}; {0};
                            return exports;
                        }}();
                        for(var customFunction in customFunctions) {{
                            this[customFunction] = customFunctions[customFunction];
                        }};", customFunctions), new ParserOptions {
                    Source = "customFunctions.js"
                });
            }
            var cachedResult = new CachedResult
            {
                Usage     = 1,
                Timestamp = SystemTime.UtcNow,
                Engine    = result
            };

            if (_cache.Count > CacheMaxSize)
            {
                foreach (var item in _cache.OrderBy(x => x.Value?.Usage)
                         .ThenByDescending(x => x.Value?.Timestamp)
                         .Take(CacheMaxSize / 10)
                         .Select(source => source.Key)
                         .ToList())
                {
                    _cache.Remove(item);
                }
            }
            _cache[patchRequestAndCustomFunctionsTuple] = cachedResult;


            return(result);
        }
Ejemplo n.º 4
0
        public Engine CheckoutScript(Func<ScriptedPatchRequest, Engine> createEngine, ScriptedPatchRequest request, RavenJObject customFunctions)
        {
            CachedResult value;
            var patchRequestAndCustomFunctionsTuple = new ScriptedPatchRequestAndCustomFunctionsToken(request, customFunctions);
            if (cacheDic.TryGetValue(patchRequestAndCustomFunctionsTuple, out value))
            {
                Interlocked.Increment(ref value.Usage);
                Engine context;
                if (value.Queue.TryDequeue(out context))
                {
                    return context;
                }
            }
            var result = createEngine(request);

            RavenJToken functions;
            if (customFunctions != null && customFunctions.TryGetValue("Functions", out functions))

                result.Execute(string.Format(@"var customFunctions = function() {{  var exports = {{ }}; {0};
                            return exports;
                        }}();
                        for(var customFunction in customFunctions) {{
                            this[customFunction] = customFunctions[customFunction];
                        }};", functions), new ParserOptions { Source = "customFunctions.js" });
            var cachedResult = new CachedResult
            {
                Usage = 1,
                Queue = new ConcurrentQueue<Engine>(),
                Timestamp = SystemTime.UtcNow
            };

            cacheDic.AddOrUpdate(patchRequestAndCustomFunctionsTuple, cachedResult, (_, existing) =>
            {
                Interlocked.Increment(ref existing.Usage);
                return existing;
            });
            if (cacheDic.Count > CacheMaxSize)
            {
                foreach (var source in cacheDic
                    .Where(x => x.Value != null)
                    .OrderByDescending(x => x.Value.Usage)
                    .ThenBy(x => x.Value.Timestamp)
                    .Skip(CacheMaxSize - CacheMaxSize / 10))
                {
                    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);
                }
                foreach (var source in cacheDic.Where(x => x.Value == null))
                {
                    CachedResult ignored;
                    cacheDic.TryRemove(source.Key, out ignored);
                }
            }

            return result;
        }
Ejemplo n.º 5
0
        public Engine CheckoutScript(Func <ScriptedPatchRequest, Engine> createEngine, ScriptedPatchRequest request, RavenJObject customFunctions)
        {
            CachedResult value;
            var          patchRequestAndCustomFunctionsTuple = new ScriptedPatchRequestAndCustomFunctionsToken(request, customFunctions);

            if (cacheDic.TryGetValue(patchRequestAndCustomFunctionsTuple, out value))
            {
                Interlocked.Increment(ref value.Usage);
                Engine context;
                if (value.Queue.TryDequeue(out context))
                {
                    return(context);
                }
            }
            var result = createEngine(request);

            RavenJToken functions;

            if (customFunctions != null && customFunctions.TryGetValue("Functions", out functions))
            {
                result.Execute(string.Format(@"var customFunctions = function() {{  var exports = {{ }}; {0};
                            return exports;
                        }}();
                        for(var customFunction in customFunctions) {{
                            this[customFunction] = customFunctions[customFunction];
                        }};", functions), new ParserOptions {
                    Source = "customFunctions.js"
                });
            }
            var cachedResult = new CachedResult
            {
                Usage     = 1,
                Queue     = new ConcurrentQueue <Engine>(),
                Timestamp = SystemTime.UtcNow
            };

            cacheDic.AddOrUpdate(patchRequestAndCustomFunctionsTuple, cachedResult, (_, existing) =>
            {
                Interlocked.Increment(ref existing.Usage);
                return(existing);
            });
            if (cacheDic.Count > CacheMaxSize)
            {
                foreach (var source in cacheDic
                         .Where(x => x.Value != null)
                         .OrderByDescending(x => x.Value.Usage)
                         .ThenBy(x => x.Value.Timestamp)
                         .Skip(CacheMaxSize - CacheMaxSize / 10))
                {
                    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);
                }
                foreach (var source in cacheDic.Where(x => x.Value == null))
                {
                    CachedResult ignored;
                    cacheDic.TryRemove(source.Key, out ignored);
                }
            }

            return(result);
        }