示例#1
0
        /// <summary>
        /// Loads an XML file into memory for faster read / write
        /// </summary>
        /// <param name="fileName">File to load</param>
        /// <param name="timeOut">Time out in seconds - Default 2 minutes</param>
        /// <param name="rootNode">Root node of XML File</param>
        public static void BeginUpdate(string fileName, ulong timeOut = 120, string rootNode = "WebDefender")
        {
            // min time out 5 seconds, max timeout 20 minutes
            timeOut = Utilities.CheckMinMaxU(timeOut, 5, 20 * 60);

            using (TimedLock.Lock(_lockObject))
            {
                // is the file still in memory
                if (_memoryXMLFile.ContainsKey(fileName))
                {
                    return;
                }
            }

            // not found, add to cache
            XMLMemoryFile memFile = new XMLMemoryFile(fileName, timeOut);
            XmlDocument   doc     = memFile.Document;

            LoadXMLFile(ref doc, fileName, rootNode);

            using (TimedLock.Lock(_lockObject))
            {
                _memoryXMLFile.Add(fileName, memFile);
            }
        }
示例#2
0
        /// <summary>
        /// Ends an update on the file and removes from cache
        /// </summary>
        /// <param name="fileName">xml file</param>
        /// <param name="save">Determines wether contents should be saved</param>
        public static void EndUpdate(string fileName, bool save)
        {
            using (TimedLock.Lock(_lockObject))
            {
                // is the file still in memory
                if (!_memoryXMLFile.ContainsKey(fileName))
                {
                    return;
                }

                // found, save file and remove from cache
                if (save)
                {
                    XMLMemoryFile memFile = _memoryXMLFile[fileName];
                    memFile.Document.Save(memFile.FileName);
                }

                _memoryXMLFile.Remove(fileName);
            }
        }