溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Dictionary中怎么批量插入日志數(shù)據(jù)

發(fā)布時(shí)間:2021-08-06 16:06:04 來(lái)源:億速云 閱讀:145 作者:Leah 欄目:編程語(yǔ)言

本篇文章為大家展示了Dictionary中怎么批量插入日志數(shù)據(jù),內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

問(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<string, Tuple<ObjectInfo, object>> _storage = new Dictionary<string, Tuple<ObjectInfo, object>>(StringComparer.OrdinalIgnoreCase);

2、添加元素,操作的時(shí)候需要對(duì)其進(jìn)行線程安全處理,最簡(jiǎn)單的方式就是加鎖(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、定義一個(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<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();         }

上述內(nèi)容就是Dictionary中怎么批量插入日志數(shù)據(jù),你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI