示例#1
0
        /// <summary>
        /// 初始化
        /// </summary>
        public static void Initialize()
        {
            _directMutex = new Mutex(false, SecretUtil.MD5Encrypt32(CachePath));
            var doc = new XmlDocument();

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

            case ObCacheMode.Off:
                return;

            case ObCacheMode.Secret:
                _exteName = ".dat";
                doc       = new XmlEDocument();
                break;
            }
            if (!Directory.Exists(CachePath))
            {
                Directory.CreateDirectory(CachePath);
            }
            foreach (var filePath in Directory.GetFiles(CachePath, "*" + _exteName))
            {
                try
                {
                    doc.Load(filePath);
                }
                catch
                {
                    continue;
                }
                var obcache = doc.SelectSingleNode("obcache");
                if (obcache == null)
                {
                    return;
                }
                foreach (XmlElement childNode in obcache.ChildNodes)
                {
                    var model = new ObSqlcache
                    {
                        Version = childNode.GetAttribute("version"),
                        SqlText = childNode.ChildNodes[0].InnerText
                    };
                    var key = childNode.GetAttribute("key");
                    if (childNode.ChildNodes[1].InnerText.Length > 0)
                    {
                        var columns = childNode.ChildNodes[1].InnerText.Split(',');
                        model.ColumnNames = new List <string>();
                        foreach (var column in columns)
                        {
                            model.ColumnNames.Add(column);
                        }
                    }
                    lock (HCacheMemory)
                    {
                        try
                        {
                            if (!HCacheMemory.ContainsKey(key))
                            {
                                HCacheMemory.Add(key, model);
                            }
                        }
                        catch
                        {
                            //continue;
                        }
                    }
                }
            }
            Initialized = true;
        }
示例#2
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();
                }
            }
        }