c#怎么用Dictionary實(shí)現(xiàn)日志數(shù)據(jù)批量插入-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)c#怎么用Dictionary實(shí)現(xiàn)日志數(shù)據(jù)批量插入,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、微信小程序定制開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了閻良免費(fèi)建站歡迎大家使用!

背景

最近再做一個(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<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)于“c#怎么用Dictionary實(shí)現(xiàn)日志數(shù)據(jù)批量插入”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

文章題目:c#怎么用Dictionary實(shí)現(xiàn)日志數(shù)據(jù)批量插入-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://www.muchs.cn/article36/depisg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站維護(hù)手機(jī)網(wǎng)站建設(shè)、標(biāo)簽優(yōu)化搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)