最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

巧用Dictionary實(shí)現(xiàn)日志數(shù)據(jù)批量插入

 更新時(shí)間:2021年02月01日 11:26:32   作者:UP技術(shù)控  
這篇文章主要介紹了巧用Dictionary實(shí)現(xiàn)日志數(shù)據(jù)批量插入,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

背景

最近再做一個(gè)需求,就是對(duì)站點(diǎn)的一些事件進(jìn)行埋點(diǎn),說白了就是記錄用戶的訪問行為。那么這些數(shù)據(jù)怎么保存呢,人家點(diǎn)一下保存一下?顯然不合適,肯定是需要批量保存,提高效率。

問題窺探

首先,我想到的是Dictionary,對(duì)于C#中的Dictionary類相信大家都不陌生,這是一個(gè)Collection(集合)類型,可以通過Key/Value(鍵值對(duì)的形式來存放數(shù)據(jù);該類最大的優(yōu)點(diǎn)就是它查找元素的時(shí)間復(fù)雜度接近O(1),實(shí)際項(xiàng)目中常被用來做一些數(shù)據(jù)的本地緩存,提升整體效率。Dictionary是非線程安全的類型,可以實(shí)現(xiàn)先添加到內(nèi)存當(dāng)中,在批量保存進(jìn)去數(shù)據(jù)庫。

主要代碼實(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();
    }

總結(jié)

1、利用Dictionary。多線程添加數(shù)據(jù)到內(nèi)存;

2、達(dá)到一定量的時(shí)候,批量保存數(shù)據(jù)。

3、使用lock ,保證Dictionary操作安全。

到此這篇關(guān)于巧用Dictionary實(shí)現(xiàn)日志數(shù)據(jù)批量插入的文章就介紹到這了,更多相關(guān)Dictionary實(shí)現(xiàn)日志數(shù)據(jù)批量插入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 基于C#實(shí)現(xiàn)的多邊形沖突檢測(cè)實(shí)例

    基于C#實(shí)現(xiàn)的多邊形沖突檢測(cè)實(shí)例

    這篇文章主要給大家介紹了基于C#實(shí)現(xiàn)的多邊形沖突檢測(cè)的相關(guān)資料,文中介紹的方法并未使用第三方類庫,可以完美解決這個(gè)問題,需要的朋友可以參考下
    2021-07-07
  • C# pictureBox用法案例詳解

    C# pictureBox用法案例詳解

    這篇文章主要介紹了C# pictureBox用法案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • C# 使用modbus 讀取PLC 寄存器地址的方法

    C# 使用modbus 讀取PLC 寄存器地址的方法

    今天通過本文給大家介紹C# 使用modbus 讀取PLC 寄存器地址的方法,使用的組件Nmodbus,文中通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2021-10-10
  • 最新評(píng)論

    淳化县| 图木舒克市| 鹤峰县| 蓝田县| 郯城县| 盐城市| 岱山县| 集安市| 高台县| 浠水县| 东台市| 于田县| 安西县| 永川市| 墨脱县| 巴彦县| 正蓝旗| 双桥区| 基隆市| 东阿县| 佛坪县| 白朗县| 昂仁县| 太保市| 秦安县| 依兰县| 大渡口区| 恩施市| 瑞金市| 阿拉善右旗| 嵊泗县| 大渡口区| 班戈县| 罗甸县| 万宁市| 乐山市| 游戏| 金秀| 海城市| 太保市| 灵武市|