示例#1
0
        public static void Add(string key, ObSqlcache model)
        {
            var doc = new XmlDocument();

            switch (CacheMode)
            {
            case ObCacheMode.On:
                break;

            case ObCacheMode.Off:
                return;

            case ObCacheMode.Secret:
                doc = new XmlEDocument();
                break;
            }
            lock (HCacheMemory)
            {
                if (HCacheMemory.Contains(key))
                {
                    HCacheMemory[key] = model;
                }
                else
                {
                    HCacheMemory.Add(key, model);
                }

                var di    = new DirectoryInfo(CachePath);
                var files = di.GetFiles("*" + _exteName);
                var fc    = new FileComparer();
                Array.Sort(files, fc);
                string filePath;
                if (files.Length != 0 && files[files.Length - 1].Length < FileSize * 1024 * 1024)
                {
                    filePath = files[files.Length - 1].FullName;
                }
                else
                {
                    filePath = CachePath + string.Format(FileName, Guid.NewGuid()) + _exteName;
                }

                _directMutex.WaitOne();
                try
                {
                    XmlNode obcache;
                    if (!File.Exists(filePath))
                    {
                        var dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
                        doc.AppendChild(dec);
                        obcache = doc.CreateElement("obcache");
                        doc.AppendChild(obcache);
                    }
                    else
                    {
                        doc.Load(filePath);
                        obcache = doc.SelectSingleNode("obcache");
                    }
                    var obsqlcacheNode = (XmlElement)doc.SelectSingleNode($"/obcache/obsqlcache[@key='{key}']");
                    if (obsqlcacheNode == null)
                    {
                        var obsqlcache = doc.CreateElement("obsqlcache");
                        obsqlcache.SetAttribute("key", key);
                        if (!string.IsNullOrEmpty(model.Version))
                        {
                            obsqlcache.SetAttribute("version", model.Version);
                        }
                        var sqltext = doc.CreateElement("sqltext");
                        sqltext.InnerText = model.SqlText;
                        var columns = doc.CreateElement("columns");
                        columns.InnerText = model.ColumnNamesToString();
                        obsqlcache.AppendChild(sqltext);
                        obsqlcache.AppendChild(columns);
                        obcache?.AppendChild(obsqlcache);
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(model.Version))
                        {
                            obsqlcacheNode.SetAttribute("version", model.Version);
                        }
                        var sqltext = obsqlcacheNode.ChildNodes[0];
                        var columns = obsqlcacheNode.ChildNodes[1];
                        sqltext.InnerText = model.SqlText;
                        columns.InnerText = model.ColumnNamesToString();
                    }
                    doc.Save(filePath);
                }
                finally
                {
                    _directMutex.ReleaseMutex();
                }
            }
        }