You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
3.0 KiB
112 lines
3.0 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Sog.Data
|
|
{
|
|
/// <summary>
|
|
/// 需要保存数据库的数据基础类,采用数据序列号保证数据保存成功
|
|
/// </summary>
|
|
public class DataSeqSave
|
|
{
|
|
/// <summary>
|
|
/// 数据版本
|
|
/// </summary>
|
|
public uint DataSeq { get; private set; }
|
|
|
|
/// <summary>
|
|
/// 成功保存数据库的版本
|
|
/// </summary>
|
|
public uint SavedSuccessSeq { get; set; }
|
|
|
|
/// <summary>
|
|
/// 最后一次发送保存请求的时间
|
|
/// </summary>
|
|
public long LastSaveDBReqTime { get; private set; }
|
|
|
|
/// <summary>
|
|
/// 最后一次发送保存请求的数据版本
|
|
/// </summary>
|
|
public uint LastSaveDBReqDataSeq { get; private set; }
|
|
|
|
|
|
/// <summary>
|
|
/// 数据频繁变化的的时候2此保存请求最小时间间隔
|
|
/// </summary>
|
|
public int SaveReqMinInterval { get; set; } = 5000;
|
|
|
|
/// <summary>
|
|
/// 发送请求后,等待db响应的超时时间,毫秒
|
|
/// </summary>
|
|
public int SaveReqWaitAckTimeout { get; set; } = 180000;
|
|
|
|
/// <summary>
|
|
/// 是否数据脏,脏了需要保存
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool IsDirty()
|
|
{
|
|
return SavedSuccessSeq != DataSeq;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 现在可不可以执行保存操作,如果已经发送了当前数据的保存请求,则需要等待WaitSaveReqTimeout,这个不能太短,否则容易造成mysql雪崩
|
|
/// 如果发送请求后数据马上又有变化,那至少也得等待个5秒钟,太频繁了也没必要
|
|
/// </summary>
|
|
/// <param name="nowMs"></param>
|
|
/// <returns></returns>
|
|
public bool CanDoSaveReqNow(long nowMs)
|
|
{
|
|
if(!IsDirty())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if(LastSaveDBReqDataSeq == DataSeq)
|
|
{
|
|
if (nowMs - LastSaveDBReqTime < SaveReqWaitAckTimeout)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (nowMs - LastSaveDBReqTime < SaveReqMinInterval)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 数据置脏,DataSeq++
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public uint MakeDirty()
|
|
{
|
|
DataSeq++;
|
|
|
|
return DataSeq;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发送保存请求的时候调用,设置一下保存时间
|
|
/// </summary>
|
|
/// <param name="nowMs"></param>
|
|
public void OnSaveReq(long nowMs)
|
|
{
|
|
LastSaveDBReqTime = nowMs;
|
|
LastSaveDBReqDataSeq = DataSeq;
|
|
}
|
|
|
|
// 设置数据序列号
|
|
public void SetDataSeq(uint seq)
|
|
{
|
|
DataSeq = seq;
|
|
}
|
|
}
|
|
}
|
|
|