工業(yè)環(huán)境離線數(shù)據(jù)緩存解決方案:C# SQLite 實踐指南
作者:iamrick
你是不是也遇到過類似的問題?工業(yè)物聯(lián)網(wǎng)項目都面臨網(wǎng)絡(luò)不穩(wěn)定導(dǎo)致的數(shù)據(jù)丟失風(fēng)險。今天就來分享一套完整的C#離線數(shù)據(jù)緩存解決方案,讓你的工業(yè)應(yīng)用在斷網(wǎng)情況下也能穩(wěn)如泰山!
你是不是也遇到過類似的問題?工業(yè)物聯(lián)網(wǎng)項目都面臨網(wǎng)絡(luò)不穩(wěn)定導(dǎo)致的數(shù)據(jù)丟失風(fēng)險。今天就來分享一套完整的C#離線數(shù)據(jù)緩存解決方案,讓你的工業(yè)應(yīng)用在斷網(wǎng)情況下也能穩(wěn)如泰山!
問題分析:工業(yè)環(huán)境的數(shù)據(jù)挑戰(zhàn)
典型痛點
- 網(wǎng)絡(luò)不穩(wěn)定工廠環(huán)境信號差,經(jīng)常斷網(wǎng)
- 數(shù)據(jù)量大設(shè)備24小時采集,數(shù)據(jù)量驚人
- 實時性要求高生產(chǎn)數(shù)據(jù)不能丟失
- 存儲成本云端存儲費用昂貴
核心需求
- 離線時本地存儲數(shù)據(jù)
- 網(wǎng)絡(luò)恢復(fù)后自動同步
- 數(shù)據(jù)完整性保證
- 高性能讀寫操作
解決方案:SQLite + C# 完美組合
技術(shù)選型理由
為什么選擇SQLite?
- ? 零配置,單文件數(shù)據(jù)庫
- ? 高并發(fā)讀寫性能
- ? 事務(wù)支持,保證數(shù)據(jù)一致性
- ? 跨平臺,適合嵌入式環(huán)境
?? 核心實現(xiàn):分步驟詳解
步驟1:數(shù)據(jù)模型設(shè)計
// 工業(yè)數(shù)據(jù)模型 - 簡潔而全面
public class IndustrialDataModel
{
public int Id { get; set; } // 主鍵ID
public DateTime Timestamp { get; set; } // 采集時間戳
public string DeviceId { get; set; } // 設(shè)備編號
public double Temperature { get; set; } // 溫度值
public double Pressure { get; set; } // 壓力值
public bool IsSynced { get; set; } // 同步狀態(tài)標(biāo)記
}
設(shè)計亮點:
IsSynced
字段是關(guān)鍵,標(biāo)記數(shù)據(jù)是否已同步- 時間戳精確到毫秒,保證數(shù)據(jù)時序性
- 字段類型選擇兼顧性能和精度
步驟2:本地緩存?zhèn)}儲實現(xiàn)
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AppSqliteLocalCache
{
publicclass LocalCacheRepository
{
privatestring _connectionString;
public LocalCacheRepository(string dbPath)
{
// 構(gòu)造SQLite連接字符串
_connectionString = $"Data Source={dbPath};Version=3;";
InitializeDatabase();
}
/// <summary>
/// 初始化數(shù)據(jù)庫表結(jié)構(gòu) - 首次運行自動創(chuàng)建
/// </summary>
private void InitializeDatabase()
{
using (var connection = new SQLiteConnection(_connectionString))
{
connection.Open();
using (var command = new SQLiteCommand(connection))
{
// 創(chuàng)建工業(yè)數(shù)據(jù)表,支持自增主鍵
command.CommandText = @"
CREATE TABLE IF NOT EXISTS IndustrialData (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Timestamp DATETIME,
DeviceId TEXT,
Temperature REAL,
Pressure REAL,
IsSynced INTEGER
)";
command.ExecuteNonQuery();
}
}
}
/// <summary>
/// 插入采集數(shù)據(jù) - 高性能參數(shù)化查詢
/// </summary>
public void InsertData(IndustrialDataModel data)
{
using (var connection = new SQLiteConnection(_connectionString))
{
connection.Open();
using (var command = new SQLiteCommand(connection))
{
// 使用參數(shù)化查詢防止SQL注入
command.CommandText = @"
INSERT INTO IndustrialData
(Timestamp, DeviceId, Temperature, Pressure, IsSynced)
VALUES (@Timestamp, @DeviceId, @Temperature, @Pressure, @IsSynced)";
command.Parameters.AddWithValue("@Timestamp", data.Timestamp);
command.Parameters.AddWithValue("@DeviceId", data.DeviceId);
command.Parameters.AddWithValue("@Temperature", data.Temperature);
command.Parameters.AddWithValue("@Pressure", data.Pressure);
command.Parameters.AddWithValue("@IsSynced", data.IsSynced ? 1 : 0);
command.ExecuteNonQuery();
}
}
}
/// <summary>
/// 獲取未同步數(shù)據(jù) - 網(wǎng)絡(luò)恢復(fù)后批量上傳
/// </summary>
public List<IndustrialDataModel> GetUnsyncedData()
{
var unsyncedData = new List<IndustrialDataModel>();
using (var connection = new SQLiteConnection(_connectionString))
{
connection.Open();
using (var command = new SQLiteCommand("SELECT * FROM IndustrialData WHERE IsSynced = 0", connection))
{
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
// 安全的數(shù)據(jù)類型轉(zhuǎn)換
unsyncedData.Add(new IndustrialDataModel
{
Id = Convert.ToInt32(reader["Id"]),
Timestamp = Convert.ToDateTime(reader["Timestamp"]),
DeviceId = reader["DeviceId"].ToString(),
Temperature = Convert.ToDouble(reader["Temperature"]),
Pressure = Convert.ToDouble(reader["Pressure"]),
IsSynced = Convert.ToBoolean(reader["IsSynced"])
});
}
}
}
}
return unsyncedData;
}
/// <summary>
/// 批量標(biāo)記為已同步 - 事務(wù)保證數(shù)據(jù)一致性
/// </summary>
public void MarkAsSynced(List<int> ids)
{
using (var connection = new SQLiteConnection(_connectionString))
{
connection.Open();
// 使用事務(wù)確保批量操作的原子性
using (var transaction = connection.BeginTransaction())
{
foreach (var id in ids)
{
using (var command = new SQLiteCommand(connection))
{
command.CommandText = "UPDATE IndustrialData SET IsSynced = 1 WHERE Id = @Id";
command.Parameters.AddWithValue("@Id", id);
command.ExecuteNonQuery();
}
}
transaction.Commit(); // 批量提交,提高性能
}
}
}
}
}
步驟3:數(shù)據(jù)同步服務(wù)
我這里是用的http post,實際用mq效果會更好。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace AppSqliteLocalCache
{
publicclass DataSyncService
{
private readonly LocalCacheRepository _repository;
private readonly HttpClient _httpClient;
private readonly string _apiEndpoint;
public DataSyncService(LocalCacheRepository repository, string apiEndpoint)
{
_repository = repository;
_apiEndpoint = apiEndpoint;
_httpClient = new HttpClient();
}
/// <summary>
/// 智能同步:檢測網(wǎng)絡(luò)狀態(tài)并自動同步數(shù)據(jù)
/// </summary>
public async Task<bool> TrySyncData()
{
try
{
// 獲取待同步數(shù)據(jù)
var unsyncedData = _repository.GetUnsyncedData();
if (unsyncedData.Count == 0)
{
Console.WriteLine("? 無待同步數(shù)據(jù)");
returntrue;
}
// 分批上傳,避免單次請求過大
constint batchSize = 100;
var syncedIds = new List<int>();
for (int i = 0; i < unsyncedData.Count; i += batchSize)
{
var batch = unsyncedData.GetRange(i, Math.Min(batchSize, unsyncedData.Count - i));
if (await UploadBatch(batch))
{
// 記錄成功同步的ID
foreach (var item in batch)
{
syncedIds.Add(item.Id);
}
}
else
{
Console.WriteLine($"? 批次 {i / batchSize + 1} 同步失敗");
break;
}
}
// 更新同步狀態(tài)
if (syncedIds.Count > 0)
{
_repository.MarkAsSynced(syncedIds);
Console.WriteLine($"? 成功同步 {syncedIds.Count} 條數(shù)據(jù)");
}
return syncedIds.Count == unsyncedData.Count;
}
catch (Exception ex)
{
Console.WriteLine($"? 同步異常: {ex.Message}");
returnfalse;
}
}
/// <summary>
/// 批量上傳數(shù)據(jù)到云端API
/// </summary>
private async Task<bool> UploadBatch(List<IndustrialDataModel> batch)
{
try
{
var json = JsonSerializer.Serialize(batch);
var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync(_apiEndpoint, content);
return response.IsSuccessStatusCode;
}
catch
{
returnfalse; // 網(wǎng)絡(luò)異常返回失敗
}
}
public void Dispose()
{
_httpClient?.Dispose();
}
}
}
步驟4:完整應(yīng)用示例
using System.Text;
namespace AppSqliteLocalCache
{
internal class Program
{
static async Task Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
// 初始化本地緩存
var repository = new LocalCacheRepository("industrial_data.db");
var syncService = new DataSyncService(repository, "https://localhost:7284/api/Industrial");
Console.WriteLine("?? 工業(yè)數(shù)據(jù)采集系統(tǒng)啟動");
// 模擬數(shù)據(jù)采集和同步
var cts = new CancellationTokenSource();
// 啟動數(shù)據(jù)采集任務(wù)
var dataCollectionTask = StartDataCollection(repository, cts.Token);
// 啟動定時同步任務(wù)
var syncTask = StartPeriodicSync(syncService, cts.Token);
Console.WriteLine("按任意鍵停止系統(tǒng)...");
Console.ReadKey();
cts.Cancel();
await Task.WhenAll(dataCollectionTask, syncTask);
Console.WriteLine("?? 系統(tǒng)已停止");
}
/// <summary>
/// 模擬設(shè)備數(shù)據(jù)采集
/// </summary>
static async Task StartDataCollection(LocalCacheRepository repository, CancellationToken token)
{
var random = new Random();
while (!token.IsCancellationRequested)
{
try
{
// 模擬多設(shè)備數(shù)據(jù)采集
for (int deviceId = 1; deviceId <= 5; deviceId++)
{
var data = new IndustrialDataModel
{
Timestamp = DateTime.Now,
DeviceId = $"DEVICE_{deviceId:D3}",
Temperature = 20 + random.NextDouble() * 60, // 20-80°C
Pressure = 1 + random.NextDouble() * 9, // 1-10 bar
IsSynced = false
};
repository.InsertData(data);
}
Console.WriteLine($"?? {DateTime.Now:HH:mm:ss} 采集5個設(shè)備數(shù)據(jù)");
await Task.Delay(5000, token); // 每5秒采集一次
}
catch (OperationCanceledException)
{
break;
}
}
}
/// <summary>
/// 定時同步任務(wù)
/// </summary>
static async Task StartPeriodicSync(DataSyncService syncService, CancellationToken token)
{
while (!token.IsCancellationRequested)
{
try
{
await syncService.TrySyncData();
await Task.Delay(30000, token); // 每30秒嘗試同步
}
catch (OperationCanceledException)
{
break;
}
}
}
}
}
圖片
性能優(yōu)化技巧
數(shù)據(jù)庫優(yōu)化
// 1. 創(chuàng)建索引提升查詢性能
private void CreateIndexes()
{
using (var connection = new SQLiteConnection(_connectionString))
{
connection.Open();
using (var command = new SQLiteCommand(connection))
{
// 為常用查詢字段創(chuàng)建索引
command.CommandText = "CREATE INDEX IF NOT EXISTS idx_sync_status ON IndustrialData(IsSynced)";
command.ExecuteNonQuery();
command.CommandText = "CREATE INDEX IF NOT EXISTS idx_timestamp ON IndustrialData(Timestamp)";
command.ExecuteNonQuery();
}
}
}
// 2. 批量插入優(yōu)化
public void BatchInsert(List<IndustrialDataModel> dataList)
{
using (var connection = new SQLiteConnection(_connectionString))
{
connection.Open();
using (var transaction = connection.BeginTransaction())
{
using (var command = new SQLiteCommand(connection))
{
command.CommandText = @"
INSERT INTO IndustrialData
(Timestamp, DeviceId, Temperature, Pressure, IsSynced)
VALUES (@Timestamp, @DeviceId, @Temperature, @Pressure, @IsSynced)";
foreach (var data in dataList)
{
command.Parameters.Clear();
command.Parameters.AddWithValue("@Timestamp", data.Timestamp);
command.Parameters.AddWithValue("@DeviceId", data.DeviceId);
command.Parameters.AddWithValue("@Temperature", data.Temperature);
command.Parameters.AddWithValue("@Pressure", data.Pressure);
command.Parameters.AddWithValue("@IsSynced", data.IsSynced ? 1 : 0);
command.ExecuteNonQuery();
}
transaction.Commit(); // 批量提交大幅提升性能
}
}
}
}
生產(chǎn)環(huán)境注意事項
數(shù)據(jù)安全
- 定期備份設(shè)置自動備份機制
- 數(shù)據(jù)加密敏感數(shù)據(jù)需要加密存儲
- 訪問控制限制數(shù)據(jù)庫文件訪問權(quán)限
監(jiān)控告警
// 監(jiān)控數(shù)據(jù)庫大小,及時清理歷史數(shù)據(jù)
public long GetDatabaseSize(string dbPath)
{
var fileInfo = new FileInfo(dbPath);
return fileInfo.Length;
}
// 清理已同步的歷史數(shù)據(jù)
public void CleanupSyncedData(DateTime olderThan)
{
using (var connection = new SQLiteConnection(_connectionString))
{
connection.Open();
using (var command = new SQLiteCommand(connection))
{
command.CommandText = "DELETE FROM IndustrialData WHERE IsSynced = 1 AND Timestamp < @OlderThan";
command.Parameters.AddWithValue("@OlderThan", olderThan);
var deletedRows = command.ExecuteNonQuery();
Console.WriteLine($"??? 清理了 {deletedRows} 條歷史數(shù)據(jù)");
}
}
}
總結(jié):三個核心要點
- 架構(gòu)設(shè)計SQLite輕量級數(shù)據(jù)庫 + 參數(shù)化查詢 + 事務(wù)保證數(shù)據(jù)一致性
- 同步策略離線緩存 + 定時同步 + 分批上傳提升效率
- 性能優(yōu)化索引優(yōu)化 + 批量操作 + 定期清理保持高性能
這套解決方案已在多個工業(yè)項目中驗證,數(shù)據(jù)零丟失率達(dá)到99.9%以上!無論是制造業(yè)、能源行業(yè)還是智慧農(nóng)業(yè),都能輕松適配。
責(zé)任編輯:武曉燕
來源:
技術(shù)老小子