c# 用Dictionary实现日志数据批量插入
作者:UP技术控 时间:2022-05-29 02:01:45
背景
最近再做一个需求,就是对站点的一些事件进行埋点,说白了就是记录用户的访问行为。那么这些数据怎么保存呢,人家点一下保存一下?显然不合适,肯定是需要批量保存,提高效率。
问题窥探
首先,我想到的是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/p/14355164.html
标签:c#,Dictionary,日志,数据批量插入
0
投稿
猜你喜欢
详解Mybatis的二级缓存配置
2023-03-20 10:48:37
Java中this关键字的用法详解
2023-10-04 05:05:53
java搭建ftp/sftp进行数据传递的全过程
2023-11-29 15:08:10
详解二维码生成工厂
2022-09-26 11:26:13
Java基本数据类型(动力节点java学院整理)
2022-09-26 12:14:13
Android实现双层ViewPager嵌套
2021-12-23 02:35:52
详解Java线程堆栈
2021-06-29 03:37:38
Mybatis 复杂对象resultMap的使用
2023-10-12 22:56:44
详解java IO流之缓冲流的使用
2023-08-08 18:33:16
在Spring Boot中加载初始化数据的实现
2023-08-18 10:01:44
SpringMVC @NotNull校验不生效的解决方案
2021-07-19 23:20:07
深入浅析Java Object Serialization与 Hadoop 序列化
2023-07-30 17:02:16
Java方法的覆盖与隐藏的区别分析
2021-12-19 05:15:52
基于Java实现多线程下载并允许断点续传
2021-07-19 03:05:52
详解Java中的ForkJoin
2023-05-09 15:22:20
SprinBoot如何集成参数校验Validator及参数校验的高阶技巧
2023-10-23 16:24:31
Java 程序内部是如何执行的?
2022-04-29 20:18:36
Java中超详细this与super的概念和用法
2021-05-25 19:45:50
java中Executor,ExecutorService,ThreadPoolExecutor详解
2023-10-31 10:50:56
通过实例深入了解java序列化
2022-12-01 17:37:39