国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

如何簡易的提高吞吐量

2019-11-14 13:51:11
字體:
供稿:網(wǎng)友

    性能提升還是那幾個要素,就像我在之前的博文里面提到的一樣,這一篇只是更加簡單一點而已。

    因為硬件配置是固定的,那我們只是簡單說一說在使用C#進行開發(fā)的項目中,如何使用一些簡單的小招數(shù)讓性能有一個比較大幅度的顯著提升。

    一、繞開那些煩人卻又不可避免的DB操作。

    DB操作是不可避免卻又是項目重中之中的所在,那我們可以做哪些優(yōu)化呢?

    首先,根據(jù)業(yè)務(wù)將DB操作分為需要即時和非即時的兩塊,關(guān)于非即時的我們可以將其單獨的交給某一個線程來單獨慢慢處理。代碼如下:

public class DbActionQueue : IDisposable    {        public Queue<Action> _transQueue;        PRivate Thread _thread;        private bool _isDispose = false;        private static readonly object _syncObject = new object();        private readonly object _syncQueueObject = new object();        private static DbActionQueue _instance;        public static DbActionQueue Instance        {            get            {                if (_instance == null)                {                    lock (_syncObject)                    {                        if (_instance == null)                        {                            _instance = new DbActionQueue();                        }                    }                }                return _instance;            }        }        private DbActionQueue()        {            if (_transQueue == null)            {                _transQueue = new Queue<Action>();            }            if (_thread == null)            {                _thread = new Thread(Thread_Work)                {                    IsBackground = true                };            }            _thread.Start();        }        public void Push(Action action)        {            if (_transQueue == null) throw new ArgumentNullException("dbActionQueue is not init");            lock (_syncQueueObject)            {                _transQueue.Enqueue(action);            }        }        public void Thread_Work()        {            while (!_isDispose)            {                Action[] items = null;                if (_transQueue != null && _transQueue.Count > 0)                {                    lock (_syncQueueObject)                    {                        items = new Action[_transQueue.Count];                        _transQueue.CopyTo(items, 0);                        _transQueue.Clear();                    }                }                if (items != null && items.Length > 0)                {                    foreach (var item in items)                    {                        try                        {                            item.Invoke();                        }                        catch (Exception ex)                        {                            LogHelper.Write(string.Format("DbActionQueue error. | Exception.StackTrace:{0}", ex.StackTrace), ex);                        }                    }                }                Thread.Sleep(1);            }        }        public void Dispose()        {            _isDispose = true;            _thread.Join();        }    }
View Code

    通過代碼可以看到,我們在實例化的時候,單獨創(chuàng)建了一個線程,用來做處理。

    那對于一些像日志之類的操作,則可以通過以下代碼進行操作:

DbActionQueue.Instance.Push(() =>{    LogBLL.Instance.Add(new Log                        {                            action_time = DateTime.Now;                        });});            
View Code

    到這里不難免要問了,如果數(shù)據(jù)量過大,單個隊列已經(jīng)無法滿足的時候,怎么做處理。關(guān)于隊列的監(jiān)控,優(yōu)化不在該文討論,通俗一點的做法可以引入一些第三方的隊列。另外在項目中,其實我們更多的時候,實際上并不是在Insert,Update,Delete等操作,而是Select的操作,那關(guān)于Select的一些緩存處理,也不在該文的討論范疇,因為關(guān)于Cache的各種中間件實在太多,而且篇幅太大。

    可能在某些時候,我們還是覺得單個線程處理太慢,希望多開幾個線程來處理對于DB的請求,則我們可以根據(jù)實際業(yè)務(wù)情況和機器配置,初始化任意個線程來處理,則以上代碼需要稍稍改裝一下,將單例的換成可自由實例化的,代碼如下:

 public class DbQueue    {        public Queue<Action> _transQueue;        private Thread _thread;        private bool _isDispose = false;        private readonly object _syncQueueObject = new object();        public DbQueue()        {            if (_transQueue == null)            {                _transQueue = new Queue<Action>();            }            if (_thread == null)            {                _thread = new Thread(Thread_Work)                {                    IsBackground = true                };            }            _thread.Start();        }        public void Thread_Work()        {            while (!_isDispose)            {                Action[] items = null;                if (_transQueue != null && _transQueue.Count > 0)                {                    lock (_syncQueueObject)                    {                        items = new Action[_transQueue.Count];                        _transQueue.CopyTo(items, 0);                        _transQueue.Clear();                    }                }                if (items != null && items.Length > 0)                {                    foreach (var item in items)                    {                        try                        {                            item.Invoke();                        }                        catch (Exception ex)                        {                            LogHelper.Write(string.Format("DbActionQueue error. | Exception.StackTrace:{0}", ex.StackTrace), ex);                        }                    }                }                Thread.Sleep(1);            }        }        public void Push(Action action)        {            if (_transQueue == null) throw new ArgumentNullException("dbActionQueue is not init");            lock (_syncQueueObject)            {                _transQueue.Enqueue(action);            }        }        public void Dispose()        {            _isDispose = true;            _thread.Join();        }    }
View Code

 那多個線程之間的處理,代碼則如下:

 public class DbQueueManage    {         private int _threadNumber = 2;        private DbQueue[] _dbQueues;        private Random random = new Random();        private DbQueueManage()        {                    }        static DbQueueManage()        {        }        private static readonly object _syncObject = new object();        private static DbQueueManage _instance;        public static DbQueueManage Instance        {            get            {                if (_instance == null)                {                    lock (_syncObject)                    {                        if (_instance == null)                        {                            _instance = new DbQueueManage();                        }                    }                }                return _instance;            }        }        public void Init(Action action, int threadNum = 2)        {            if (_dbQueues == null)            {                this._threadNumber = threadNum;                _dbQueues = new DbQueue[threadNum];                for (var i = 0; i < threadNum; i++)                {                    _dbQueues[i] = new DbQueue();                }            }        }        public void Push(Action action)        {            var index = GetRandomThreadIndex();            if (_dbQueues != null && _dbQueues.Length > index)            {                _dbQueues[index].Push(action);            }        }        public int GetRandomThreadIndex()        {            return random.Next(0, this._threadNumber);        }    }
View Code

另外關(guān)于為什么不選用Task來做處理,雖然Task關(guān)于線程的處理還是很優(yōu)秀的,這里請各位同僚自行定奪。關(guān)于即時性的DB操作,則可以以Redis,MongoDb,或者自行編寫的緩存來作為中間件,然后再講具體入庫的操作,放入之前編寫好的隊列處理里面。

    


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 抚宁县| 澄城县| 刚察县| 荥经县| 政和县| 澎湖县| 正宁县| 砚山县| 西乌珠穆沁旗| 南城县| 洛宁县| 德昌县| 台江县| 当雄县| 渝北区| 贞丰县| 徐汇区| 昌邑市| 喀喇沁旗| 高邑县| 资兴市| 股票| 南丰县| 太白县| 海门市| 仁化县| 英吉沙县| 绥芬河市| 固始县| 镇雄县| 名山县| 青河县| 万全县| 奇台县| 霍城县| 九江县| 佛学| 扬中市| 信阳市| 德保县| 邻水|