新聞中心
本文轉(zhuǎn)載自微信公眾號(hào)「UP技術(shù)控」,作者conan5566 。轉(zhuǎn)載本文請(qǐng)聯(lián)系UP技術(shù)控公眾號(hào)。

創(chuàng)新互聯(lián)建站基于成都重慶香港及美國(guó)等地區(qū)分布式IDC機(jī)房數(shù)據(jù)中心構(gòu)建的電信大帶寬,聯(lián)通大帶寬,移動(dòng)大帶寬,多線BGP大帶寬租用,是為眾多客戶提供專業(yè)服務(wù)器托管報(bào)價(jià),主機(jī)托管價(jià)格性價(jià)比高,為金融證券行業(yè)服務(wù)器托管,ai人工智能服務(wù)器托管提供bgp線路100M獨(dú)享,G口帶寬及機(jī)柜租用的專業(yè)成都idc公司。
背景
最近再做一個(gè)需求,就是對(duì)站點(diǎn)的一些事件進(jìn)行埋點(diǎn),說(shuō)白了就是記錄用戶的訪問(wèn)行為。那么這些數(shù)據(jù)怎么保存呢,人家點(diǎn)一下保存一下?顯然不合適,肯定是需要批量保存,提高效率。
問(wèn)題窺探
首先,我想到的是Dictionary,對(duì)于C#中的Dictionary類相信大家都不陌生,這是一個(gè)Collection(集合)類型,可以通過(guò)Key/Value(鍵值對(duì)的形式來(lái)存放數(shù)據(jù);該類最大的優(yōu)點(diǎn)就是它查找元素的時(shí)間復(fù)雜度接近O(1),實(shí)際項(xiàng)目中常被用來(lái)做一些數(shù)據(jù)的本地緩存,提升整體效率。Dictionary是非線程安全的類型,可以實(shí)現(xiàn)先添加到內(nèi)存當(dāng)中,在批量保存進(jìn)去數(shù)據(jù)庫(kù)。
主要代碼實(shí)現(xiàn)
1、定義一個(gè)Dictionary。
- private readonly Dictionary
> _storage = new Dictionary >(StringComparer.OrdinalIgnoreCase);
2、添加元素,操作的時(shí)候需要對(duì)其進(jìn)行線程安全處理,最簡(jiǎn)單的方式就是加鎖(lock)。
- public bool SaveObject
(string path, T value) where T : class { - if (String.IsNullOrWhiteSpace(path))
- throw new ArgumentNullException("path");
- lock (_lock) {
- _storage[path] = Tuple.Create(new ObjectInfo {
- Created = DateTime.Now,
- Modified = DateTime.Now,
- Path = path
- }, (object)value);
- if (_storage.Count > MaxObjects)
- _storage.Remove(_storage.OrderByDescending(kvp => kvp.Value.Item1.Created).First().Key);
- }
- return true;
- }
3、定義一個(gè)隊(duì)列,定時(shí)消費(fèi)日志。
- public DefaultEventQueue(ExceptionlessConfiguration config, IExceptionlessLog log, ISubmissionClient client, IObjectStorage objectStorage, IJsonSerializer serializer, TimeSpan? processQueueInterval, TimeSpan? queueStartDelay) {
- _log = log;
- _config = config;
- _client = client;
- _storage = objectStorage;
- _serializer = serializer;
- if (processQueueInterval.HasValue)
- _processQueueInterval = processQueueInterval.Value;
- _queueTimer = new Timer(OnProcessQueue, null, queueStartDelay ?? TimeSpan.FromSeconds(2), _processQueueInterval);
- }
這里刪除的時(shí)候也需要lock 操作。
- public bool DeleteObject(string path) {
- if (String.IsNullOrWhiteSpace(path))
- throw new ArgumentNullException("path");
- lock (_lock) {
- if (!_storage.ContainsKey(path))
- return false;
- _storage.Remove(path);
- }
- return true;
- }
- public IEnumerable
GetObjectList(string searchPattern = null, int? limit = null, DateTime? maxCreatedDate = null) { - if (searchPattern == null)
- searchPattern = "*";
- if (!maxCreatedDate.HasValue)
- maxCreatedDate = DateTime.MaxValue;
- var regex = new Regex("^" + Regex.Escape(searchPattern).Replace("\\*", ".*?") + "$");
- lock (_lock)
- return _storage.Keys.Where(k => regex.IsMatch(k)).Select(k => _storage[k].Item1).Where(f => f.Created <= maxCreatedDate).Take(limit ?? Int32.MaxValue).ToList();
- }
總結(jié)
1、利用Dictionary。多線程添加數(shù)據(jù)到內(nèi)存;
2、達(dá)到一定量的時(shí)候,批量保存數(shù)據(jù)。
3、使用lock ,保證Dictionary操作安全。
網(wǎng)站標(biāo)題:巧用Dictionary實(shí)現(xiàn)日志數(shù)據(jù)批量插入
分享地址:http://www.5511xx.com/article/djjecdp.html


咨詢
建站咨詢
