public V8Script Compile(string scriptId, string code, bool addToCache = true, int?cacheExpirationSeconds = null)
        {
            CachedV8Script cachedScript;

            if (TryGetCached(scriptId, out cachedScript))
            {
                return(cachedScript.Script);
            }

            V8Script compiledScript = _v8Runtime.Compile(scriptId, code);

            if (addToCache)
            {
                if (!cacheExpirationSeconds.HasValue)
                {
                    cacheExpirationSeconds = _settings.ScriptCacheExpirationSeconds;
                }
                if (cacheExpirationSeconds > 0)
                {
                    var cacheEntry = new CachedV8Script(compiledScript, cacheExpirationSeconds.Value);
                    _scriptCache.AddOrUpdate(scriptId, cacheEntry, (key, original) => cacheEntry);
                }
            }

            return(compiledScript);
        }
 public bool TryGetCached(string scriptId, out CachedV8Script cachedScript)
 {
     if (_scriptCache.TryGetValue(scriptId, out cachedScript))
     {
         if (cachedScript.ExpiresOn > DateTime.UtcNow)
         {
             cachedScript.CacheHits++;
             return(true);
         }
         _scriptCache.TryRemove(scriptId, out cachedScript);
     }
     cachedScript = null;
     return(false);
 }
예제 #3
0
 public bool TryGetCached(string scriptId, out CachedV8Script script)
 {
     return(_scriptCompiler.TryGetCached(scriptId, out script));
 }