Exemple #1
0
        /// <summary>
        /// Evaluates current state of the memory consumption
        /// </summary>
        /// <returns></returns>
        public MemoryWatchDirective Evaluate()
        {
            if (DateTime.Now.Subtract(lastManagementDecision).TotalSeconds < evaluationSecondsTick)
            {
                return(currentMode);
            }

            MemoryWatchDirective output = MemoryWatchDirective.normal;

            currentProcess.Refresh();

            currentlyUsed = currentProcess.WorkingSet64 / MEM_UNIT;

            if (currentlyUsed > limit_critical)
            {
                output = MemoryWatchDirective.flush;
            }
            else if (currentlyUsed > limit_normal)
            {
                output = MemoryWatchDirective.prevent;
            }

            currentMode            = output;
            lastManagementDecision = DateTime.Now;

            return(output);
        }
        /// <summary>
        /// Gets the document by the ID, if it's not already loaded into HTML DOM it will use the html source provided to load it and store into registry
        /// </summary>
        /// <param name="AssociatedID">The associated identifier.</param>
        /// <param name="html">The HTML.</param>
        /// <returns></returns>
        public HtmlDocument GetDocument(String AssociatedID, String html)
        {
            HtmlDocument output = null;

            stats.GetCalls++;

            MemoryWatchDirective memEval = memoryWatch.Evaluate();

            if (memEval == MemoryWatchDirective.flush)
            {
                IsEnabled = false;
                Dispose();
                IsEnabled = true;
            }


            if (IsEnabled && DocumentRegistry.ContainsKey(AssociatedID))
            {
                stats.GetFromMemory++;
                output = DocumentRegistry[AssociatedID];
            }
            else
            {
                output = new HtmlDocument();
                output.OptionOutputUpperCase = true;


                output.LoadHtml(html);

                stats.GetFromFile++;

                if (memEval == MemoryWatchDirective.normal)
                {
                    if (IsEnabled)
                    {
                        if (!DocumentRegistry.ContainsKey(AssociatedID))
                        {
                            lock (AddDictionaryLock)
                            {
                                if (!DocumentRegistry.ContainsKey(AssociatedID))
                                {
                                    stats.SetCalls++;
                                    DocumentRegistry.Add(AssociatedID, output);
                                }
                            }
                        }
                    }
                }
                else
                {
                    stats.SetPrevented++;
                }
            }

            return(output);
        }