コード例 #1
0
            private Script GenerateContent()
            {
                byte[] ub = null;
                byte[] cb = null;

                string scriptText = Generator.GetScript();

                using (var ms = new MemoryStream(scriptText.Length))
                {
                    using (var sw = new StreamWriter(ms, new UTF8Encoding(true)))
                    {
                        sw.Write(scriptText);
                        sw.Flush();

                        ub = ms.ToArray();
                        ms.Seek(0, SeekOrigin.Begin);

                        if (ms.Length > 4096)
                        {
                            using (var cs = new MemoryStream((int)ms.Length))
                            {
                                using (var gz = new GZipStream(cs, CompressionMode.Compress))
                                {
                                    ms.CopyTo(gz);
                                    gz.Flush();
                                }

                                cb = cs.ToArray();
                            }
                        }
                    }

                    var script = new Script
                    {
                        Hash              = GetMD5HashString(ub),
                        Time              = DateTime.UtcNow,
                        ScriptText        = scriptText,
                        CompressedBytes   = cb,
                        UncompressedBytes = ub,
                        Expiration        = Generator.Expiration == TimeSpan.Zero ? DateTime.MaxValue :
                                            DateTime.Now.Add(Generator.Expiration)
                    };

                    this.content = script;

                    if (Generator.GroupKey == null)
                    {
                        return(script);
                    }

                    TwoLevelCache.GetLocalStoreOnly("DynamicScriptCheck:" + this.Name, Generator.Expiration,
                                                    Generator.GroupKey, () =>
                    {
                        return(new object());
                    });

                    return(script);
                }
            }
コード例 #2
0
        private IScriptContent EnsureScriptContent(string name, IDynamicScript script)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            var cacheKey = "DynamicScript:" + name;

            if (script is ICacheSuffix ics)
            {
                cacheKey = cacheKey + ":" + ics.CacheSuffix;
            }

            ScriptContent factory()
            {
                var content = utf8Encoding.GetBytes(script.GetScript());

                return(new ScriptContent(content, DateTime.UtcNow, content.Length > 4096));
            }

            var groupKey = script.GroupKey;

            ScriptContent getOrCreate()
            {
                if (groupKey == null)
                {
                    return(cache.Memory.Get(cacheKey, script.Expiration, factory));
                }
                else
                {
                    return(cache.GetLocalStoreOnly(cacheKey, script.Expiration, groupKey, factory));
                }
            };

            var scriptContent = getOrCreate();

            if (scriptLastChange.TryGetValue(name, out DateTime lastChange) &&
                lastChange >= scriptContent.Time)
            {
                if (groupKey == null)
                {
                    cache.Memory.Remove(cacheKey);
                }
                else
                {
                    cache.Remove(cacheKey);
                }

                return(getOrCreate());
            }

            return(scriptContent);
        }