From d98c65817b09548bf122167b333b2ee6c9abb504 Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Sat, 18 Jan 2025 02:50:02 +0800 Subject: [PATCH 01/16] docs: update release notes --- TYLDDB/TYLDDB.csproj | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/TYLDDB/TYLDDB.csproj b/TYLDDB/TYLDDB.csproj index d78d8fa..3f4fb4c 100644 --- a/TYLDDB/TYLDDB.csproj +++ b/TYLDDB/TYLDDB.csproj @@ -4,14 +4,14 @@ netstandard2.1;net6.0;net7.0;net8.0;net9.0;net6.0-windows;net7.0-windows;net8.0-windows;net9.0-windows True TYLDDB.NET - 1.0.0-alpha.4pre2 + 1.0.0-alpha.4 TYLDDB-Project LICENSE True icon.png https://github.com/TYLDDB/TYLDDB-CSharp git - ttypdb.qingyi-studio.top + https://tylddb.qingyi-studio.top BSD 3-Clause License (Modified) A strongly typed constrained distributed file system that can act as a database. True @@ -19,9 +19,7 @@ DataBase QingYi-Studio README.md - - Optimize error classes: Add more refined error handling and throwing. -- Allows you to customize the buffer size when reading files in blocks. -- Add a three-value dictionary (under test). + - Apply a three-value dictionary. From 8d686b6b0661ecd05840a2b643501327b5b2e03f Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Sat, 18 Jan 2025 03:27:57 +0800 Subject: [PATCH 02/16] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E4=B8=89?= =?UTF-8?q?=E5=80=BC=E8=AF=8D=E5=85=B8=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TDCache/ITripleDictionaryCache.cs | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 TYLDDB/Utils/FastCache/TDCache/ITripleDictionaryCache.cs diff --git a/TYLDDB/Utils/FastCache/TDCache/ITripleDictionaryCache.cs b/TYLDDB/Utils/FastCache/TDCache/ITripleDictionaryCache.cs new file mode 100644 index 0000000..22b5f59 --- /dev/null +++ b/TYLDDB/Utils/FastCache/TDCache/ITripleDictionaryCache.cs @@ -0,0 +1,45 @@ +#if NET8_0_OR_GREATER +namespace TYLDDB.Utils.FastCache.TDCache +{ + /// + /// Three-value dictionary interface.
+ /// 三值字典的接口。 + ///
+ public interface ITripleDictionaryCache + { + /// + /// Set a cache entry for a specified key.
+ /// 为指定键设置缓存项。 + ///
+ /// + /// Key
键 + /// Value
值 + /// Whether the operation is successful.
操作是否成功。
+ public abstract bool? Set(string type, string key, object value); + + /// + /// Synchronization method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Data type.
数据类型。 + /// Key
键 + /// Value
+ public abstract bool? Get(string type, string key); + + /// + /// Remove a cache entry by its key.
+ /// 根据键移除缓存项。 + ///
+ /// + /// Key
键 + /// Whether the removal is successful.
移除操作是否成功。
+ public abstract bool? Remove(string type, string key); + + /// + /// Clear all cache entries.
+ /// 清空所有缓存项。 + ///
+ public abstract void Clear(); + } +} +#endif \ No newline at end of file From 4e1e2c148b52f13fecf3be12797cda999b0e8fef Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Sat, 18 Jan 2025 03:41:58 +0800 Subject: [PATCH 03/16] =?UTF-8?q?docs:=20=E8=A1=A5=E5=85=A8=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E6=8E=A5=E5=8F=A3=E4=B8=AD=E6=96=87=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TYLDDB/Utils/Database/IDatabase.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/TYLDDB/Utils/Database/IDatabase.cs b/TYLDDB/Utils/Database/IDatabase.cs index c154648..baef727 100644 --- a/TYLDDB/Utils/Database/IDatabase.cs +++ b/TYLDDB/Utils/Database/IDatabase.cs @@ -3,7 +3,8 @@ namespace TYLDDB.Utils.Database { /// - /// Interfaces for operations on external databases. + /// Interfaces for operations on external databases.
+ /// 对外部数据库进行操作的接口。 ///
public interface IDatabase { From bb47e4c1327551ec05dad08bd20c4464ced24be1 Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Sat, 18 Jan 2025 03:42:43 +0800 Subject: [PATCH 04/16] =?UTF-8?q?style:=20=E4=BD=BF=E7=94=A8=E8=A1=A8?= =?UTF-8?q?=E8=BE=BE=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TYLDDB/Utils/Database/V2.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/TYLDDB/Utils/Database/V2.cs b/TYLDDB/Utils/Database/V2.cs index 4ef461d..9a0e66d 100644 --- a/TYLDDB/Utils/Database/V2.cs +++ b/TYLDDB/Utils/Database/V2.cs @@ -48,9 +48,6 @@ public string GetDatabaseContent(string input, string databaseName) return result.ToString().TrimEnd(); // 移除结尾的多余换行符 } - public List GetDatabaseList(string fileContent) - { - throw new System.NotImplementedException(); - } + public List GetDatabaseList(string fileContent) => throw new NotImplementedException(); } } From 7ff1600ed7e61b922458390726c290661e40bd32 Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Sat, 18 Jan 2025 03:47:38 +0800 Subject: [PATCH 05/16] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E5=AE=9E=E4=BE=8B=E5=8C=96=E7=9A=84=E6=9E=9A=E4=B8=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TYLDDB/TYLDDB_Enum.cs | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 TYLDDB/TYLDDB_Enum.cs diff --git a/TYLDDB/TYLDDB_Enum.cs b/TYLDDB/TYLDDB_Enum.cs new file mode 100644 index 0000000..3e9d630 --- /dev/null +++ b/TYLDDB/TYLDDB_Enum.cs @@ -0,0 +1,51 @@ +namespace TYLDDB +{ + /// + /// Cache mode.
+ /// 缓存模式。 + ///
+ public enum CacheMode + { + /// + /// Use concurrent dictionaries to achieve high concurrency stability.
+ /// 使用并发词典来实现高并发的稳定性。 + ///
+ ConcurrentDictionary, + + /// + /// Use semaphore based thread locks to achieve high concurrency stability.
+ /// 使用基于信号量的线程锁来实现高并发的稳定性。 + ///
+ SemaphoreThreadLock, + + /// + /// High concurrency stability is achieved by using concurrent dictionary as core and semaphore based thread lock.
+ /// 以并发词典为核,结合基于信号量的线程锁实现高并发的稳定性。 + ///
+ TripleDictionaryCache, + + /// + /// + + /// + CDAndSTL, + } + + /// + /// Database pre-parsing process.
+ /// 数据库预解析流程。 + ///
+ public enum DatabaseProcess + { + /// + /// Based on regular expressions.
+ /// 基于正则表达式。 + ///
+ V1, + + /// + /// Parse database content directly based on string segmentation.
+ /// 直接基于字符串分割解析数据库内容。 + ///
+ V2, + } +} From e4285f9b29a0ccfa36dbfaa660f850e3c7e4d98d Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Sat, 18 Jan 2025 13:54:07 +0800 Subject: [PATCH 06/16] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E6=9B=B4?= =?UTF-8?q?=E5=A4=9A=E5=AE=9E=E4=BE=8B=E5=8C=96=E9=87=8D=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TYLDDB/TYLDDB_Instantiation.cs | 184 +++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+) diff --git a/TYLDDB/TYLDDB_Instantiation.cs b/TYLDDB/TYLDDB_Instantiation.cs index 8b6abb1..efd2cd0 100644 --- a/TYLDDB/TYLDDB_Instantiation.cs +++ b/TYLDDB/TYLDDB_Instantiation.cs @@ -41,5 +41,189 @@ public LDDB() stlBooleanDictionary = new StlBooleanDictionary(); #endregion } + + /// + /// Instantiate the LDDB class
+ /// 实例化LDDB类 + ///
+ public LDDB(CacheMode mode) + { + #region 实例化数据库操作类 + database_v1 = new Database_V1(); + database_v2 = new Database_V2(); + #endregion + + switch (mode) + { + case CacheMode.ConcurrentDictionary: + #region 实例化并发词典 + cdStringDictionary = new CdStringDictionary(); + cdShortDictionary = new CdShortDictionary(); + cdLongDictionary = new CdLongDictionary(); + cdIntegerDictionary = new CdIntegerDictionary(); + cdFloatDictionary = new CdFloatDictionary(); + cdDoubleDictionary = new CdDoubleDictionary(); + cdDecimalDictionary = new CdDecimalDictionary(); + cdCharDictionary = new CdCharDictionary(); + cdBooleanDictionary = new CdBooleanDictionary(); + #endregion + break; + case CacheMode.SemaphoreThreadLock: + #region 实例化信号词典 + stlStringDictionary = new StlStringDictionary(); + stlShortDictionary = new StlShortDictionary(); + stlLongDictionary = new StlLongDictionary(); + stlIntegerDictionary = new StlIntegerDictionary(); + stlFloatDictionary = new StlFloatDictionary(); + stlDoubleDictionary = new StlDoubleDictionary(); + stlDecimalDictionary = new StlDecimalDictionary(); + stlCharDictionary = new StlCharDictionary(); + stlBooleanDictionary = new StlBooleanDictionary(); + #endregion + break; + case CacheMode.CDAndSTL: + #region 实例化并发词典 + cdStringDictionary = new CdStringDictionary(); + cdShortDictionary = new CdShortDictionary(); + cdLongDictionary = new CdLongDictionary(); + cdIntegerDictionary = new CdIntegerDictionary(); + cdFloatDictionary = new CdFloatDictionary(); + cdDoubleDictionary = new CdDoubleDictionary(); + cdDecimalDictionary = new CdDecimalDictionary(); + cdCharDictionary = new CdCharDictionary(); + cdBooleanDictionary = new CdBooleanDictionary(); + #endregion + + #region 实例化信号词典 + stlStringDictionary = new StlStringDictionary(); + stlShortDictionary = new StlShortDictionary(); + stlLongDictionary = new StlLongDictionary(); + stlIntegerDictionary = new StlIntegerDictionary(); + stlFloatDictionary = new StlFloatDictionary(); + stlDoubleDictionary = new StlDoubleDictionary(); + stlDecimalDictionary = new StlDecimalDictionary(); + stlCharDictionary = new StlCharDictionary(); + stlBooleanDictionary = new StlBooleanDictionary(); + #endregion + break; + case CacheMode.TripleDictionaryCache: + break; + } + } + + /// + /// Instantiate the LDDB class
+ /// 实例化LDDB类 + ///
+ public LDDB(DatabaseProcess process) + { + #region 实例化并发词典 + cdStringDictionary = new CdStringDictionary(); + cdShortDictionary = new CdShortDictionary(); + cdLongDictionary = new CdLongDictionary(); + cdIntegerDictionary = new CdIntegerDictionary(); + cdFloatDictionary = new CdFloatDictionary(); + cdDoubleDictionary = new CdDoubleDictionary(); + cdDecimalDictionary = new CdDecimalDictionary(); + cdCharDictionary = new CdCharDictionary(); + cdBooleanDictionary = new CdBooleanDictionary(); + #endregion + + #region 实例化信号词典 + stlStringDictionary = new StlStringDictionary(); + stlShortDictionary = new StlShortDictionary(); + stlLongDictionary = new StlLongDictionary(); + stlIntegerDictionary = new StlIntegerDictionary(); + stlFloatDictionary = new StlFloatDictionary(); + stlDoubleDictionary = new StlDoubleDictionary(); + stlDecimalDictionary = new StlDecimalDictionary(); + stlCharDictionary = new StlCharDictionary(); + stlBooleanDictionary = new StlBooleanDictionary(); + #endregion + + switch (process) + { + case DatabaseProcess.V1: + database_v1 = new Database_V1(); + break; + case DatabaseProcess.V2: + database_v2 = new Database_V2(); + break; + } + } + + /// + /// Instantiate the LDDB class
+ /// 实例化LDDB类 + ///
+ public LDDB(CacheMode mode, DatabaseProcess process) + { + switch (mode) + { + case CacheMode.ConcurrentDictionary: + #region 实例化并发词典 + cdStringDictionary = new CdStringDictionary(); + cdShortDictionary = new CdShortDictionary(); + cdLongDictionary = new CdLongDictionary(); + cdIntegerDictionary = new CdIntegerDictionary(); + cdFloatDictionary = new CdFloatDictionary(); + cdDoubleDictionary = new CdDoubleDictionary(); + cdDecimalDictionary = new CdDecimalDictionary(); + cdCharDictionary = new CdCharDictionary(); + cdBooleanDictionary = new CdBooleanDictionary(); + #endregion + break; + case CacheMode.SemaphoreThreadLock: + #region 实例化信号词典 + stlStringDictionary = new StlStringDictionary(); + stlShortDictionary = new StlShortDictionary(); + stlLongDictionary = new StlLongDictionary(); + stlIntegerDictionary = new StlIntegerDictionary(); + stlFloatDictionary = new StlFloatDictionary(); + stlDoubleDictionary = new StlDoubleDictionary(); + stlDecimalDictionary = new StlDecimalDictionary(); + stlCharDictionary = new StlCharDictionary(); + stlBooleanDictionary = new StlBooleanDictionary(); + #endregion + break; + case CacheMode.CDAndSTL: + #region 实例化并发词典 + cdStringDictionary = new CdStringDictionary(); + cdShortDictionary = new CdShortDictionary(); + cdLongDictionary = new CdLongDictionary(); + cdIntegerDictionary = new CdIntegerDictionary(); + cdFloatDictionary = new CdFloatDictionary(); + cdDoubleDictionary = new CdDoubleDictionary(); + cdDecimalDictionary = new CdDecimalDictionary(); + cdCharDictionary = new CdCharDictionary(); + cdBooleanDictionary = new CdBooleanDictionary(); + #endregion + + #region 实例化信号词典 + stlStringDictionary = new StlStringDictionary(); + stlShortDictionary = new StlShortDictionary(); + stlLongDictionary = new StlLongDictionary(); + stlIntegerDictionary = new StlIntegerDictionary(); + stlFloatDictionary = new StlFloatDictionary(); + stlDoubleDictionary = new StlDoubleDictionary(); + stlDecimalDictionary = new StlDecimalDictionary(); + stlCharDictionary = new StlCharDictionary(); + stlBooleanDictionary = new StlBooleanDictionary(); + #endregion + break; + case CacheMode.TripleDictionaryCache: + break; + } + + switch (process) + { + case DatabaseProcess.V1: + database_v1 = new Database_V1(); + break; + case DatabaseProcess.V2: + database_v2 = new Database_V2(); + break; + } + } } } From 2942a2d0e6dc5b8fac999c677237f59a1ca7b51e Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Sat, 18 Jan 2025 14:05:06 +0800 Subject: [PATCH 07/16] =?UTF-8?q?feat:=20=E5=AD=97=E5=85=B8=E5=88=9D?= =?UTF-8?q?=E6=AD=A5=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TDCache/ITripleDictionaryCache.cs | 6 +- .../TDCache/TripleDictionaryCache.cs | 67 +++++++++++++++++++ 2 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 TYLDDB/Utils/FastCache/TDCache/TripleDictionaryCache.cs diff --git a/TYLDDB/Utils/FastCache/TDCache/ITripleDictionaryCache.cs b/TYLDDB/Utils/FastCache/TDCache/ITripleDictionaryCache.cs index 22b5f59..b9aad8b 100644 --- a/TYLDDB/Utils/FastCache/TDCache/ITripleDictionaryCache.cs +++ b/TYLDDB/Utils/FastCache/TDCache/ITripleDictionaryCache.cs @@ -15,7 +15,7 @@ public interface ITripleDictionaryCache /// Key
键 /// Value
值 /// Whether the operation is successful.
操作是否成功。
- public abstract bool? Set(string type, string key, object value); + public abstract bool Set(string type, string key, object value); /// /// Synchronization method: Obtain the corresponding value by key.
@@ -24,7 +24,7 @@ public interface ITripleDictionaryCache /// Data type.
数据类型。 /// Key
键 /// Value
- public abstract bool? Get(string type, string key); + public abstract object Get(string type, string key); /// /// Remove a cache entry by its key.
@@ -33,7 +33,7 @@ public interface ITripleDictionaryCache /// /// Key
键 /// Whether the removal is successful.
移除操作是否成功。
- public abstract bool? Remove(string type, string key); + public abstract bool Remove(string type, string key); /// /// Clear all cache entries.
diff --git a/TYLDDB/Utils/FastCache/TDCache/TripleDictionaryCache.cs b/TYLDDB/Utils/FastCache/TDCache/TripleDictionaryCache.cs new file mode 100644 index 0000000..689a05e --- /dev/null +++ b/TYLDDB/Utils/FastCache/TDCache/TripleDictionaryCache.cs @@ -0,0 +1,67 @@ +#if NET8_0_OR_GREATER +using System; +using System.Threading; +using TYLDDB.Basic; + +namespace TYLDDB.Utils.FastCache.TDCache +{ + public class TripleDictionaryCache : ITripleDictionaryCache, IDisposable + { + private TripleDictionary dictionary; + private SemaphoreSlim semaphore; // 控制并发访问 + + public TripleDictionaryCache() + { + dictionary = new TripleDictionary(); + semaphore = new SemaphoreSlim(1, 1); // 使用信号量来同步 + } + + /// + /// Call directly.
+ /// 直接调用。 + ///
+ public void Dispose() + { + Clear(); + } + + /// + /// Clear all cache entries.
+ /// 清空所有缓存项。 + ///
+ public void Clear() + { + dictionary = new TripleDictionary(); + semaphore = null; + } + + /// + /// Synchronization method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Data type.
数据类型。 + /// Key
键 + /// Value
+ public object Get(string type, string key) => dictionary.Get(type, key); + + /// + /// Remove a cache entry by its key.
+ /// 根据键移除缓存项。 + ///
+ /// Data type.
数据类型。 + /// Key
键 + /// Whether the removal is successful.
移除操作是否成功。
+ public bool Remove(string type, string key) => dictionary.RemoveKey(type, key); + + /// + /// Set a cache entry for a specified key.
+ /// 为指定键设置缓存项。 + ///
+ /// Data type.
数据类型。 + /// Key
键 + /// Value
值 + /// Whether the operation is successful.
操作是否成功。
+ public bool Set(string type, string key, object value) => dictionary.Add(type, key, value); + } +} +#endif \ No newline at end of file From 06377eb5f2bb561b08e610019de3025542798001 Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Sat, 18 Jan 2025 14:12:20 +0800 Subject: [PATCH 08/16] =?UTF-8?q?test:=20=E4=BC=98=E5=8C=96=E6=97=A7?= =?UTF-8?q?=E7=9A=84=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TYLDDB.Test/Program.cs | 202 +++++++++++++++++++++-------------------- 1 file changed, 104 insertions(+), 98 deletions(-) diff --git a/TYLDDB.Test/Program.cs b/TYLDDB.Test/Program.cs index 5f4d0b1..dbbe139 100644 --- a/TYLDDB.Test/Program.cs +++ b/TYLDDB.Test/Program.cs @@ -9,117 +9,123 @@ var lddb = new LDDB(); #endregion -#region 读取文件 -///////////////////////////////////////////////////////////////////////////////////////////////////////// 读取文件 -HighPrecisionTimer readDbTimer = new(); // 从发起读取文件到成功读取的总时间 -lddb.FilePath = dbFilePath; -readDbTimer.Start(); -lddb.ReadingFile(); -readDbTimer.Stop(); -WriteTime("从发起读取文件指令到成功读取的总时间为: ", readDbTimer.ElapsedMilliseconds()); -#endregion - -#region 读取数据库 -///////////////////////////////////////////////////////////////////////////////////////////////////////// 读取数据库 -HighPrecisionTimer loadDbTimer = new(); // 从发起读取数据库到成功返回读取内容的总时间 -loadDbTimer.Start(); -lddb.LoadDatabase("database1"); -Console.WriteLine(lddb.GetLoadingDatabaseContent()); // 输出database1内容 -loadDbTimer.Stop(); -WriteTime("从发起读取数据库指令到成功返回读取内容的总时间为(V1): ", loadDbTimer.ElapsedMilliseconds()); - -HighPrecisionTimer loadDbV2Timer = new(); // 从发起读取数据库到成功返回读取内容的总时间 -loadDbV2Timer.Start(); -lddb.LoadDatabase_V2("database1"); -Console.WriteLine(lddb.GetLoadingDatabaseContent()); // 输出database1内容 -loadDbV2Timer.Stop(); -WriteTime("从发起读取数据库指令到成功返回读取内容的总时间为(V2): ", loadDbV2Timer.ElapsedMilliseconds()); -#endregion - -#region 获取所有数据库名称 -///////////////////////////////////////////////////////////////////////////////////////////////////////// 获取所有数据库名称 -HighPrecisionTimer readAllDbNameTimer = new(); // 从发起读取数据库名称到成功返回读取内容的总时间 -readAllDbNameTimer.Start(); -lddb.ReadAllDatabaseName(); -readAllDbNameTimer.Stop(); -if(lddb.AllDatabaseName != null) -{ - foreach (var dbName in lddb.AllDatabaseName) - { - Console.WriteLine(dbName); - } -} -WriteTime("从发起读取数据库名称到成功返回读取内容的总时间为: ", readAllDbNameTimer.ElapsedMilliseconds()); -#endregion - -///////////////////////////////////////////////////////////////////////////////////////////////////////// 数据库解析缓存 -HighPrecisionTimer parseDbTimer = new(); -parseDbTimer.Start(); -await lddb.Parse_V1(); -parseDbTimer.Stop(); -WriteTime("解析文件并写入缓存V1(同步): ", parseDbTimer.ElapsedMilliseconds()); -HighPrecisionTimer parseDbTimerAsync = new(); -parseDbTimerAsync.Start(); -await lddb.ParseAsync_V1(); -parseDbTimerAsync.Stop(); -WriteTime("解析文件并写入缓存V1(异步): ", parseDbTimerAsync.ElapsedMilliseconds()); - -//HighPrecisionTimer parseDbV2Timer = new(); -//parseDbV2Timer.Start(); -//lddb.Parse_V2(); -//parseDbV2Timer.Stop(); -//WriteTime("解析文件并写入缓存V1(同步): ", parseDbV2Timer.ElapsedMilliseconds()); - -///////////////////////////////////////////////////////////////////////////////////////////////////////// 并发词典数据库全类型同步搜寻 -HighPrecisionTimer allTypeSearchFromConcurrentDictionaryTimer = new(); -allTypeSearchFromConcurrentDictionaryTimer.Start(); -string[] AllTypeSearchFromConcurrentDictionaryResult = lddb.AllTypeSearchFromConcurrentDictionary("str_name"); -allTypeSearchFromConcurrentDictionaryTimer.Stop(); -// 使用 foreach 输出数组的每个元素 -foreach (var str in AllTypeSearchFromConcurrentDictionaryResult) -{ - Console.WriteLine(str); -} -WriteTime("并发词典数据库全类型同步搜寻: ", allTypeSearchFromConcurrentDictionaryTimer.ElapsedMilliseconds()); - -///////////////////////////////////////////////////////////////////////////////////////////////////////// 信号量线程锁词典数据库全类型同步搜寻 -HighPrecisionTimer allTypeSearchFromSemaphoreThreadLockTimer = new(); -allTypeSearchFromSemaphoreThreadLockTimer.Start(); -string[] AllTypeSearchFromSemaphoreThreadLockResult = lddb.AllTypeSearchFromSemaphoreThreadLock("str_name"); -allTypeSearchFromSemaphoreThreadLockTimer.Stop(); -// 使用 foreach 输出数组的每个元素 -foreach (var str in AllTypeSearchFromSemaphoreThreadLockResult) -{ - Console.WriteLine(str); -} -WriteTime("信号量线程锁词典数据库全类型同步搜寻: ", allTypeSearchFromSemaphoreThreadLockTimer.ElapsedMilliseconds()); - - - - - - - - - - - +ReadFile(); +ReadDatabase(); +GetAllDatabaseName(); +ParseDatabaseToTemp(); +CdAllTypeSearch(); +StlAllTypeSearch(); +ExportTestData(); +Console.ReadLine(); +#region Test Method +void ReadFile() +{ + #region 读取文件 + ///////////////////////////////////////////////////////////////////////////////////////////////////////// 读取文件 + HighPrecisionTimer readDbTimer = new(); // 从发起读取文件到成功读取的总时间 + lddb.FilePath = dbFilePath; + readDbTimer.Start(); + lddb.ReadingFile(); + readDbTimer.Stop(); + WriteTime("从发起读取文件指令到成功读取的总时间为: ", readDbTimer.ElapsedMilliseconds()); + #endregion +} +void ReadDatabase() +{ + #region 读取数据库 + ///////////////////////////////////////////////////////////////////////////////////////////////////////// 读取数据库 + HighPrecisionTimer loadDbTimer = new(); // 从发起读取数据库到成功返回读取内容的总时间 + loadDbTimer.Start(); + lddb.LoadDatabase("database1"); + Console.WriteLine(lddb.GetLoadingDatabaseContent()); // 输出database1内容 + loadDbTimer.Stop(); + WriteTime("从发起读取数据库指令到成功返回读取内容的总时间为(V1): ", loadDbTimer.ElapsedMilliseconds()); + + HighPrecisionTimer loadDbV2Timer = new(); // 从发起读取数据库到成功返回读取内容的总时间 + loadDbV2Timer.Start(); + lddb.LoadDatabase_V2("database1"); + Console.WriteLine(lddb.GetLoadingDatabaseContent()); // 输出database1内容 + loadDbV2Timer.Stop(); + WriteTime("从发起读取数据库指令到成功返回读取内容的总时间为(V2): ", loadDbV2Timer.ElapsedMilliseconds()); + #endregion +} +void GetAllDatabaseName() +{ + #region 获取所有数据库名称 + ///////////////////////////////////////////////////////////////////////////////////////////////////////// 获取所有数据库名称 + HighPrecisionTimer readAllDbNameTimer = new(); // 从发起读取数据库名称到成功返回读取内容的总时间 + readAllDbNameTimer.Start(); + lddb.ReadAllDatabaseName(); + readAllDbNameTimer.Stop(); + if (lddb.AllDatabaseName != null) + { + foreach (var dbName in lddb.AllDatabaseName) + { + Console.WriteLine(dbName); + } + } + WriteTime("从发起读取数据库名称到成功返回读取内容的总时间为: ", readAllDbNameTimer.ElapsedMilliseconds()); + #endregion +} +async void ParseDatabaseToTemp() +{ + #region 数据库解析缓存 + HighPrecisionTimer parseDbTimer = new(); + parseDbTimer.Start(); + await lddb.Parse_V1(); + parseDbTimer.Stop(); + WriteTime("解析文件并写入缓存V1(同步): ", parseDbTimer.ElapsedMilliseconds()); + HighPrecisionTimer parseDbTimerAsync = new(); + parseDbTimerAsync.Start(); + await lddb.ParseAsync_V1(); + parseDbTimerAsync.Stop(); + WriteTime("解析文件并写入缓存V1(异步): ", parseDbTimerAsync.ElapsedMilliseconds()); + #endregion +} -ExportTestData(); -Console.ReadLine(); +void CdAllTypeSearch() +{ + #region 并发词典数据库全类型搜寻 + HighPrecisionTimer allTypeSearchFromConcurrentDictionaryTimer = new(); + allTypeSearchFromConcurrentDictionaryTimer.Start(); + string[] AllTypeSearchFromConcurrentDictionaryResult = lddb.AllTypeSearchFromConcurrentDictionary("str_name"); + allTypeSearchFromConcurrentDictionaryTimer.Stop(); + // 使用 foreach 输出数组的每个元素 + foreach (var str in AllTypeSearchFromConcurrentDictionaryResult) + { + Console.WriteLine(str); + } + WriteTime("并发词典数据库全类型同步搜寻: ", allTypeSearchFromConcurrentDictionaryTimer.ElapsedMilliseconds()); + #endregion +} -////////////////////////////////////////////////////////////////////////////////////////////////////////// Test Method +void StlAllTypeSearch() +{ + #region 信号量线程锁词典数据库全类型搜寻 + HighPrecisionTimer allTypeSearchFromSemaphoreThreadLockTimer = new(); + allTypeSearchFromSemaphoreThreadLockTimer.Start(); + string[] AllTypeSearchFromSemaphoreThreadLockResult = lddb.AllTypeSearchFromSemaphoreThreadLock("str_name"); + allTypeSearchFromSemaphoreThreadLockTimer.Stop(); + // 使用 foreach 输出数组的每个元素 + foreach (var str in AllTypeSearchFromSemaphoreThreadLockResult) + { + Console.WriteLine(str); + } + WriteTime("信号量线程锁词典数据库全类型搜寻: ", allTypeSearchFromSemaphoreThreadLockTimer.ElapsedMilliseconds()); + #endregion +} +#endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////// 工具 void WriteTime(string what, double time) From a950fa7601970f3f1c8c85c24380183223a8c719 Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Sat, 18 Jan 2025 15:22:10 +0800 Subject: [PATCH 09/16] =?UTF-8?q?feat:=20=E6=8F=90=E4=BE=9B=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E7=B1=BB=E7=9A=84=E4=B8=89=E5=80=BC=E5=AD=97?= =?UTF-8?q?=E5=85=B8=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TYLDDB.Test/Program.cs | 11 ++ TYLDDB/TYLDDB_Instantiation.cs | 18 ++++ TYLDDB/TYLDDB_Parse.cs | 184 ++++++++++++++++++++++++++++++++- TYLDDB/TYLDDB_PrivateField.cs | 14 ++- 4 files changed, 223 insertions(+), 4 deletions(-) diff --git a/TYLDDB.Test/Program.cs b/TYLDDB.Test/Program.cs index dbbe139..65ef162 100644 --- a/TYLDDB.Test/Program.cs +++ b/TYLDDB.Test/Program.cs @@ -21,6 +21,8 @@ StlAllTypeSearch(); +TripleDictionaryTest(); + ExportTestData(); Console.ReadLine(); @@ -125,6 +127,15 @@ void StlAllTypeSearch() WriteTime("信号量线程锁词典数据库全类型搜寻: ", allTypeSearchFromSemaphoreThreadLockTimer.ElapsedMilliseconds()); #endregion } + +void TripleDictionaryTest() +{ + HighPrecisionTimer parse = new(); + parse.Start(); + lddb.TripleDictParse(); + parse.Stop(); + WriteTime("三值字典解析并写入(同步): ", parse.ElapsedMilliseconds()); +} #endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////// 工具 diff --git a/TYLDDB/TYLDDB_Instantiation.cs b/TYLDDB/TYLDDB_Instantiation.cs index efd2cd0..53ac7a5 100644 --- a/TYLDDB/TYLDDB_Instantiation.cs +++ b/TYLDDB/TYLDDB_Instantiation.cs @@ -2,6 +2,10 @@ using TYLDDB.Utils.FastCache.ConcurrentDictionary; using TYLDDB.Utils.FastCache.SemaphoreThreadLock; +#if NET8_0_OR_GREATER +using TYLDDB.Utils.FastCache.TDCache; +#endif + namespace TYLDDB { public partial class LDDB @@ -40,6 +44,10 @@ public LDDB() stlCharDictionary = new StlCharDictionary(); stlBooleanDictionary = new StlBooleanDictionary(); #endregion + +#if NET8_0_OR_GREATER + tripleDictionaryCache = new TripleDictionaryCache(); +#endif } /// @@ -107,6 +115,9 @@ public LDDB(CacheMode mode) #endregion break; case CacheMode.TripleDictionaryCache: +#if NET8_0_OR_GREATER + tripleDictionaryCache = new TripleDictionaryCache(); +#endif break; } } @@ -141,6 +152,10 @@ public LDDB(DatabaseProcess process) stlBooleanDictionary = new StlBooleanDictionary(); #endregion +#if NET8_0_OR_GREATER + tripleDictionaryCache = new TripleDictionaryCache(); +#endif + switch (process) { case DatabaseProcess.V1: @@ -212,6 +227,9 @@ public LDDB(CacheMode mode, DatabaseProcess process) #endregion break; case CacheMode.TripleDictionaryCache: +#if NET8_0_OR_GREATER + tripleDictionaryCache = new TripleDictionaryCache(); +#endif break; } diff --git a/TYLDDB/TYLDDB_Parse.cs b/TYLDDB/TYLDDB_Parse.cs index 876e2c6..0429dc8 100644 --- a/TYLDDB/TYLDDB_Parse.cs +++ b/TYLDDB/TYLDDB_Parse.cs @@ -1,6 +1,10 @@ using System.Threading.Tasks; using TYLDDB.Parser; +#if NET8_0_OR_GREATER +using System.Threading; +#endif + namespace TYLDDB { public partial class LDDB @@ -57,7 +61,7 @@ await Task.WhenAll(cdStringCacheTask, stlCharCacheTask, stlBoolCacheTask); - // ConcurrentDictionary + #region ConcurrentDictionary void CdString() { var dict = DataParser_V1.ParseString(_databaseContent); @@ -184,6 +188,7 @@ void CdBool() cdBooleanDictionary.Set(key, value); } } + #endregion // SemaphoreThreadLock void StlString() @@ -620,5 +625,182 @@ async void StlBool() } } } + +#if NET8_0_OR_GREATER + // 三值字典操作 + /// + /// Parses the database and writes to the three-value dictionary cache.
+ /// 解析数据库并写入三值字典缓存。 + ///
+ public void TripleDictParse() + { + #region 各数据类型字典 + var _str = DataParser_V1.ParseString(_databaseContent); + var _short = DataParser_V1.ParseShort(_databaseContent); + var _long = DataParser_V1.ParseLong(_databaseContent); + var _int = DataParser_V1.ParseInt(_databaseContent); + var _float = DataParser_V1.ParseFloat(_databaseContent); + var _double = DataParser_V1.ParseDouble(_databaseContent); + var _decimal = DataParser_V1.ParseDecimal(_databaseContent); + var _char = DataParser_V1.ParseChar(_databaseContent); + var _bool = DataParser_V1.ParseBoolean(_databaseContent); + #endregion + + #region 方法 + void Str() + { + // 遍历 dict 中的每一项 + foreach (var kvp in _str) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + tripleDictionaryCache.Set("string", key, value); + } + } + + void Short() + { + // 遍历 dict 中的每一项 + foreach (var kvp in _short) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + tripleDictionaryCache.Set("short", key, value); + } + } + + void Long() + { + // 遍历 dict 中的每一项 + foreach (var kvp in _long) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + tripleDictionaryCache.Set("long", key, value); + } + } + + void Int() + { + // 遍历 dict 中的每一项 + foreach (var kvp in _int) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + tripleDictionaryCache.Set("int", key, value); + } + } + + void Float() + { + // 遍历 dict 中的每一项 + foreach (var kvp in _float) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + tripleDictionaryCache.Set("float", key, value); + } + } + + void Double() + { + // 遍历 dict 中的每一项 + foreach (var kvp in _double) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + tripleDictionaryCache.Set("double", key, value); + } + } + + void Decimal() + { + // 遍历 dict 中的每一项 + foreach (var kvp in _decimal) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + tripleDictionaryCache.Set("decimal", key, value); + } + } + + void Char() + { + // 遍历 dict 中的每一项 + foreach (var kvp in _char) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + tripleDictionaryCache.Set("char", key, value); + } + } + + void Bool() + { + // 遍历 dict 中的每一项 + foreach (var kvp in _bool) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + tripleDictionaryCache.Set("bool", key, value); + } + } + #endregion + + #region 线程 + #region 创建线程实例 + Thread strThread = new(Str); + Thread shortThread = new(Short); + Thread longThread = new(Long); + Thread floatThread = new(Float); + Thread doubleThread = new(Double); + Thread decimalThread = new(Decimal); + Thread charThread = new(Char); + Thread intThread = new(Int); + Thread boolThread = new(Bool); + #endregion + #region 启动线程 + strThread.Start(); + shortThread.Start(); + longThread.Start(); + floatThread.Start(); + doubleThread.Start(); + decimalThread.Start(); + charThread.Start(); + intThread.Start(); + boolThread.Start(); + #endregion + #region 等待线程执行完毕 + strThread.Join(); + shortThread.Join(); + longThread.Join(); + floatThread.Join(); + doubleThread.Join(); + decimalThread.Join(); + charThread.Join(); + intThread.Join(); + boolThread.Join(); + #endregion + #endregion + } +#endif } } diff --git a/TYLDDB/TYLDDB_PrivateField.cs b/TYLDDB/TYLDDB_PrivateField.cs index 52254b2..babf4b3 100644 --- a/TYLDDB/TYLDDB_PrivateField.cs +++ b/TYLDDB/TYLDDB_PrivateField.cs @@ -2,6 +2,10 @@ using TYLDDB.Utils.FastCache.ConcurrentDictionary; using TYLDDB.Utils.FastCache.SemaphoreThreadLock; +#if NET8_0_OR_GREATER +using TYLDDB.Utils.FastCache.TDCache; +#endif + namespace TYLDDB { public partial class LDDB @@ -10,8 +14,8 @@ public partial class LDDB private string _filePath; // 存储文件路径 private string _fileContent; // 存储文件内容 private string _databaseContent; // 存储数据库内容 - private Database_V1 database_v1; - private Database_V2 database_v2; + private readonly Database_V1 database_v1; + private readonly Database_V2 database_v2; private CdStringDictionary cdStringDictionary; private CdShortDictionary cdShortDictionary; private CdLongDictionary cdLongDictionary; @@ -30,6 +34,10 @@ public partial class LDDB private StlDecimalDictionary stlDecimalDictionary; private StlCharDictionary stlCharDictionary; private StlBooleanDictionary stlBooleanDictionary; - #endregion + +#if NET8_0_OR_GREATER + private readonly TripleDictionaryCache tripleDictionaryCache; +#endif +#endregion } } From c4459b9ad7bd5ddf4a7a0d6f8a3b46f2e85c0eb7 Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Sat, 18 Jan 2025 15:45:54 +0800 Subject: [PATCH 10/16] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96V1=E8=A7=A3?= =?UTF-8?q?=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 速度提升约15% --- TYLDDB/TYLDDB.csproj | 3 +- TYLDDB/TYLDDB_Parse.cs | 236 +++++++++-------------------------------- 2 files changed, 51 insertions(+), 188 deletions(-) diff --git a/TYLDDB/TYLDDB.csproj b/TYLDDB/TYLDDB.csproj index 3f4fb4c..af2e0b4 100644 --- a/TYLDDB/TYLDDB.csproj +++ b/TYLDDB/TYLDDB.csproj @@ -19,7 +19,8 @@ DataBase QingYi-Studio README.md - - Apply a three-value dictionary. + · Apply a three-value dictionary. +· diff --git a/TYLDDB/TYLDDB_Parse.cs b/TYLDDB/TYLDDB_Parse.cs index 0429dc8..32b1700 100644 --- a/TYLDDB/TYLDDB_Parse.cs +++ b/TYLDDB/TYLDDB_Parse.cs @@ -1,9 +1,6 @@ using System.Threading.Tasks; using TYLDDB.Parser; - -#if NET8_0_OR_GREATER using System.Threading; -#endif namespace TYLDDB { @@ -13,56 +10,10 @@ public partial class LDDB /// Reparse the entire database.
/// 重新解析整个数据库。 ///
- public async Task Parse_V1() + public void Parse_V1() { - // 创建多个任务,并使用 LongRunning 来确保每个任务在独立线程中运行 - - #region 并发数据库任务 - Task cdStringCacheTask = Task.Factory.StartNew(() => CdString(), TaskCreationOptions.LongRunning); - Task cdIntCacheTask = Task.Factory.StartNew(() => CdInt(), TaskCreationOptions.LongRunning); - Task cdShortCacheTask = Task.Factory.StartNew(() => CdShort(), TaskCreationOptions.LongRunning); - Task cdLongCacheTask = Task.Factory.StartNew(() => CdLong(), TaskCreationOptions.LongRunning); - Task cdFloatCacheTask = Task.Factory.StartNew(() => CdFloat(), TaskCreationOptions.LongRunning); - Task cdDoubleCacheTask = Task.Factory.StartNew(() => CdDouble(), TaskCreationOptions.LongRunning); - Task cdDecimalCacheTask = Task.Factory.StartNew(() => CdDecimal(), TaskCreationOptions.LongRunning); - Task cdCharCacheTask = Task.Factory.StartNew(() => CdChar(), TaskCreationOptions.LongRunning); - Task cdBoolCacheTask = Task.Factory.StartNew(() => CdBool(), TaskCreationOptions.LongRunning); - #endregion - - #region 信号量数据库任务 - Task stlStringCacheTask = Task.Factory.StartNew(() => StlString(), TaskCreationOptions.LongRunning); - Task stlIntCacheTask = Task.Factory.StartNew(() => StlInt(), TaskCreationOptions.LongRunning); - Task stlShortCacheTask = Task.Factory.StartNew(() => StlShort(), TaskCreationOptions.LongRunning); - Task stlLongCacheTask = Task.Factory.StartNew(() => StlLong(), TaskCreationOptions.LongRunning); - Task stlFloatCacheTask = Task.Factory.StartNew(() => StlFloat(), TaskCreationOptions.LongRunning); - Task stlDoubleCacheTask = Task.Factory.StartNew(() => StlDouble(), TaskCreationOptions.LongRunning); - Task stlDecimalCacheTask = Task.Factory.StartNew(() => StlDecimal(), TaskCreationOptions.LongRunning); - Task stlCharCacheTask = Task.Factory.StartNew(() => StlChar(), TaskCreationOptions.LongRunning); - Task stlBoolCacheTask = Task.Factory.StartNew(() => StlBool(), TaskCreationOptions.LongRunning); - #endregion - - // 等待所有任务完成 - await Task.WhenAll(cdStringCacheTask, - cdIntCacheTask, - cdShortCacheTask, - cdLongCacheTask, - cdFloatCacheTask, - cdDoubleCacheTask, - cdDecimalCacheTask, - cdCharCacheTask, - cdBoolCacheTask, - stlStringCacheTask, - stlIntCacheTask, - stlShortCacheTask, - stlLongCacheTask, - stlFloatCacheTask, - stlDoubleCacheTask, - stlDecimalCacheTask, - stlCharCacheTask, - stlBoolCacheTask); - - #region ConcurrentDictionary - void CdString() + #region method + void String() { var dict = DataParser_V1.ParseString(_databaseContent); @@ -73,10 +24,11 @@ void CdString() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 + stlStringDictionary.Set(key, value); cdStringDictionary.Set(key, value); } } - void CdShort() + void Short() { var dict = DataParser_V1.ParseShort(_databaseContent); @@ -87,10 +39,11 @@ void CdShort() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 + stlShortDictionary.Set(key, value); cdShortDictionary.Set(key, value); } } - void CdLong() + void Long() { var dict = DataParser_V1.ParseLong(_databaseContent); @@ -101,10 +54,11 @@ void CdLong() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 + stlLongDictionary.Set(key, value); cdLongDictionary.Set(key, value); } } - void CdInt() + void Int() { var dict = DataParser_V1.ParseInt(_databaseContent); @@ -115,10 +69,11 @@ void CdInt() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 + stlIntegerDictionary.Set(key, value); cdIntegerDictionary.Set(key, value); } } - void CdFloat() + void Float() { var dict = DataParser_V1.ParseFloat(_databaseContent); @@ -129,10 +84,11 @@ void CdFloat() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 + stlFloatDictionary.Set(key, value); cdFloatDictionary.Set(key, value); } } - void CdDouble() + void Double() { var dict = DataParser_V1.ParseDouble(_databaseContent); @@ -143,10 +99,11 @@ void CdDouble() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 + stlDoubleDictionary.Set(key, value); cdDoubleDictionary.Set(key, value); } } - void CdDecimal() + void Decimal() { var dict = DataParser_V1.ParseDecimal(_databaseContent); @@ -157,10 +114,11 @@ void CdDecimal() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 + stlDecimalDictionary.Set(key, value); cdDecimalDictionary.Set(key, value); } } - void CdChar() + void Char() { var dict = DataParser_V1.ParseChar(_databaseContent); @@ -171,10 +129,11 @@ void CdChar() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 + stlCharDictionary.Set(key, value); cdCharDictionary.Set(key, value); } } - void CdBool() + void Bool() { var dict = DataParser_V1.ParseBoolean(_databaseContent); @@ -185,138 +144,41 @@ void CdBool() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 + stlBooleanDictionary.Set(key, value); cdBooleanDictionary.Set(key, value); } } #endregion - // SemaphoreThreadLock - void StlString() - { - var dict = DataParser_V1.ParseString(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - stlStringDictionary.Set(key, value); - } - } - void StlShort() - { - var dict = DataParser_V1.ParseShort(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - stlShortDictionary.Set(key, value); - } - } - void StlLong() - { - var dict = DataParser_V1.ParseLong(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - stlLongDictionary.Set(key, value); - } - } - void StlInt() - { - var dict = DataParser_V1.ParseInt(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - stlIntegerDictionary.Set(key, value); - } - } - void StlFloat() - { - var dict = DataParser_V1.ParseFloat(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - stlFloatDictionary.Set(key, value); - } - } - void StlDouble() - { - var dict = DataParser_V1.ParseDouble(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - stlDoubleDictionary.Set(key, value); - } - } - void StlDecimal() - { - var dict = DataParser_V1.ParseDecimal(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - stlDecimalDictionary.Set(key, value); - } - } - void StlChar() - { - var dict = DataParser_V1.ParseChar(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - stlCharDictionary.Set(key, value); - } - } - void StlBool() - { - var dict = DataParser_V1.ParseBoolean(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - stlBooleanDictionary.Set(key, value); - } - } + Thread _str = new Thread(String); + Thread _short = new Thread(Short); + Thread _long = new Thread(Long); + Thread _float = new Thread(Float); + Thread _double = new Thread(Double); + Thread _decimal = new Thread(Decimal); + Thread _char = new Thread(Char); + Thread _int = new Thread(Int); + Thread _bool = new Thread(Bool); + + _str.Start(); + _short.Start(); + _long.Start(); + _float.Start(); + _double.Start(); + _decimal.Start(); + _char.Start(); + _int.Start(); + _bool.Start(); + + _str.Join(); + _short.Join(); + _long.Join(); + _float.Join(); + _double.Join(); + _decimal.Join(); + _char.Join(); + _int.Join(); + _bool.Join(); } /// From 86524d3b4dd20ebb34d7f935cff09198119f899d Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Sat, 18 Jan 2025 22:33:38 +0800 Subject: [PATCH 11/16] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E5=88=B0=E7=BC=93=E5=AD=98=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 异步逻辑暂未优化,只优化了同步逻辑 --- TYLDDB.Test/Program.cs | 2 +- TYLDDB/TYLDDB.csproj | 3 +- TYLDDB/TYLDDB_Enum.cs | 2 + TYLDDB/TYLDDB_Instantiation.cs | 18 +- TYLDDB/TYLDDB_Parse.cs | 675 +++++++++++++++++++++++++-------- TYLDDB/TYLDDB_PrivateField.cs | 1 + 6 files changed, 530 insertions(+), 171 deletions(-) diff --git a/TYLDDB.Test/Program.cs b/TYLDDB.Test/Program.cs index 65ef162..92ba611 100644 --- a/TYLDDB.Test/Program.cs +++ b/TYLDDB.Test/Program.cs @@ -85,7 +85,7 @@ async void ParseDatabaseToTemp() #region 数据库解析缓存 HighPrecisionTimer parseDbTimer = new(); parseDbTimer.Start(); - await lddb.Parse_V1(); + lddb.Parse_V1(); parseDbTimer.Stop(); WriteTime("解析文件并写入缓存V1(同步): ", parseDbTimer.ElapsedMilliseconds()); HighPrecisionTimer parseDbTimerAsync = new(); diff --git a/TYLDDB/TYLDDB.csproj b/TYLDDB/TYLDDB.csproj index af2e0b4..fd0bc18 100644 --- a/TYLDDB/TYLDDB.csproj +++ b/TYLDDB/TYLDDB.csproj @@ -20,7 +20,8 @@ QingYi-Studio README.md · Apply a three-value dictionary. -· +· Optimizing LDDB.Parse_V1() +· Add more instantiation overloads diff --git a/TYLDDB/TYLDDB_Enum.cs b/TYLDDB/TYLDDB_Enum.cs index 3e9d630..382a0e6 100644 --- a/TYLDDB/TYLDDB_Enum.cs +++ b/TYLDDB/TYLDDB_Enum.cs @@ -18,11 +18,13 @@ public enum CacheMode /// SemaphoreThreadLock, +#if NET8_0_OR_GREATER /// /// High concurrency stability is achieved by using concurrent dictionary as core and semaphore based thread lock.
/// 以并发词典为核,结合基于信号量的线程锁实现高并发的稳定性。 ///
TripleDictionaryCache, +#endif /// /// + diff --git a/TYLDDB/TYLDDB_Instantiation.cs b/TYLDDB/TYLDDB_Instantiation.cs index 53ac7a5..c171071 100644 --- a/TYLDDB/TYLDDB_Instantiation.cs +++ b/TYLDDB/TYLDDB_Instantiation.cs @@ -45,6 +45,8 @@ public LDDB() stlBooleanDictionary = new StlBooleanDictionary(); #endregion + _cacheMode = CacheMode.CDAndSTL; + #if NET8_0_OR_GREATER tripleDictionaryCache = new TripleDictionaryCache(); #endif @@ -114,11 +116,11 @@ public LDDB(CacheMode mode) stlBooleanDictionary = new StlBooleanDictionary(); #endregion break; - case CacheMode.TripleDictionaryCache: #if NET8_0_OR_GREATER + case CacheMode.TripleDictionaryCache: tripleDictionaryCache = new TripleDictionaryCache(); -#endif break; +#endif } } @@ -152,6 +154,8 @@ public LDDB(DatabaseProcess process) stlBooleanDictionary = new StlBooleanDictionary(); #endregion + _cacheMode = CacheMode.CDAndSTL; + #if NET8_0_OR_GREATER tripleDictionaryCache = new TripleDictionaryCache(); #endif @@ -187,6 +191,7 @@ public LDDB(CacheMode mode, DatabaseProcess process) cdCharDictionary = new CdCharDictionary(); cdBooleanDictionary = new CdBooleanDictionary(); #endregion + _cacheMode = mode; break; case CacheMode.SemaphoreThreadLock: #region 实例化信号词典 @@ -200,6 +205,8 @@ public LDDB(CacheMode mode, DatabaseProcess process) stlCharDictionary = new StlCharDictionary(); stlBooleanDictionary = new StlBooleanDictionary(); #endregion + + _cacheMode = mode; break; case CacheMode.CDAndSTL: #region 实例化并发词典 @@ -225,12 +232,15 @@ public LDDB(CacheMode mode, DatabaseProcess process) stlCharDictionary = new StlCharDictionary(); stlBooleanDictionary = new StlBooleanDictionary(); #endregion + + _cacheMode = mode; break; - case CacheMode.TripleDictionaryCache: #if NET8_0_OR_GREATER + case CacheMode.TripleDictionaryCache: tripleDictionaryCache = new TripleDictionaryCache(); -#endif + _cacheMode = mode; break; +#endif } switch (process) diff --git a/TYLDDB/TYLDDB_Parse.cs b/TYLDDB/TYLDDB_Parse.cs index 32b1700..1097f72 100644 --- a/TYLDDB/TYLDDB_Parse.cs +++ b/TYLDDB/TYLDDB_Parse.cs @@ -12,173 +12,518 @@ public partial class LDDB /// public void Parse_V1() { - #region method - void String() - { - var dict = DataParser_V1.ParseString(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - stlStringDictionary.Set(key, value); - cdStringDictionary.Set(key, value); - } - } - void Short() - { - var dict = DataParser_V1.ParseShort(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - stlShortDictionary.Set(key, value); - cdShortDictionary.Set(key, value); - } - } - void Long() - { - var dict = DataParser_V1.ParseLong(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - stlLongDictionary.Set(key, value); - cdLongDictionary.Set(key, value); - } - } - void Int() - { - var dict = DataParser_V1.ParseInt(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - stlIntegerDictionary.Set(key, value); - cdIntegerDictionary.Set(key, value); - } - } - void Float() - { - var dict = DataParser_V1.ParseFloat(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - stlFloatDictionary.Set(key, value); - cdFloatDictionary.Set(key, value); - } - } - void Double() - { - var dict = DataParser_V1.ParseDouble(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - stlDoubleDictionary.Set(key, value); - cdDoubleDictionary.Set(key, value); - } - } - void Decimal() - { - var dict = DataParser_V1.ParseDecimal(_databaseContent); + switch (_cacheMode) + { + case CacheMode.CDAndSTL: + { + #region method + void String() + { + var dict = DataParser_V1.ParseString(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlStringDictionary.Set(key, value); + cdStringDictionary.Set(key, value); + } + } + void Short() + { + var dict = DataParser_V1.ParseShort(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlShortDictionary.Set(key, value); + cdShortDictionary.Set(key, value); + } + } + void Long() + { + var dict = DataParser_V1.ParseLong(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlLongDictionary.Set(key, value); + cdLongDictionary.Set(key, value); + } + } + void Int() + { + var dict = DataParser_V1.ParseInt(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlIntegerDictionary.Set(key, value); + cdIntegerDictionary.Set(key, value); + } + } + void Float() + { + var dict = DataParser_V1.ParseFloat(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlFloatDictionary.Set(key, value); + cdFloatDictionary.Set(key, value); + } + } + void Double() + { + var dict = DataParser_V1.ParseDouble(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlDoubleDictionary.Set(key, value); + cdDoubleDictionary.Set(key, value); + } + } + void Decimal() + { + var dict = DataParser_V1.ParseDecimal(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlDecimalDictionary.Set(key, value); + cdDecimalDictionary.Set(key, value); + } + } + void Char() + { + var dict = DataParser_V1.ParseChar(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlCharDictionary.Set(key, value); + cdCharDictionary.Set(key, value); + } + } + void Bool() + { + var dict = DataParser_V1.ParseBoolean(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlBooleanDictionary.Set(key, value); + cdBooleanDictionary.Set(key, value); + } + } + #endregion + + #region thread + Thread _str = new Thread(String); + Thread _short = new Thread(Short); + Thread _long = new Thread(Long); + Thread _float = new Thread(Float); + Thread _double = new Thread(Double); + Thread _decimal = new Thread(Decimal); + Thread _char = new Thread(Char); + Thread _int = new Thread(Int); + Thread _bool = new Thread(Bool); + + _str.Start(); + _short.Start(); + _long.Start(); + _float.Start(); + _double.Start(); + _decimal.Start(); + _char.Start(); + _int.Start(); + _bool.Start(); + + _str.Join(); + _short.Join(); + _long.Join(); + _float.Join(); + _double.Join(); + _decimal.Join(); + _char.Join(); + _int.Join(); + _bool.Join(); + break; + #endregion + } + + case CacheMode.SemaphoreThreadLock: + { + #region method + void String() + { + var dict = DataParser_V1.ParseString(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlStringDictionary.Set(key, value); + } + } + void Short() + { + var dict = DataParser_V1.ParseShort(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlShortDictionary.Set(key, value); + } + } + void Long() + { + var dict = DataParser_V1.ParseLong(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlLongDictionary.Set(key, value); + } + } + void Int() + { + var dict = DataParser_V1.ParseInt(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlIntegerDictionary.Set(key, value); + } + } + void Float() + { + var dict = DataParser_V1.ParseFloat(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlFloatDictionary.Set(key, value); + } + } + void Double() + { + var dict = DataParser_V1.ParseDouble(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlDoubleDictionary.Set(key, value); + } + } + void Decimal() + { + var dict = DataParser_V1.ParseDecimal(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlDecimalDictionary.Set(key, value); + } + } + void Char() + { + var dict = DataParser_V1.ParseChar(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlCharDictionary.Set(key, value); + } + } + void Bool() + { + var dict = DataParser_V1.ParseBoolean(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlBooleanDictionary.Set(key, value); + } + } + #endregion + + #region thread + Thread _str = new Thread(String); + Thread _short = new Thread(Short); + Thread _long = new Thread(Long); + Thread _float = new Thread(Float); + Thread _double = new Thread(Double); + Thread _decimal = new Thread(Decimal); + Thread _char = new Thread(Char); + Thread _int = new Thread(Int); + Thread _bool = new Thread(Bool); + + _str.Start(); + _short.Start(); + _long.Start(); + _float.Start(); + _double.Start(); + _decimal.Start(); + _char.Start(); + _int.Start(); + _bool.Start(); + + _str.Join(); + _short.Join(); + _long.Join(); + _float.Join(); + _double.Join(); + _decimal.Join(); + _char.Join(); + _int.Join(); + _bool.Join(); + break; + #endregion + } + + case CacheMode.ConcurrentDictionary: + { + #region method + void String() + { + var dict = DataParser_V1.ParseString(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + cdStringDictionary.Set(key, value); + } + } + void Short() + { + var dict = DataParser_V1.ParseShort(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + cdShortDictionary.Set(key, value); + } + } + void Long() + { + var dict = DataParser_V1.ParseLong(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + cdLongDictionary.Set(key, value); + } + } + void Int() + { + var dict = DataParser_V1.ParseInt(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + cdIntegerDictionary.Set(key, value); + } + } + void Float() + { + var dict = DataParser_V1.ParseFloat(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + cdFloatDictionary.Set(key, value); + } + } + void Double() + { + var dict = DataParser_V1.ParseDouble(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + cdDoubleDictionary.Set(key, value); + } + } + void Decimal() + { + var dict = DataParser_V1.ParseDecimal(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + cdDecimalDictionary.Set(key, value); + } + } + void Char() + { + var dict = DataParser_V1.ParseChar(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + cdCharDictionary.Set(key, value); + } + } + void Bool() + { + var dict = DataParser_V1.ParseBoolean(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + cdBooleanDictionary.Set(key, value); + } + } + #endregion + + #region thread + Thread _str = new Thread(String); + Thread _short = new Thread(Short); + Thread _long = new Thread(Long); + Thread _float = new Thread(Float); + Thread _double = new Thread(Double); + Thread _decimal = new Thread(Decimal); + Thread _char = new Thread(Char); + Thread _int = new Thread(Int); + Thread _bool = new Thread(Bool); + + _str.Start(); + _short.Start(); + _long.Start(); + _float.Start(); + _double.Start(); + _decimal.Start(); + _char.Start(); + _int.Start(); + _bool.Start(); + + _str.Join(); + _short.Join(); + _long.Join(); + _float.Join(); + _double.Join(); + _decimal.Join(); + _char.Join(); + _int.Join(); + _bool.Join(); + break; + #endregion + } - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - stlDecimalDictionary.Set(key, value); - cdDecimalDictionary.Set(key, value); - } - } - void Char() - { - var dict = DataParser_V1.ParseChar(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - stlCharDictionary.Set(key, value); - cdCharDictionary.Set(key, value); - } - } - void Bool() - { - var dict = DataParser_V1.ParseBoolean(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - stlBooleanDictionary.Set(key, value); - cdBooleanDictionary.Set(key, value); - } +#if NET8_0_OR_GREATER + case CacheMode.TripleDictionaryCache: + TripleDictParse(); + break; +#endif } - #endregion - - Thread _str = new Thread(String); - Thread _short = new Thread(Short); - Thread _long = new Thread(Long); - Thread _float = new Thread(Float); - Thread _double = new Thread(Double); - Thread _decimal = new Thread(Decimal); - Thread _char = new Thread(Char); - Thread _int = new Thread(Int); - Thread _bool = new Thread(Bool); - - _str.Start(); - _short.Start(); - _long.Start(); - _float.Start(); - _double.Start(); - _decimal.Start(); - _char.Start(); - _int.Start(); - _bool.Start(); - - _str.Join(); - _short.Join(); - _long.Join(); - _float.Join(); - _double.Join(); - _decimal.Join(); - _char.Join(); - _int.Join(); - _bool.Join(); } /// diff --git a/TYLDDB/TYLDDB_PrivateField.cs b/TYLDDB/TYLDDB_PrivateField.cs index babf4b3..2ccdcfe 100644 --- a/TYLDDB/TYLDDB_PrivateField.cs +++ b/TYLDDB/TYLDDB_PrivateField.cs @@ -16,6 +16,7 @@ public partial class LDDB private string _databaseContent; // 存储数据库内容 private readonly Database_V1 database_v1; private readonly Database_V2 database_v2; + private readonly CacheMode _cacheMode; private CdStringDictionary cdStringDictionary; private CdShortDictionary cdShortDictionary; private CdLongDictionary cdLongDictionary; From 3b6a109a30b2e0f758c8dcab385f324af81bf862 Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Sat, 18 Jan 2025 22:53:40 +0800 Subject: [PATCH 12/16] =?UTF-8?q?test:=20=E4=BC=98=E5=8C=96=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TYLDDB.Test/Program.cs | 276 ++++++++++++++++++++--------------------- 1 file changed, 136 insertions(+), 140 deletions(-) diff --git a/TYLDDB.Test/Program.cs b/TYLDDB.Test/Program.cs index 92ba611..50a07a2 100644 --- a/TYLDDB.Test/Program.cs +++ b/TYLDDB.Test/Program.cs @@ -1,174 +1,170 @@ using TimeRecord; using TYLDDB; -string dbFilePath = "./example.lddb"; -List testData = []; - -#region 实例化 -///////////////////////////////////////////////////////////////////////////////////////////////////////// 实例化 -var lddb = new LDDB(); -#endregion +internal class Program +{ + private readonly LDDB lddb = new LDDB(); + private readonly string dbFilePath = "./example.lddb"; + private readonly List testData = []; + private void Main(string[] args) + { + ReadFile(); -ReadFile(); + ReadDatabase(); -ReadDatabase(); + GetAllDatabaseName(); -GetAllDatabaseName(); + ParseDatabaseToTemp(); -ParseDatabaseToTemp(); + CdAllTypeSearch(); -CdAllTypeSearch(); + StlAllTypeSearch(); -StlAllTypeSearch(); + TripleDictionaryTest(); -TripleDictionaryTest(); + ExportTestData(); + Console.ReadLine(); + } + #region Test Method + private void ReadFile() + { + #region 读取文件 + ///////////////////////////////////////////////////////////////////////////////////////////////////////// 读取文件 + HighPrecisionTimer readDbTimer = new(); // 从发起读取文件到成功读取的总时间 + lddb.FilePath = dbFilePath; + readDbTimer.Start(); + lddb.ReadingFile(); + readDbTimer.Stop(); + WriteTime("从发起读取文件指令到成功读取的总时间为: ", readDbTimer.ElapsedMilliseconds()); + #endregion + } -ExportTestData(); -Console.ReadLine(); + private void ReadDatabase() + { + #region 读取数据库 + ///////////////////////////////////////////////////////////////////////////////////////////////////////// 读取数据库 + HighPrecisionTimer loadDbTimer = new(); // 从发起读取数据库到成功返回读取内容的总时间 + loadDbTimer.Start(); + lddb.LoadDatabase("database1"); + Console.WriteLine(lddb.GetLoadingDatabaseContent()); // 输出database1内容 + loadDbTimer.Stop(); + WriteTime("从发起读取数据库指令到成功返回读取内容的总时间为(V1): ", loadDbTimer.ElapsedMilliseconds()); + + HighPrecisionTimer loadDbV2Timer = new(); // 从发起读取数据库到成功返回读取内容的总时间 + loadDbV2Timer.Start(); + lddb.LoadDatabase_V2("database1"); + Console.WriteLine(lddb.GetLoadingDatabaseContent()); // 输出database1内容 + loadDbV2Timer.Stop(); + WriteTime("从发起读取数据库指令到成功返回读取内容的总时间为(V2): ", loadDbV2Timer.ElapsedMilliseconds()); + #endregion + } -#region Test Method -void ReadFile() -{ - #region 读取文件 - ///////////////////////////////////////////////////////////////////////////////////////////////////////// 读取文件 - HighPrecisionTimer readDbTimer = new(); // 从发起读取文件到成功读取的总时间 - lddb.FilePath = dbFilePath; - readDbTimer.Start(); - lddb.ReadingFile(); - readDbTimer.Stop(); - WriteTime("从发起读取文件指令到成功读取的总时间为: ", readDbTimer.ElapsedMilliseconds()); - #endregion -} + private void GetAllDatabaseName() + { + #region 获取所有数据库名称 + ///////////////////////////////////////////////////////////////////////////////////////////////////////// 获取所有数据库名称 + HighPrecisionTimer readAllDbNameTimer = new(); // 从发起读取数据库名称到成功返回读取内容的总时间 + readAllDbNameTimer.Start(); + lddb.ReadAllDatabaseName(); + readAllDbNameTimer.Stop(); + if (lddb.AllDatabaseName != null) + { + foreach (var dbName in lddb.AllDatabaseName) + { + Console.WriteLine(dbName); + } + } + WriteTime("从发起读取数据库名称到成功返回读取内容的总时间为: ", readAllDbNameTimer.ElapsedMilliseconds()); + #endregion + } -void ReadDatabase() -{ - #region 读取数据库 - ///////////////////////////////////////////////////////////////////////////////////////////////////////// 读取数据库 - HighPrecisionTimer loadDbTimer = new(); // 从发起读取数据库到成功返回读取内容的总时间 - loadDbTimer.Start(); - lddb.LoadDatabase("database1"); - Console.WriteLine(lddb.GetLoadingDatabaseContent()); // 输出database1内容 - loadDbTimer.Stop(); - WriteTime("从发起读取数据库指令到成功返回读取内容的总时间为(V1): ", loadDbTimer.ElapsedMilliseconds()); - - HighPrecisionTimer loadDbV2Timer = new(); // 从发起读取数据库到成功返回读取内容的总时间 - loadDbV2Timer.Start(); - lddb.LoadDatabase_V2("database1"); - Console.WriteLine(lddb.GetLoadingDatabaseContent()); // 输出database1内容 - loadDbV2Timer.Stop(); - WriteTime("从发起读取数据库指令到成功返回读取内容的总时间为(V2): ", loadDbV2Timer.ElapsedMilliseconds()); - #endregion -} + async void ParseDatabaseToTemp() + { + #region 数据库解析缓存 + HighPrecisionTimer parseDbTimer = new(); + parseDbTimer.Start(); + lddb.Parse_V1(); + parseDbTimer.Stop(); + WriteTime("解析文件并写入缓存V1(同步): ", parseDbTimer.ElapsedMilliseconds()); + HighPrecisionTimer parseDbTimerAsync = new(); + parseDbTimerAsync.Start(); + await lddb.ParseAsync_V1(); + parseDbTimerAsync.Stop(); + WriteTime("解析文件并写入缓存V1(异步): ", parseDbTimerAsync.ElapsedMilliseconds()); + #endregion + } -void GetAllDatabaseName() -{ - #region 获取所有数据库名称 - ///////////////////////////////////////////////////////////////////////////////////////////////////////// 获取所有数据库名称 - HighPrecisionTimer readAllDbNameTimer = new(); // 从发起读取数据库名称到成功返回读取内容的总时间 - readAllDbNameTimer.Start(); - lddb.ReadAllDatabaseName(); - readAllDbNameTimer.Stop(); - if (lddb.AllDatabaseName != null) + private void CdAllTypeSearch() { - foreach (var dbName in lddb.AllDatabaseName) + #region 并发词典数据库全类型搜寻 + HighPrecisionTimer allTypeSearchFromConcurrentDictionaryTimer = new(); + allTypeSearchFromConcurrentDictionaryTimer.Start(); + string[] AllTypeSearchFromConcurrentDictionaryResult = lddb.AllTypeSearchFromConcurrentDictionary("str_name"); + allTypeSearchFromConcurrentDictionaryTimer.Stop(); + // 使用 foreach 输出数组的每个元素 + foreach (var str in AllTypeSearchFromConcurrentDictionaryResult) { - Console.WriteLine(dbName); + Console.WriteLine(str); } + WriteTime("并发词典数据库全类型同步搜寻: ", allTypeSearchFromConcurrentDictionaryTimer.ElapsedMilliseconds()); + #endregion } - WriteTime("从发起读取数据库名称到成功返回读取内容的总时间为: ", readAllDbNameTimer.ElapsedMilliseconds()); - #endregion -} - -async void ParseDatabaseToTemp() -{ - #region 数据库解析缓存 - HighPrecisionTimer parseDbTimer = new(); - parseDbTimer.Start(); - lddb.Parse_V1(); - parseDbTimer.Stop(); - WriteTime("解析文件并写入缓存V1(同步): ", parseDbTimer.ElapsedMilliseconds()); - HighPrecisionTimer parseDbTimerAsync = new(); - parseDbTimerAsync.Start(); - await lddb.ParseAsync_V1(); - parseDbTimerAsync.Stop(); - WriteTime("解析文件并写入缓存V1(异步): ", parseDbTimerAsync.ElapsedMilliseconds()); - #endregion -} -void CdAllTypeSearch() -{ - #region 并发词典数据库全类型搜寻 - HighPrecisionTimer allTypeSearchFromConcurrentDictionaryTimer = new(); - allTypeSearchFromConcurrentDictionaryTimer.Start(); - string[] AllTypeSearchFromConcurrentDictionaryResult = lddb.AllTypeSearchFromConcurrentDictionary("str_name"); - allTypeSearchFromConcurrentDictionaryTimer.Stop(); - // 使用 foreach 输出数组的每个元素 - foreach (var str in AllTypeSearchFromConcurrentDictionaryResult) + private void StlAllTypeSearch() { - Console.WriteLine(str); + #region 信号量线程锁词典数据库全类型搜寻 + HighPrecisionTimer allTypeSearchFromSemaphoreThreadLockTimer = new(); + allTypeSearchFromSemaphoreThreadLockTimer.Start(); + string[] AllTypeSearchFromSemaphoreThreadLockResult = lddb.AllTypeSearchFromSemaphoreThreadLock("str_name"); + allTypeSearchFromSemaphoreThreadLockTimer.Stop(); + // 使用 foreach 输出数组的每个元素 + foreach (var str in AllTypeSearchFromSemaphoreThreadLockResult) + { + Console.WriteLine(str); + } + WriteTime("信号量线程锁词典数据库全类型搜寻: ", allTypeSearchFromSemaphoreThreadLockTimer.ElapsedMilliseconds()); + #endregion } - WriteTime("并发词典数据库全类型同步搜寻: ", allTypeSearchFromConcurrentDictionaryTimer.ElapsedMilliseconds()); - #endregion -} -void StlAllTypeSearch() -{ - #region 信号量线程锁词典数据库全类型搜寻 - HighPrecisionTimer allTypeSearchFromSemaphoreThreadLockTimer = new(); - allTypeSearchFromSemaphoreThreadLockTimer.Start(); - string[] AllTypeSearchFromSemaphoreThreadLockResult = lddb.AllTypeSearchFromSemaphoreThreadLock("str_name"); - allTypeSearchFromSemaphoreThreadLockTimer.Stop(); - // 使用 foreach 输出数组的每个元素 - foreach (var str in AllTypeSearchFromSemaphoreThreadLockResult) + private void TripleDictionaryTest() { - Console.WriteLine(str); + HighPrecisionTimer parse = new(); + parse.Start(); + lddb.TripleDictParse(); + parse.Stop(); + WriteTime("三值字典解析并写入(同步): ", parse.ElapsedMilliseconds()); } - WriteTime("信号量线程锁词典数据库全类型搜寻: ", allTypeSearchFromSemaphoreThreadLockTimer.ElapsedMilliseconds()); #endregion -} -void TripleDictionaryTest() -{ - HighPrecisionTimer parse = new(); - parse.Start(); - lddb.TripleDictParse(); - parse.Stop(); - WriteTime("三值字典解析并写入(同步): ", parse.ElapsedMilliseconds()); -} -#endregion + #region 工具 + private void WriteTime(string what, double time) + { + string data = what + time + "ms\n"; + Console.WriteLine(data); + testData.Add(data); + Console.WriteLine(); + } -///////////////////////////////////////////////////////////////////////////////////////////////////////// 工具 -void WriteTime(string what, double time) -{ - string data = what + time + "ms\n"; - Console.WriteLine(data); - AddTestData(data); - Console.WriteLine(); -} + private void ExportTestData() + { + // 获取当前日期和时间,格式化为 "yyyy-MM-dd HH-mm" + string fileName = DateTime.Now.ToString("yyyy-MM-dd HH-mm") + ".txt"; -void AddTestData(string data) -{ - testData.Add(data); -} + // 指定文件路径 + string directoryPath = "./testdata/"; + string filePath = Path.Combine(directoryPath, fileName); -void ExportTestData() -{ - // 获取当前日期和时间,格式化为 "yyyy-MM-dd HH-mm" - string fileName = DateTime.Now.ToString("yyyy-MM-dd HH-mm") + ".txt"; + // 确保目录存在,如果不存在则创建 + if (!Directory.Exists(directoryPath)) + { + Directory.CreateDirectory(directoryPath); + } - // 指定文件路径 - string directoryPath = "./testdata/"; - string filePath = Path.Combine(directoryPath, fileName); + // 将 List 中的每一行写入文件 + File.WriteAllLines(filePath, testData); - // 确保目录存在,如果不存在则创建 - if (!Directory.Exists(directoryPath)) - { - Directory.CreateDirectory(directoryPath); + Console.WriteLine($"数据已成功写入文件: {filePath}"); } - - // 将 List 中的每一行写入文件 - File.WriteAllLines(filePath, testData); - - Console.WriteLine($"数据已成功写入文件: {filePath}"); + #endregion } From 00abd2b14d651ff54df161a39a5156c35af5f227 Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Sat, 18 Jan 2025 23:06:42 +0800 Subject: [PATCH 13/16] =?UTF-8?q?chore:=20=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E9=A2=84=E8=A7=A3=E6=9E=90=E4=BB=A3=E7=A0=81=E5=BE=AE=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TYLDDB/Utils/Database/V1.cs | 2 +- TYLDDB/Utils/Database/V2.cs | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/TYLDDB/Utils/Database/V1.cs b/TYLDDB/Utils/Database/V1.cs index 85cb750..28f81ad 100644 --- a/TYLDDB/Utils/Database/V1.cs +++ b/TYLDDB/Utils/Database/V1.cs @@ -45,7 +45,7 @@ public string GetDatabaseContent(string content, string databaseName) string databaseContent = match.Groups[1].Value; // 替换多余的空格和换行符,保持引号内的空格 - string cleanedContent = Regex.Replace(databaseContent, @"\s*(?=\S)", ""); + // string cleanedContent = Regex.Replace(databaseContent, @"\s*(?=\S)", ""); return databaseContent; } diff --git a/TYLDDB/Utils/Database/V2.cs b/TYLDDB/Utils/Database/V2.cs index 9a0e66d..9ae3a52 100644 --- a/TYLDDB/Utils/Database/V2.cs +++ b/TYLDDB/Utils/Database/V2.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Text; namespace TYLDDB.Utils.Database { @@ -27,10 +28,10 @@ public string GetDatabaseContent(string input, string databaseName) // 提取数据库内容 int contentStartIndex = startIndex + startMarker.Length; - string dbContent = input.Substring(contentStartIndex, endIndex - contentStartIndex).Trim(); + string dbContent = input[contentStartIndex..endIndex].Trim(); // 使用 StringBuilder 高效地处理每行开头的空格 - var result = new System.Text.StringBuilder(); + var result = new StringBuilder(); // 将数据库内容按行分割 string[] lines = dbContent.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); From bf6cb73c24e8dd5b80c4183642626ac8f445dbef Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Sat, 18 Jan 2025 23:06:58 +0800 Subject: [PATCH 14/16] =?UTF-8?q?test:=20=E6=B5=8B=E8=AF=95=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E8=B0=83=E6=95=B4=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TYLDDB-CSharp.sln | 4 ++-- TYLDDB.Test/Program.cs | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/TYLDDB-CSharp.sln b/TYLDDB-CSharp.sln index bd725a6..b4cfc93 100644 --- a/TYLDDB-CSharp.sln +++ b/TYLDDB-CSharp.sln @@ -30,8 +30,8 @@ Global {FDFD566C-CB13-43EB-970E-0EC0A21E36C0}.Debug|Any CPU.Build.0 = Debug|Any CPU {FDFD566C-CB13-43EB-970E-0EC0A21E36C0}.Release|Any CPU.ActiveCfg = Release|Any CPU {FDFD566C-CB13-43EB-970E-0EC0A21E36C0}.Release|Any CPU.Build.0 = Release|Any CPU - {F95CF6BC-2255-47F8-AAB3-B7812BB2894D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {F95CF6BC-2255-47F8-AAB3-B7812BB2894D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F95CF6BC-2255-47F8-AAB3-B7812BB2894D}.Debug|Any CPU.ActiveCfg = Release|Any CPU + {F95CF6BC-2255-47F8-AAB3-B7812BB2894D}.Debug|Any CPU.Build.0 = Release|Any CPU {F95CF6BC-2255-47F8-AAB3-B7812BB2894D}.Release|Any CPU.ActiveCfg = Release|Any CPU {F95CF6BC-2255-47F8-AAB3-B7812BB2894D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection diff --git a/TYLDDB.Test/Program.cs b/TYLDDB.Test/Program.cs index 50a07a2..53c0096 100644 --- a/TYLDDB.Test/Program.cs +++ b/TYLDDB.Test/Program.cs @@ -3,10 +3,21 @@ internal class Program { - private readonly LDDB lddb = new LDDB(); + private static void Main() + { + Test test = new(); + test.TestMethod(); + } + +} + +class Test +{ + private readonly LDDB lddb = new(); private readonly string dbFilePath = "./example.lddb"; private readonly List testData = []; - private void Main(string[] args) + + public void TestMethod() { ReadFile(); From 0e9130acab268f872b07855af01b0656fc5172d7 Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Sat, 18 Jan 2025 23:14:21 +0800 Subject: [PATCH 15/16] =?UTF-8?q?feat:=20=E8=AE=A9=E4=B8=89=E5=80=BC?= =?UTF-8?q?=E5=AD=97=E5=85=B8=E7=BA=BF=E7=A8=8B=E5=AE=89=E5=85=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TDCache/TripleDictionaryCache.cs | 40 ++++++++++++++++--- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/TYLDDB/Utils/FastCache/TDCache/TripleDictionaryCache.cs b/TYLDDB/Utils/FastCache/TDCache/TripleDictionaryCache.cs index 689a05e..59aba74 100644 --- a/TYLDDB/Utils/FastCache/TDCache/TripleDictionaryCache.cs +++ b/TYLDDB/Utils/FastCache/TDCache/TripleDictionaryCache.cs @@ -5,11 +5,19 @@ namespace TYLDDB.Utils.FastCache.TDCache { + /// + /// Three-valued dictionary cache, based on concurrent dictionary as core, combined with semaphore based thread lock to achieve high concurrency stability.
+ /// 三值字典缓存,以并发词典为核,结合基于信号量的线程锁实现高并发的稳定性。 + ///
public class TripleDictionaryCache : ITripleDictionaryCache, IDisposable { private TripleDictionary dictionary; - private SemaphoreSlim semaphore; // 控制并发访问 + private readonly SemaphoreSlim semaphore; // 控制并发访问 + /// + /// Three-valued dictionary cache, based on concurrent dictionary as core, combined with semaphore based thread lock to achieve high concurrency stability.
+ /// 三值字典缓存,以并发词典为核,结合基于信号量的线程锁实现高并发的稳定性。 + ///
public TripleDictionaryCache() { dictionary = new TripleDictionary(); @@ -31,8 +39,10 @@ public void Dispose() /// public void Clear() { - dictionary = new TripleDictionary(); - semaphore = null; + lock (semaphore) + { + dictionary = new TripleDictionary(); + } } /// @@ -42,7 +52,13 @@ public void Clear() /// Data type.
数据类型。 /// Key
键 /// Value
- public object Get(string type, string key) => dictionary.Get(type, key); + public object Get(string type, string key) + { + lock (semaphore) + { + return dictionary.Get(type, key); + } + } /// /// Remove a cache entry by its key.
@@ -51,7 +67,13 @@ public void Clear() /// Data type.
数据类型。 /// Key
键 /// Whether the removal is successful.
移除操作是否成功。
- public bool Remove(string type, string key) => dictionary.RemoveKey(type, key); + public bool Remove(string type, string key) + { + lock (semaphore) + { + return dictionary.RemoveKey(type, key); + } + } /// /// Set a cache entry for a specified key.
@@ -61,7 +83,13 @@ public void Clear() /// Key
键 /// Value
值 /// Whether the operation is successful.
操作是否成功。
- public bool Set(string type, string key, object value) => dictionary.Add(type, key, value); + public bool Set(string type, string key, object value) + { + lock (semaphore) + { + return dictionary.Add(type, key, value); + } + } } } #endif \ No newline at end of file From 78fcc04f5512703d28c9b2a962eea00fa0535c0b Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Sat, 18 Jan 2025 23:24:30 +0800 Subject: [PATCH 16/16] =?UTF-8?q?feat:=20=E5=88=A0=E9=99=A4=20Semaphore=20?= =?UTF-8?q?Slim?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 并发词典不需要搞这种,真的是傻了 太晚了,脑子都糊涂了 --- .../TDCache/TripleDictionaryCache.cs | 34 +++---------------- 1 file changed, 4 insertions(+), 30 deletions(-) diff --git a/TYLDDB/Utils/FastCache/TDCache/TripleDictionaryCache.cs b/TYLDDB/Utils/FastCache/TDCache/TripleDictionaryCache.cs index 59aba74..84ad64b 100644 --- a/TYLDDB/Utils/FastCache/TDCache/TripleDictionaryCache.cs +++ b/TYLDDB/Utils/FastCache/TDCache/TripleDictionaryCache.cs @@ -12,7 +12,6 @@ namespace TYLDDB.Utils.FastCache.TDCache public class TripleDictionaryCache : ITripleDictionaryCache, IDisposable { private TripleDictionary dictionary; - private readonly SemaphoreSlim semaphore; // 控制并发访问 /// /// Three-valued dictionary cache, based on concurrent dictionary as core, combined with semaphore based thread lock to achieve high concurrency stability.
@@ -21,7 +20,6 @@ public class TripleDictionaryCache : ITripleDictionaryCache, IDisposable public TripleDictionaryCache() { dictionary = new TripleDictionary(); - semaphore = new SemaphoreSlim(1, 1); // 使用信号量来同步 } /// @@ -37,13 +35,7 @@ public void Dispose() /// Clear all cache entries.
/// 清空所有缓存项。 ///
- public void Clear() - { - lock (semaphore) - { - dictionary = new TripleDictionary(); - } - } + public void Clear() => dictionary = new TripleDictionary(); /// /// Synchronization method: Obtain the corresponding value by key.
@@ -52,13 +44,7 @@ public void Clear() /// Data type.
数据类型。 /// Key
键 /// Value
- public object Get(string type, string key) - { - lock (semaphore) - { - return dictionary.Get(type, key); - } - } + public object Get(string type, string key) => dictionary.Get(type, key); /// /// Remove a cache entry by its key.
@@ -67,13 +53,7 @@ public object Get(string type, string key) /// Data type.
数据类型。 /// Key
键 /// Whether the removal is successful.
移除操作是否成功。
- public bool Remove(string type, string key) - { - lock (semaphore) - { - return dictionary.RemoveKey(type, key); - } - } + public bool Remove(string type, string key) => dictionary.RemoveKey(type, key); /// /// Set a cache entry for a specified key.
@@ -83,13 +63,7 @@ public bool Remove(string type, string key) /// Key
键 /// Value
值 /// Whether the operation is successful.
操作是否成功。
- public bool Set(string type, string key, object value) - { - lock (semaphore) - { - return dictionary.Add(type, key, value); - } - } + public bool Set(string type, string key, object value) => dictionary.Add(type, key, value); } } #endif \ No newline at end of file