Esempio n. 1
0
        /// <summary>
        /// 返回缓冲池选项无效的异常。
        /// </summary>
        /// <param name="element">无效的缓冲池配置元素。</param>
        /// <returns><see cref="System.Configuration.ConfigurationErrorsException"/> 对象。</returns>
        internal static ConfigurationErrorsException InvalidCacheOptions(CacheElement element)
        {
            string message = ExceptionResources.GetString("InvalidCacheOptions", element.CacheType);

            return(new ConfigurationErrorsException(message,
                                                    element.ElementInformation.Source, element.ElementInformation.LineNumber));
        }
        public float GetLevel(PawnCapacityDef capacity)
        {
            if (this.pawn.health.Dead)
            {
                return(0f);
            }
            if (this.cachedCapacityLevels == null)
            {
                this.Notify_CapacityLevelsDirty();
            }
            CacheElement cacheElement = this.cachedCapacityLevels[capacity];

            if (cacheElement.status == CacheStatus.Caching)
            {
                Log.Error(string.Format("Detected infinite stat recursion when evaluating {0}", capacity));
                return(0f);
            }
            if (cacheElement.status == CacheStatus.Uncached)
            {
                cacheElement.status = CacheStatus.Caching;
                cacheElement.value  = PawnCapacityUtility.CalculateCapacityLevel(this.pawn.health.hediffSet, capacity, null);
                cacheElement.status = CacheStatus.Cached;
            }
            return(cacheElement.value);
        }
Esempio n. 3
0
        public float GetLevel(PawnCapacityDef capacity)
        {
            if (pawn.health.Dead)
            {
                return(0f);
            }
            if (cachedCapacityLevels == null)
            {
                Notify_CapacityLevelsDirty();
            }
            CacheElement cacheElement = cachedCapacityLevels[capacity];

            if (cacheElement.status == CacheStatus.Caching)
            {
                Log.Error($"Detected infinite stat recursion when evaluating {capacity}");
                return(0f);
            }
            if (cacheElement.status == CacheStatus.Uncached)
            {
                cacheElement.status = CacheStatus.Caching;
                try
                {
                    cacheElement.value = PawnCapacityUtility.CalculateCapacityLevel(pawn.health.hediffSet, capacity);
                }
                finally
                {
                    cacheElement.status = CacheStatus.Cached;
                }
            }
            return(cacheElement.value);
        }
Esempio n. 4
0
        /// <summary>
        /// 返回缓冲池选项无效的异常。
        /// </summary>
        /// <param name="element">无效的缓冲池配置元素。</param>
        /// <returns><see cref="ConfigurationErrorsException"/> 对象。</returns>
        internal static ConfigurationErrorsException InvalidCacheOptions(CacheElement element)
        {
            Contract.Requires(element != null);
            string message = Format(ExceptionResources.InvalidCacheOptions, element.CacheType);

            return(GetConfigurationErrorsException(message, element.ElementInformation));
        }
Esempio n. 5
0
        /// <summary>
        /// Gets a score from the cache.
        /// Get it from the cache if the score has not expired or download it.
        /// </summary>
        /// <param name="url"></param>
        /// <param name="encoding"></param>
        /// <param name="reload"></param>
        /// <returns></returns>
        public string GetScore(string url, string encoding, bool reload)
        {
            if (m_client.IsBusy)
            {
                return("");
            }

            string html = String.Empty;

            if (!reload && m_cache.ContainsKey(url))
            {
                if (!m_cache[url].Expired)
                {
                    html = m_cache[url].Value;
                }
            }

            if (String.IsNullOrEmpty(html))
            {
                Tools.LogMessage("Download = {0}", url);
                html = Tools.DownloadFile(m_client, url, encoding);
                html = Tools.FixHtml(html);
                if (m_lifeTime > 0)
                {
                    m_cache[url] = new CacheElement(m_lifeTime, html);
                }
            }

            return(html);
        }
Esempio n. 6
0
 public GridSectionCache(int size)
 {
     cache = new CacheElement[size];
     for (int i = 0; i < size; i++)
     {
         cache[i] = new CacheElement();
     }
     hashCache = new Dictionary <Vector2Int, Section>(size);
     this.size = size;
 }
Esempio n. 7
0
        private async Task AddOrUpdateCache(MessageData response, int index, string key)
        {
            var span     = TimeSpan.FromSeconds(response.MaxAge);
            var newEntry = new CacheElement(DateTime.Now + span, new List <MessageData>()
            {
                response
            });

            await SaveInRedis(key, newEntry, span);
        }
Esempio n. 8
0
 public static bool GetLevel(PawnCapacitiesHandler __instance, ref float __result, PawnCapacityDef capacity)
 {
     if (__instance.pawn.health.Dead)
     {
         __result = 0f;
         return(false);
     }
     if (__instance.cachedCapacityLevels == null)
     {
         __instance.Notify_CapacityLevelsDirty();
     }
     lock (__instance)
     {
         CacheElement cacheElement = __instance.cachedCapacityLevels[capacity];
         if (cacheElement.status == CacheStatus.Caching)
         {
             Log.Error($"Detected infinite stat recursion when evaluating {capacity}");
             __result = 0f;
             return(false);
         }
         if (cacheElement.status == CacheStatus.Uncached)
         {
             cacheElement.status = CacheStatus.Caching;
             try
             {
                 cacheElement.value = PawnCapacityUtility.CalculateCapacityLevel(__instance.pawn.health.hediffSet, capacity);
             }
             finally
             {
                 cacheElement.status = CacheStatus.Cached;
             }
         }
         __result = cacheElement.value;
         return(false);
     }
 }
