自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

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

[[379626]]

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

背景

最近再做一個(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。

  1. private readonly Dictionary<string, Tuple<ObjectInfo, object>> _storage = new Dictionary<string, Tuple<ObjectInfo, object>>(StringComparer.OrdinalIgnoreCase); 

2、添加元素,操作的時(shí)候需要對(duì)其進(jìn)行線程安全處理,最簡(jiǎn)單的方式就是加鎖(lock)。

  1. public bool SaveObject<T>(string path, T value) where T : class { 
  2.             if (String.IsNullOrWhiteSpace(path)) 
  3.                 throw new ArgumentNullException("path"); 
  4.  
  5.             lock (_lock) { 
  6.                 _storage[path] = Tuple.Create(new ObjectInfo { 
  7.                     Created = DateTime.Now, 
  8.                     Modified = DateTime.Now, 
  9.                     Path = path 
  10.                 }, (object)value); 
  11.  
  12.                 if (_storage.Count > MaxObjects) 
  13.                     _storage.Remove(_storage.OrderByDescending(kvp => kvp.Value.Item1.Created).First().Key); 
  14.             } 
  15.  
  16.             return true
  17.         } 

3、定義一個(gè)隊(duì)列,定時(shí)消費(fèi)日志。

 

  1. public DefaultEventQueue(ExceptionlessConfiguration config, IExceptionlessLog log, ISubmissionClient client, IObjectStorage objectStorage, IJsonSerializer serializer, TimeSpan? processQueueInterval, TimeSpan? queueStartDelay) { 
  2.             _log = log; 
  3.             _config = config; 
  4.             _client = client; 
  5.             _storage = objectStorage; 
  6.             _serializer = serializer; 
  7.             if (processQueueInterval.HasValue) 
  8.                 _processQueueInterval = processQueueInterval.Value; 
  9.  
  10.             _queueTimer = new Timer(OnProcessQueue, null, queueStartDelay ?? TimeSpan.FromSeconds(2), _processQueueInterval); 
  11.         } 

這里刪除的時(shí)候也需要lock 操作。

  1. public bool DeleteObject(string path) { 
  2.             if (String.IsNullOrWhiteSpace(path)) 
  3.                 throw new ArgumentNullException("path"); 
  4.  
  5.             lock (_lock) { 
  6.                 if (!_storage.ContainsKey(path)) 
  7.                     return false
  8.  
  9.                 _storage.Remove(path); 
  10.             } 
  11.  
  12.             return true
  13.         } 
  1. public IEnumerable<ObjectInfo> GetObjectList(string searchPattern = nullint? limit = null, DateTime? maxCreatedDate = null) { 
  2.             if (searchPattern == null
  3.                 searchPattern = "*"
  4.             if (!maxCreatedDate.HasValue) 
  5.                 maxCreatedDate = DateTime.MaxValue; 
  6.  
  7.             var regex = new Regex("^" + Regex.Escape(searchPattern).Replace("\\*"".*?") + "$"); 
  8.             lock (_lock) 
  9.                 return _storage.Keys.Where(k => regex.IsMatch(k)).Select(k => _storage[k].Item1).Where(f => f.Created <= maxCreatedDate).Take(limit ?? Int32.MaxValue).ToList(); 
  10.         } 

總結(jié)

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

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

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

 

責(zé)任編輯:武曉燕 來源: UP技術(shù)控
相關(guān)推薦

2024-10-22 08:47:03

2013-04-01 15:03:58

Android開發(fā)Android批量插入

2021-02-14 20:41:56

API日志web

2021-08-05 17:40:05

XpanesLinux服務(wù)器

2021-04-08 10:55:53

MySQL數(shù)據(jù)庫代碼

2011-08-04 18:00:47

SQLite數(shù)據(jù)庫批量數(shù)據(jù)

2021-09-27 07:56:41

MyBatis Plu數(shù)據(jù)庫批量插入

2010-09-03 11:47:38

SQL刪除

2010-11-04 14:50:52

DB2插入數(shù)據(jù)

2023-03-10 08:59:30

2010-09-01 16:26:11

SQL刪除批量

2022-09-29 10:06:56

SQLMySQL服務(wù)端

2024-07-31 09:56:20

2010-09-08 16:53:43

SQL查詢循環(huán)

2025-04-07 03:00:00

SpringBoot數(shù)據(jù)庫

2021-10-09 06:59:36

技術(shù)MyBatis數(shù)據(jù)

2024-08-26 08:27:18

2023-12-30 20:04:51

MyBatis框架數(shù)據(jù)

2009-07-20 17:03:55

批量插入數(shù)據(jù)ASP.NET

2020-11-23 10:50:27

MySQLSQL數(shù)據(jù)庫
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)