巧用Dictionary实现日志数据批量插入
作者:UP技术控 时间:2022-03-10 12:31:05
背景
最近再做一个需求,就是对站点的一些事件进行埋点,说白了就是记录用户的访问行为。那么这些数据怎么保存呢,人家点一下保存一下?显然不合适,肯定是需要批量保存,提高效率。
问题窥探
首先,我想到的是Dictionary,对于C#中的Dictionary类相信大家都不陌生,这是一个Collection(集合)类型,可以通过Key/Value(键值对的形式来存放数据;该类最大的优点就是它查找元素的时间复杂度接近O(1),实际项目中常被用来做一些数据的本地缓存,提升整体效率。Dictionary是非线程安全的类型,可以实现先添加到内存当中,在批量保存进去数据库。
主要代码实现
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();
}
总结
1、利用Dictionary。多线程添加数据到内存;
2、达到一定量的时候,批量保存数据。
3、使用lock ,保证Dictionary操作安全。
来源:https://www.cnblogs.com/lyl6796910/archive/2021/02/01/14355164.html
标签:Dictionary,日志数据,批量插入
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java运算符从见过到掌握下
2023-01-29 15:47:19
![](https://img.aspxhome.com/file/2023/5/102655_0s.png)
使用JSON.toJSONString格式化成json字符串时保留null属性
2023-05-10 14:56:34
dubbo服务链路跟踪方式
2023-08-24 09:54:21
![](https://img.aspxhome.com/file/2023/6/58296_0s.png)
c# 可选参数、命名参数
2022-08-06 05:38:41
Android Studio实现带边框的圆形头像
2023-11-30 01:52:23
![](https://img.aspxhome.com/file/2023/6/92576_0s.png)
解析HikariCP一百行代码轻松掌握多线程
2023-03-21 19:11:00
用C#来解析PDF文件
2023-11-02 08:07:07
![](https://img.aspxhome.com/file/2023/8/82238_0s.png)
Spring activiti如何实现指定任务处理者
2023-04-08 05:53:16
Spring代理对象导致的获取不到原生对象注解的解决
2021-12-05 11:44:19
Android缓存机制——LruCache的详解
2023-07-30 07:26:34
C#基于Socket实现简单聊天室功能
2023-05-09 01:17:32
详解Spring Boot集成MyBatis(注解方式)
2023-10-03 17:45:47
![](https://img.aspxhome.com/file/2023/1/64481_0s.jpg)
C# List集合中获取重复值及集合运算详解
2022-06-13 17:15:45
C#基础知识之GetType与typeof的区别小结
2022-04-06 21:59:34
![](https://img.aspxhome.com/file/2023/0/96330_0s.png)
使用反射方式获取JPA Entity的属性和值
2023-07-24 17:43:22
C#键值对容器的介绍
2023-04-14 12:26:56
详解C#编程获取资源文件中图片的方法
2023-06-17 23:58:51
Springboot使用filter对response内容进行加密方式
2023-11-17 15:29:25
![](https://img.aspxhome.com/file/2023/5/90955_0s.png)
Android实现显示和隐藏密码功能的示例代码
2022-12-14 23:40:09
![](https://img.aspxhome.com/file/2023/0/101870_0s.png)
SpringBoot深入探究@Conditional条件装配的使用
2021-08-18 00:06:53
![](https://img.aspxhome.com/file/2023/2/61792_0s.png)