c# 用Dictionary實現(xiàn)日志數(shù)據(jù)批量插入
背景
最近再做一個需求,就是對站點的一些事件進行埋點,說白了就是記錄用戶的訪問行為。那么這些數(shù)據(jù)怎么保存呢,人家點一下保存一下?顯然不合適,肯定是需要批量保存,提高效率。
問題窺探
首先,我想到的是Dictionary,對于C#中的Dictionary類相信大家都不陌生,這是一個Collection(集合)類型,可以通過Key/Value(鍵值對的形式來存放數(shù)據(jù);該類最大的優(yōu)點就是它查找元素的時間復(fù)雜度接近O(1),實際項目中常被用來做一些數(shù)據(jù)的本地緩存,提升整體效率。Dictionary是非線程安全的類型,可以實現(xiàn)先添加到內(nèi)存當中,在批量保存進去數(shù)據(jù)庫。
主要代碼實現(xiàn)
1、定義一個Dictionary。
private readonly Dictionary<string, Tuple<ObjectInfo, object>> _storage = new Dictionary<string, Tuple<ObjectInfo, object>>(StringComparer.OrdinalIgnoreCase);
2、添加元素,操作的時候需要對其進行線程安全處理,最簡單的方式就是加鎖(lock)。
public bool SaveObject<T>(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、定義一個隊列,定時消費日志。
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); }
這里刪除的時候也需要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<ObjectInfo> 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、達到一定量的時候,批量保存數(shù)據(jù)。
3、使用lock ,保證Dictionary操作安全。
以上就是c# 用Dictionary實現(xiàn)日志數(shù)據(jù)批量插入的詳細內(nèi)容,更多關(guān)于Dictionary實現(xiàn)日志數(shù)據(jù)批量插入的資料請關(guān)注腳本之家其它相關(guān)文章!
- C# ArrayList、HashSet、HashTable、List、Dictionary的區(qū)別詳解
- C#中數(shù)組、ArrayList、List、Dictionary的用法與區(qū)別淺析(存取數(shù)據(jù))
- C#數(shù)組中List, Dictionary的相互轉(zhuǎn)換問題
- C#常見的幾種集合 ArrayList,Hashtable,List<T>,Dictionary<K,V> 遍歷方法對比
- C#并發(fā)容器之ConcurrentDictionary與普通Dictionary帶鎖性能詳解
- C# 解決在Dictionary中使用枚舉的效率問題
- C#字典Dictionary的用法說明(注重性能版)
- C#中Dictionary<TKey,TValue>排序方式的實現(xiàn)
- 聊聊C# 中HashTable與Dictionary的區(qū)別說明
- c# List和Dictionary常用的操作
相關(guān)文章
C# SerialPort實現(xiàn)串口通訊的代碼詳解
在.NET平臺下創(chuàng)建C#串口通信程序,.NET 2.0提供了串口通信的功能,其命名空間是System.IO.Ports,這個新的框架不但可以訪問計算機上的串口,還可以和串口設(shè)備進行通信,本文給大家介紹了C# SerialPort實現(xiàn)串口通訊,需要的朋友可以參考下2024-06-06C#使用RabbitMQ發(fā)送和接收消息工具類的實現(xiàn)
RabbitMQ是一個消息的代理器,用于接收和發(fā)送消息,本文主要介紹了C#使用RabbitMQ發(fā)送和接收消息工具類的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2023-12-12C# Stream 和 byte[] 之間的轉(zhuǎn)換
Stream 和 byte[] 之間的轉(zhuǎn)換2008-03-03C#調(diào)用C++DLL傳遞結(jié)構(gòu)體數(shù)組的終極解決方案
這篇文章主要介紹了C#調(diào)用C++DLL傳遞結(jié)構(gòu)體數(shù)組的終極解決方案的相關(guān)資料,需要的朋友可以參考下2017-01-01implicit關(guān)鍵字做自定義類型隱式轉(zhuǎn)換的方法
implicit 關(guān)鍵字用于聲明隱式的用戶定義類型轉(zhuǎn)換運算符。如果轉(zhuǎn)換過程可以確保不會造成數(shù)據(jù)丟失,則可使用該關(guān)鍵字在用戶定義類型和其他類型之間進行隱式轉(zhuǎn)換,這篇文章就給大家詳細介紹implicit關(guān)鍵字做自定義類型隱式轉(zhuǎn)換的方法,需要的朋友可以參考下2015-08-08