Esempio n. 9
0
        public Func<IEnumerable<CacheElement>> PrepareToExecute(IEnumerable<string> toSelect)
        {
            var selectList = toSelect.ToList();
            if (selectList.Count == 0) return () => new List<CacheElement>();

            var selectOp = selectOps[selectList.Count - 1];
            var now = sched.Now;

            return (() => 
            {
                var result = new List<CacheElement>();
                try 
                {
                    for (int i = 0; i < selectList.Count; i++) 
                    {
                        this.Checked(raw.sqlite3_bind_text(selectOp, i+1, selectList[i]));
                    }

                    while (this.Checked(raw.sqlite3_step(selectOp)) == SQLite3.Result.Row) 
                    {
                        var ce = new CacheElement() {
                            Key = raw.sqlite3_column_text(selectOp, 0), 
                            TypeName = raw.sqlite3_column_text(selectOp, 1), 
                            Value = raw.sqlite3_column_blob(selectOp, 2),
                            Expiration = new DateTime(raw.sqlite3_column_int64(selectOp, 3)),
                            CreatedAt = new DateTime(raw.sqlite3_column_int64(selectOp, 4)),
                        };

                        if (now.UtcTicks <= ce.Expiration.Ticks) result.Add(ce);
                    }
                } 
                finally 
                {
                    this.Checked(raw.sqlite3_reset(selectOp));
                }

                return result;
            });
        }
Esempio n. 10
0
        /// <summary>
        /// Takes the hash identifying the urls of the files that make up a combined file.
        /// Returns the compressed content of the combined files, and the version ID
        /// of the combined files. The version ID is based on the last modified time of the last
        /// modified file file that goes into the combined file.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="urlsHash"></param>
        /// <param name="totalFileNames">
        /// The file names of the files read by this method get added to this list.
        /// If this is null, nothing is done with this parameter.
        /// </param>
        /// <param name="combinedFileContent">
        /// Content to be sent back to the browser. 
        /// Will be null if the content could not be retrieved, because the hash was not found in
        /// the Application object. This means that the file tag that caused the browser to 
        /// request this file was generated in JavaScript or appeared outside the head tag
        /// on the page. This will also happen in debug mode. 
        /// In this case, the name of the requested file matches an actual
        /// file on the server.
        /// </param>
        /// <param name="versionId"></param>
        private static void GetContentVersion(
            HttpContext context, 
            string urlsHash,
            UrlProcessor urlProcessor,
            List<string> totalFileNames,
            bool minifyCSS, bool minifyJavaScript,
            out string combinedFileContent,
            out string versionId, 
            out FileTypeUtilities.FileType fileType)
        {
            combinedFileContent = null;
            versionId = null;

            List<Uri> fileUrls;
            RetrieveFileUrls(context, urlsHash, out fileUrls, out fileType);
            if (fileUrls == null)
            {
                return;
            }

            CacheElement cacheElement = (CacheElement)context.Cache[urlsHash];
            if (cacheElement == null)
            {
                StringBuilder combinedContentSb = new StringBuilder();
                DateTime mostRecentModifiedTime = DateTime.MinValue;
                List<string> fileNames = new List<string>();
                bool fileMissing = false;

                foreach (Uri fileUrl in fileUrls)
                {
                    string filePath = MapPath(fileUrl.AbsolutePath, urlProcessor.ThrowExceptionOnMissingFile);
                    string fileContent = null;

                    if (filePath != null)
                    {
                        fileContent = File.ReadAllText(filePath);
                        if (fileType == FileTypeUtilities.FileType.CSS)
                        {
                            FixUrlProperties(ref fileContent, fileUrl, urlProcessor);
                        }

                        DateTime lastModifiedTime = File.GetLastWriteTime(filePath);

                        mostRecentModifiedTime =
                            (mostRecentModifiedTime > lastModifiedTime) ?
                                mostRecentModifiedTime : lastModifiedTime;

                        fileNames.Add(filePath);
                        if (totalFileNames != null) { totalFileNames.Add(filePath); }
                    }
                    else
                    {
                        // A comment starting with /*! doesn't get removed by the minifier
                        fileContent = string.Format("\n/*!\n** Does not exist: {0}\n*/\n", fileUrl);

                        fileMissing = true;
                    }

                    combinedContentSb.Append(fileContent);
                }

                string combinedContent = combinedContentSb.ToString();
                if (!string.IsNullOrEmpty(combinedContent))
                {
                    cacheElement = new CacheElement();

                    cacheElement.CombinedFileContent = combinedContent;
                    if (fileType == FileTypeUtilities.FileType.JavaScript)
                    {
                        if (minifyJavaScript)
                        {
                            cacheElement.CombinedFileContent = JavaScriptCompressor.Compress(combinedContent);
                        }
                    }
                    else
                    {
                        if (minifyCSS)
                        {
                            cacheElement.CombinedFileContent = CssCompressor.Compress(combinedContent);
                        }
                    }

                    cacheElement.VersionId = VersionId(mostRecentModifiedTime);

                    // Cache the newly created cacheElement
                    // 
                    // Do not cache the cacheElement if one of the files couldn't be found.
                    // That way, the package will keep checking the missing file, and pick it up
                    // when someone puts the file there.
                    if (!fileMissing)
                    {
                        CacheDependency cd = new CacheDependency(fileNames.ToArray());
                        context.Cache.Insert(urlsHash, cacheElement, cd);
                    }
                }
            }

            if (cacheElement == null)
            {
                if (context.IsDebuggingEnabled) { throw new Exception("cacheElement == null"); }

                combinedFileContent = "";
                versionId = "";
            }
            else
            {
                combinedFileContent = cacheElement.CombinedFileContent;
                versionId = cacheElement.VersionId;
            }
        }