Business description
When the user completes the business operation or data operation, the business record / data tracking is inserted into Redis. ThreadPool.QueueUserWorkItem periodically checks the queue and inserts the above data into the database for persistence.
Implementation process
1. Implementation of RedisHelp
/// <summary> /// Redison Helper class /// </summary> public class RedisCacheHelper { private static IDatabase databse { get { return ConnectionMultiplexer.Connect("127.0.0.1").GetDatabase(); } } /// <summary> /// Business log team entry /// </summary> /// <param name="mess"></param> public static void EnqueueOperate(string mess) { databse.ListRightPush("OperateQueue", mess); } /// <summary> /// Business log team out /// </summary> /// <param name="mess"></param> public static string DequeueOperate() { return databse.ListRightPop("OperateQueue").ToString(); } /// <summary> ///Data tracking team entry /// </summary> /// <param name="mess"></param> public static void EnqueueDataTrack(string mess) { databse.ListRightPush("DataTrackQueue", mess); } /// <summary> /// Data tracking out of the team /// </summary> /// <param name="mess"></param> public static string DequeueDataTrack() { return databse.ListRightPop("DataTrackQueue").ToString(); } }
Among them, redundancy is implemented by StackExchange.Redis. It is mainly about the out of line and in line operation of the list object.
Then there is ThreadPool.QueueUserWorkItem scheduled to leave the team
public static class OperationLogConfig { public static void OperationLogStart() { ThreadPool.QueueUserWorkItem(x => { while (true) { try { var Operate = RedisCacheHelper.DequeueOperate(); var DataTrack = RedisCacheHelper.DequeueDataTrack(); if (!string.IsNullOrEmpty(Operate)) { //do something } if (!string.IsNullOrEmpty(DataTrack)) { //do something } if (string.IsNullOrEmpty(Operate) && string.IsNullOrEmpty(DataTrack)) { Thread.Sleep(60000); } } catch (Exception ex) { Thread.Sleep(60000); } } }); } }
It should be noted that unlike asp.net, the call of OperationLogConfig.OperationLogStart(); must be in front of the Run of the Main function.