From 248c25b3a9023c13a9e52b4b03e1ef4805dd429c Mon Sep 17 00:00:00 2001 From: leiurayer <1432593898@qq.com> Date: Sun, 4 Aug 2024 11:02:10 +0800 Subject: [PATCH] =?UTF-8?q?Login=E6=95=B0=E6=8D=AE=E5=BA=93=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E5=8A=A0=E5=AF=86=E6=A8=A1=E5=BC=8F=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=8D=95=E4=BE=8B=E6=A8=A1=E5=BC=8F=E8=B0=83?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Database/Login/LoginDatabase.cs | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/Downkyi.Core/Database/Login/LoginDatabase.cs b/src/Downkyi.Core/Database/Login/LoginDatabase.cs index cd6ae64..e180869 100644 --- a/src/Downkyi.Core/Database/Login/LoginDatabase.cs +++ b/src/Downkyi.Core/Database/Login/LoginDatabase.cs @@ -1,31 +1,43 @@ -using Downkyi.Core.Storage; -using SQLite; +using SQLite; namespace Downkyi.Core.Database.Login; public class LoginDatabase { - private const SQLiteOpenFlags _flags = - SQLiteOpenFlags.ReadWrite | - SQLiteOpenFlags.Create | - SQLiteOpenFlags.SharedCache; - - private readonly string _databasePath = Constant.Login; + private readonly string _databasePath = Storage.StorageManager.GetLogin(); private SQLiteAsyncConnection? _database; + // 私有构造函数防止外部实例化 + private LoginDatabase() { } + + // 单例模式 + private static LoginDatabase? _instance; + private static readonly object _lock = new(); + public static LoginDatabase Instance() + { + // 双重检查锁定 + if (_instance == null) + { + lock (_lock) + { + _instance ??= new LoginDatabase(); + } + } + return _instance; + } + private async Task Init() { if (_database is not null) return; - _database = new SQLiteAsyncConnection(_databasePath, _flags); + var options = new SQLiteConnectionString(_databasePath, true, key: "Bu1rj3jc"); + _database = new SQLiteAsyncConnection(options); await _database.CreateTableAsync(); await _database.CreateTableAsync(); } - // 增删查改 - public async Task AddCookiesAsync(Cookies cookies) { await Init();