예제 #1
0
        /// <summary>
        /// 数据初始化
        /// </summary>
        /// <param name="execAction">处理批量数据</param>
        /// <param name="interval">暂存时间 毫秒</param>
        /// <param name="poolName"></param>
        private void InitDataBufferPool(Action <List <TItem> > execAction, int interval = 5000, string poolName = "DataBufferPool")
        {
            DataBufferPoolName = typeof(TItem).Name + "_" + poolName;

            _itemsQueue.Clear();

            AutoClock.Regist(() =>
            {
                List <TItem> ls = null;
                lock (LockObj)
                {
                    while (_itemsQueue.Count > 0)
                    {
                        if (ls == null)
                        {
                            ls = new List <TItem>();
                        }
                        ls.Add(_itemsQueue.Dequeue());
                    }
                }

                LogHelper.CustomInfoEnabled = true;
                string log = string.Format("本次执行【{0}】条数据", ls == null ? "null" : ls.Count.ToString());
                LogHelper.CustomInfo(log, DataBufferPoolName);

                if (ls != null && ls.Count > 0 && execAction != null)
                {
                    execAction(ls);
                    ls.Clear();
                }
            }, interval);
        }
예제 #2
0
        private static void RegistLocalDB()
        {
            string dbFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "db");

            if (!Directory.Exists(dbFilePath))
            {
                Directory.CreateDirectory(dbFilePath);
            }

            string dbFileName = (typeof(T).Namespace + "_" + typeof(T).Name).Replace(".", "_") + ".db";
            string fileName   = dbFilePath + "/" + dbFileName;

            if (File.Exists(fileName))
            {
                try
                {
                    string allStr = File.ReadAllText(fileName);
                    allSource = SerializerHelper.JsonDeserialize <List <T> >(allStr);
                }
                catch (Exception ex)
                {
                    LogHelper.CustomInfoEnabled = true;
                    LogHelper.CustomInfo(ex, "RegistLocalDB_JsonDeserialize_ERR");
                    File.Delete(fileName);
                }
            }

            if (allSource == null)
            {
                allSource = new List <T>();
            }

            AutoClock.Regist(() =>
            {
                try
                {
                    FileInfo dbFile = new FileInfo(fileName);
                    FileStream fs   = dbFile.Open(FileMode.Create, FileAccess.Write);
                    char[] r        = SerializerHelper.JsonSerializer(allSource).ToCharArray();
                    Encoder e       = Encoding.UTF8.GetEncoder();
                    byte[] byData   = new byte[r.Length];
                    e.GetBytes(r, 0, r.Length, byData, 0, true);
                    fs.Write(byData, 0, byData.Length);
                    fs.Flush();
                    fs.Close();
                }
                catch (Exception ex)
                {
                    LogHelper.CustomInfoEnabled = true;
                    LogHelper.CustomInfo(ex, "RegistLocalDB_AutoClock_ERR");
                }
            }, 3);
        }