diff --git a/TYLDDB-CSharp.sln b/TYLDDB-CSharp.sln index d409f8a..bd725a6 100644 --- a/TYLDDB-CSharp.sln +++ b/TYLDDB-CSharp.sln @@ -12,16 +12,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TYLDDB.Test", "TYLDDB.Test\TYLDDB.Test.csproj", "{FDFD566C-CB13-43EB-970E-0EC0A21E36C0}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TYLDDB.Utils.FastCache.Test", "TYLDDB.Utils.FastCache.Test\TYLDDB.Utils.FastCache.Test.csproj", "{A58C0A6B-9BD4-4B29-9B5F-5FDFB849D23B}" -EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Test", "Test", "{95BC8FAF-AB03-44A3-A7E7-8E42FA7138E3}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TYLDDB.Utils.Writer.Test", "TYLDDB.Utils.Writer.Test\TYLDDB.Utils.Writer.Test.csproj", "{1BA4CC53-078F-4943-B8EF-A522DA9E5060}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TimeRecord", "TimeRecord\TimeRecord.csproj", "{F95CF6BC-2255-47F8-AAB3-B7812BB2894D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TYLDDB.Parser.Test", "TYLDDB.Parser.Test\TYLDDB.Parser.Test.csproj", "{0B96CCF3-16C7-4B18-9D07-A19D5D8E9641}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -36,32 +30,17 @@ 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 - {A58C0A6B-9BD4-4B29-9B5F-5FDFB849D23B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {A58C0A6B-9BD4-4B29-9B5F-5FDFB849D23B}.Debug|Any CPU.Build.0 = Debug|Any CPU - {A58C0A6B-9BD4-4B29-9B5F-5FDFB849D23B}.Release|Any CPU.ActiveCfg = Release|Any CPU - {A58C0A6B-9BD4-4B29-9B5F-5FDFB849D23B}.Release|Any CPU.Build.0 = Release|Any CPU - {1BA4CC53-078F-4943-B8EF-A522DA9E5060}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {1BA4CC53-078F-4943-B8EF-A522DA9E5060}.Debug|Any CPU.Build.0 = Debug|Any CPU - {1BA4CC53-078F-4943-B8EF-A522DA9E5060}.Release|Any CPU.ActiveCfg = Release|Any CPU - {1BA4CC53-078F-4943-B8EF-A522DA9E5060}.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}.Release|Any CPU.ActiveCfg = Release|Any CPU {F95CF6BC-2255-47F8-AAB3-B7812BB2894D}.Release|Any CPU.Build.0 = Release|Any CPU - {0B96CCF3-16C7-4B18-9D07-A19D5D8E9641}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {0B96CCF3-16C7-4B18-9D07-A19D5D8E9641}.Debug|Any CPU.Build.0 = Debug|Any CPU - {0B96CCF3-16C7-4B18-9D07-A19D5D8E9641}.Release|Any CPU.ActiveCfg = Release|Any CPU - {0B96CCF3-16C7-4B18-9D07-A19D5D8E9641}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution {FDFD566C-CB13-43EB-970E-0EC0A21E36C0} = {95BC8FAF-AB03-44A3-A7E7-8E42FA7138E3} - {A58C0A6B-9BD4-4B29-9B5F-5FDFB849D23B} = {95BC8FAF-AB03-44A3-A7E7-8E42FA7138E3} - {1BA4CC53-078F-4943-B8EF-A522DA9E5060} = {95BC8FAF-AB03-44A3-A7E7-8E42FA7138E3} {F95CF6BC-2255-47F8-AAB3-B7812BB2894D} = {95BC8FAF-AB03-44A3-A7E7-8E42FA7138E3} - {0B96CCF3-16C7-4B18-9D07-A19D5D8E9641} = {95BC8FAF-AB03-44A3-A7E7-8E42FA7138E3} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {19390089-354C-41AD-AF7D-9FB3998A88EA} diff --git a/TYLDDB.Test/Program.cs b/TYLDDB.Test/Program.cs index f727e38..9158a16 100644 --- a/TYLDDB.Test/Program.cs +++ b/TYLDDB.Test/Program.cs @@ -36,8 +36,12 @@ } WriteTime("从发起读取数据库名称到成功返回读取内容的总时间为: ", readAllDbNameTimer.ElapsedMilliseconds()); - - +///////////////////////////////////////////////////////////////////////////////////////////////////////// 数据库解析缓存 +HighPrecisionTimer parseDbTimer = new(); // 从发起解析文件到成功解析并写入缓存的总时间 +parseDbTimer.Start(); +await lddb.ParseAsync(); +parseDbTimer.Stop(); +WriteTime("从发起解析文件到成功解析并写入缓存的总时间: ", parseDbTimer.ElapsedMilliseconds()); diff --git a/TYLDDB.Utils.FastCache.Test/Program.cs b/TYLDDB.Utils.FastCache.Test/Program.cs deleted file mode 100644 index 5172226..0000000 --- a/TYLDDB.Utils.FastCache.Test/Program.cs +++ /dev/null @@ -1,149 +0,0 @@ -using TimeRecord; -using TYLDDB.Utils.FastCache; - -Console.WriteLine("基于信号量的缓存读写运行"); -SemaphoreSlimDefault(); - -Console.WriteLine(); -Console.WriteLine("基于信号量的缓存读写运行"); -ConcurrentDictionary(); -Console.ReadLine(); - -// 基于信号量的缓存读写运行 -static async void SemaphoreSlimDefault() -{ - var setTime = new HighPrecisionTimer(); - var setAsyncTime = new HighPrecisionTimer(); - var getByKeyTime = new HighPrecisionTimer(); - var getByKeyAsyncTime = new HighPrecisionTimer(); - var getKeysByValueTime = new HighPrecisionTimer(); - var getKeysByValueAsyncTime = new HighPrecisionTimer(); - var getAllCacheTime = new HighPrecisionTimer(); - var getAllCacheAsyncTime = new HighPrecisionTimer(); - - var cache = new SemaphoreThreadLock(); - - setTime.Start(); - cache.Set("TESTKEY1", "TESTVALUE1"); - setTime.Stop(); - - setAsyncTime.Start(); - await cache.SetAsync("TESTKEY2", "TESTVALUE2"); - setAsyncTime.Stop(); - - getByKeyTime.Start(); - Console.WriteLine("TESTKEY1对应的值:" + cache.GetByKey("TESTKEY1")); - getByKeyTime.Stop(); - - getByKeyAsyncTime.Start(); - Console.WriteLine("TESTKEY2对应的值:" + await cache.GetByKeyAsync("TESTKEY2")); - getByKeyAsyncTime.Stop(); - - cache.Set("TESTKEY3", "TESTVALUE2"); - - Console.WriteLine("TESTVALUE2对应的所有键:"); - getKeysByValueTime.Start(); - Console.WriteLine(string.Join(", ", cache.GetKeysByValue("TESTVALUE2"))); - getKeysByValueTime.Stop(); - - Console.WriteLine("TESTVALUE2对应的所有键:"); - getKeysByValueAsyncTime.Start(); - Console.WriteLine(string.Join(", ", await cache.GetKeysByValueAsync("TESTVALUE2"))); - getKeysByValueAsyncTime.Stop(); - - // 获取并输出所有缓存项(同步方法) - getAllCacheTime.Start(); - var allCacheSync = cache.GetAllCache(); - Console.WriteLine("同步获取所有缓存项:"); - foreach (var kvp in allCacheSync) - { - Console.WriteLine($"{kvp.Key}: {kvp.Value}"); - } - getAllCacheTime.Stop(); - - // 获取并输出所有缓存项(异步方法) - getAllCacheAsyncTime.Start(); - var allCacheAsync = await cache.GetAllCacheAsync(); - Console.WriteLine("异步获取所有缓存项:"); - foreach (var kvp in allCacheAsync) - { - Console.WriteLine($"{kvp.Key}: {kvp.Value}"); - } - getAllCacheAsyncTime.Stop(); - - - Console.WriteLine("时间消耗:"); - Console.WriteLine($"设置键值(同步):{setTime.ElapsedMilliseconds()}ms 设置键值(异步):{setAsyncTime.ElapsedMilliseconds()}ms"); - Console.WriteLine($"根据键获取值(同步):{getByKeyTime.ElapsedMilliseconds()}ms 根据键获取值(异步):{getByKeyAsyncTime.ElapsedMilliseconds()}ms"); - Console.WriteLine($"根据值获取键(同步):{getKeysByValueTime.ElapsedMilliseconds()}ms 根据值获取键(异步):{getKeysByValueAsyncTime.ElapsedMilliseconds()}ms"); - Console.WriteLine($"获取所有缓存项(同步):{getAllCacheTime.ElapsedMilliseconds()}ms 获取所有缓存项(异步):{getAllCacheAsyncTime.ElapsedMilliseconds()}ms"); -} - -// 基于并发词典的缓存读写运行 -static async void ConcurrentDictionary() -{ - var setTime = new HighPrecisionTimer(); - var setAsyncTime = new HighPrecisionTimer(); - var getByKeyTime = new HighPrecisionTimer(); - var getByKeyAsyncTime = new HighPrecisionTimer(); - var getKeysByValueTime = new HighPrecisionTimer(); - var getKeysByValueAsyncTime = new HighPrecisionTimer(); - var getAllCacheTime = new HighPrecisionTimer(); - var getAllCacheAsyncTime = new HighPrecisionTimer(); - - var cache = new ConcurrentDictionary(); - - setTime.Start(); - cache.Set("TESTKEY1", "TESTVALUE1"); - setTime.Stop(); - - setAsyncTime.Start(); - await cache.SetAsync("TESTKEY2", "TESTVALUE2"); - setAsyncTime.Stop(); - - getByKeyTime.Start(); - Console.WriteLine("TESTKEY1对应的值:" + cache.GetByKey("TESTKEY1")); - getByKeyTime.Stop(); - - getByKeyAsyncTime.Start(); - Console.WriteLine("TESTKEY2对应的值:" + await cache.GetByKeyAsync("TESTKEY2")); - getByKeyAsyncTime.Stop(); - - cache.Set("TESTKEY3", "TESTVALUE2"); - - Console.WriteLine("TESTVALUE2对应的所有键:"); - getKeysByValueTime.Start(); - Console.WriteLine(string.Join(", ", cache.GetKeysByValue("TESTVALUE2"))); - getKeysByValueTime.Stop(); - - Console.WriteLine("TESTVALUE2对应的所有键:"); - getKeysByValueAsyncTime.Start(); - Console.WriteLine(string.Join(", ", await cache.GetKeysByValueAsync("TESTVALUE2"))); - getKeysByValueAsyncTime.Stop(); - - // 获取并输出所有缓存项(同步方法) - getAllCacheTime.Start(); - var allCacheSync = cache.GetAllCache(); - Console.WriteLine("同步获取所有缓存项:"); - foreach (var kvp in allCacheSync) - { - Console.WriteLine($"{kvp.Key}: {kvp.Value}"); - } - getAllCacheTime.Stop(); - - // 获取并输出所有缓存项(异步方法) - getAllCacheAsyncTime.Start(); - var allCacheAsync = await cache.GetAllCacheAsync(); - Console.WriteLine("异步获取所有缓存项:"); - foreach (var kvp in allCacheAsync) - { - Console.WriteLine($"{kvp.Key}: {kvp.Value}"); - } - getAllCacheAsyncTime.Stop(); - - Console.WriteLine("时间消耗:"); - Console.WriteLine($"设置键值(同步):{setTime.ElapsedMilliseconds()}ms 设置键值(异步):{setAsyncTime.ElapsedMilliseconds()}ms"); - Console.WriteLine($"根据键获取值(同步):{getByKeyTime.ElapsedMilliseconds()}ms 根据键获取值(异步):{getByKeyAsyncTime.ElapsedMilliseconds()}ms"); - Console.WriteLine($"根据值获取键(同步):{getKeysByValueTime.ElapsedMilliseconds()}ms 根据值获取键(异步):{getKeysByValueAsyncTime.ElapsedMilliseconds()}ms"); - Console.WriteLine($"获取所有缓存项(同步):{getAllCacheTime.ElapsedMilliseconds()}ms 获取所有缓存项(异步):{getAllCacheAsyncTime.ElapsedMilliseconds()}ms"); -} diff --git a/TYLDDB.Utils.FastCache.Test/TYLDDB.Utils.FastCache.Test.csproj b/TYLDDB.Utils.FastCache.Test/TYLDDB.Utils.FastCache.Test.csproj deleted file mode 100644 index 700866e..0000000 --- a/TYLDDB.Utils.FastCache.Test/TYLDDB.Utils.FastCache.Test.csproj +++ /dev/null @@ -1,15 +0,0 @@ - - - - Exe - net8.0 - enable - enable - - - - - - - - diff --git a/TYLDDB/Parser/Parser.cs b/TYLDDB/Parser/Parser.cs index 326f561..02c995b 100644 --- a/TYLDDB/Parser/Parser.cs +++ b/TYLDDB/Parser/Parser.cs @@ -8,7 +8,7 @@ namespace TYLDDB.Parser /// Data type parser.
/// 数据类型解析器。 /// - public class Parser + public class DataParser { // 定义正则表达式来匹配键值对(类型::"key"="value") private readonly static string pattern = @"(?<=^|\s)(\w+)\s*::\s*""([^""]+)"""; diff --git a/TYLDDB/TYLDDB.cs b/TYLDDB/TYLDDB.cs index c268361..cdc8d6a 100644 --- a/TYLDDB/TYLDDB.cs +++ b/TYLDDB/TYLDDB.cs @@ -1,8 +1,10 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; +using System.Threading.Tasks; using TYLDDB.Basic; +using TYLDDB.Parser; using TYLDDB.Utils; -using TYLDDB.Utils.FastCache; +using TYLDDB.Utils.FastCache.ConcurrentDictionary; +using TYLDDB.Utils.FastCache.SemaphoreThreadLock; namespace TYLDDB { @@ -27,10 +29,9 @@ public LDDB() private string _database; // 存储正在访问的数据库 private string _databaseContent; // 存储数据库内容 private bool _isRead = false; // 是否已调用读取文件 - private event Action OnFileReadComplete; private Database database = new Database(); - private Cache cdCache = new ConcurrentDictionary(); - private Cache stlCache = new SemaphoreThreadLock(); + private CdStringDictionary cdStringDictionary = new CdStringDictionary(); + private StlStringDictionary StlStringDictionary = new StlStringDictionary(); ///////////////////////////////////////////////////// 公开字段 /// @@ -39,11 +40,16 @@ public LDDB() /// public string FilePath { - get => _filePath; // 获取文件路径 + get + { + return _filePath; // 获取文件路径 + } set { - ValidateFilePath(value); // 在设置值之前进行验证 - _filePath = value; // 只有通过验证后才设置值 + if (ValidateFilePath(value)) // 在设置值之前进行验证 + { + _filePath = value; + } } } /// @@ -57,12 +63,15 @@ public string FilePath /// /// 路径 /// - private static void ValidateFilePath(string path) + /// If true, it can be used, if false, it cannot be used.
如果为true则可以使用,若为false则不可使用。
+ private static bool ValidateFilePath(string path) { if (string.IsNullOrWhiteSpace(path)) { + return false; throw new FilePathIsNullOrWhiteSpace("文件路径不能为 null 或空白"); } + return true; } /// @@ -80,7 +89,7 @@ public void ReadingFile() /// 设置要加载的数据库 /// /// name of the database
数据库名称 - public void LoadDatabase(string db) + public async void LoadDatabase(string db) { switch (_isRead) { @@ -92,6 +101,7 @@ public void LoadDatabase(string db) _databaseContent = database.GetDatabaseContent(_fileContent, db); break; } + await ParseAsync(); } /// @@ -105,5 +115,35 @@ public void LoadDatabase(string db) /// 读取全部数据库的名称 /// public void ReadAllDatabaseName() => AllDatabaseName = database.GetDatabaseList(_fileContent); + + /// + /// Reparse the entire database.
+ /// 重新解析整个数据库。 + ///
+ public async Task ParseAsync() + { + // 创建多个任务,并使用 LongRunning 来确保每个任务在独立线程中运行 + Task cdStringCacheTask = Task.Factory.StartNew(() => CdString(), TaskCreationOptions.LongRunning); + //Task cdIntCacheTask = Task.Factory.StartNew(() => CdInt(), TaskCreationOptions.LongRunning); + //Task cdShortCacheTask = Task.Factory.StartNew(() => CdShort(), TaskCreationOptions.LongRunning); + + // 等待所有任务完成 + await Task.WhenAll(cdStringCacheTask); + + async void CdString() + { + var dict = DataParser.ParseString(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + await cdStringDictionary.SetAsync(key, value); + } + } + } } } diff --git a/TYLDDB/TYLDDB.csproj b/TYLDDB/TYLDDB.csproj index a8a6ed5..b470d6c 100644 --- a/TYLDDB/TYLDDB.csproj +++ b/TYLDDB/TYLDDB.csproj @@ -4,7 +4,7 @@ netstandard2.1;net6.0;net8.0;net9.0 True TYLDDB.NET - 1.0.0-alpha.3 + 1.0.0-alpha.4 TYLDDB-Project LICENSE True diff --git a/TYLDDB/Utils/FastCache/Cache.cs b/TYLDDB/Utils/FastCache/Cache.cs deleted file mode 100644 index dc06ba2..0000000 --- a/TYLDDB/Utils/FastCache/Cache.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; - -namespace TYLDDB.Utils.FastCache -{ - /// - /// For cached abstract classes, you need to inherit the class to do concrete implementation.
- /// 对于缓存的抽象类,需要继承该类来做具体实现。 - ///
- public abstract class Cache : ICache - { - /// - public abstract void Clear(); - /// - public virtual async Task ClearAsync() => await Task.Run(() => Clear()); - /// - public abstract Dictionary GetAllCache(); - /// - public virtual async Task> GetAllCacheAsync() => await Task.FromResult(GetAllCache()); - /// - public abstract string GetByKey(string key); - /// - public virtual async Task GetByKeyAsync(string key) => await Task.FromResult(GetByKey(key)); - /// - public abstract List GetKeysByValue(string value); - /// - public virtual async Task> GetKeysByValueAsync(string value) => await Task.FromResult(GetKeysByValue(value)); - /// - public abstract bool RemoveByKey(string key); - /// - public virtual async Task RemoveByKeyAsync(string key) => await Task.FromResult(RemoveByKey(key)); - /// - public abstract bool Set(string key, string value); - /// - public virtual async Task SetAsync(string key, string value) => await Task.FromResult(Set(key, value)); - } -} diff --git a/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdBooleanDictionary.cs b/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdBooleanDictionary.cs new file mode 100644 index 0000000..9a50a8f --- /dev/null +++ b/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdBooleanDictionary.cs @@ -0,0 +1,124 @@ +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace TYLDDB.Utils.FastCache.ConcurrentDictionary +{ + /// + /// Use concurrent dictionaries to achieve high concurrency stability.
+ /// 使用并发词典来实现高并发的稳定性。 + ///
+ public class CdBooleanDictionary + { + /// + /// Thread-safe dictionary to store cache data.
+ /// 线程安全的字典,用于存储缓存数据。 + ///
+ private readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); + + /// + /// Synchronization method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual bool GetByKey(string key) + { + _cache.TryGetValue(key, out var value); + return value; + } + + /// + /// Asynchronous method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual async Task GetByKeyAsync(string key) => await Task.FromResult(GetByKey(key)); + + /// + /// Get a list of keys that correspond to a specific value.
+ /// 获取与指定值对应的所有键的列表。 + ///
+ /// Value to match
要匹配的值 + /// List of keys
键的列表
+ public virtual List GetKeysByValue(bool value) + { + var keys = new List(); + foreach (var kvp in _cache) + { + if (kvp.Value == value) + { + keys.Add(kvp.Key); + } + } + return keys; + } + + /// + /// Get a list of keys that correspond to a specific value.
+ /// 获取与指定值对应的所有键的列表。 + ///
+ /// Value to match
要匹配的值 + /// List of keys
键的列表
+ public virtual async Task> GetKeysByValueAsync(bool value) => await Task.FromResult(GetKeysByValue(value)); + + /// + /// Set a cache entry for a specified key.
+ /// 为指定键设置缓存项。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the operation is successful.
操作是否成功。
+ public virtual bool Set(string key, bool value) + { + // Using AddOrUpdate to ensure atomic insert or update operation + _cache.AddOrUpdate(key, value, (existingKey, existingValue) => value); + return true; + } + + /// + /// Set a cache entry for a specified key.
+ /// 为指定键设置缓存项。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the operation is successful.
操作是否成功。
+ public virtual async Task SetAsync(string key, bool value) => await Task.FromResult(Set(key, value)); + + /// + /// Remove a cache entry by its key.
+ /// 根据键移除缓存项。 + ///
+ /// Key
键 + /// Whether the removal is successful.
移除操作是否成功。
+ public virtual bool RemoveByKey(string key) => _cache.TryRemove(key, out _); + + /// + /// Remove a cache entry by its key.
+ /// 根据键移除缓存项。 + ///
+ /// Key
键 + /// Whether the removal is successful.
移除操作是否成功。
+ public virtual async Task RemoveByKeyAsync(string key) => await Task.FromResult(RemoveByKey(key)); + + /// + /// Clear all cache entries.
+ /// 清空所有缓存项。 + ///
+ public virtual void Clear() => _cache.Clear(); + + /// + /// Clear all cache entries.
+ /// 清空所有缓存项。 + ///
+ public virtual async Task ClearAsync() => await Task.Run(() => Clear()); + + /// + /// Get all cache entries as a dictionary.
+ /// 获取所有缓存项,返回字典。 + ///
+ /// All cache entries as a dictionary.
所有缓存项的字典。
+ public virtual Dictionary GetAllCache() => new Dictionary(_cache); + } +} diff --git a/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdCharDictionary.cs b/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdCharDictionary.cs new file mode 100644 index 0000000..9e27136 --- /dev/null +++ b/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdCharDictionary.cs @@ -0,0 +1,124 @@ +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace TYLDDB.Utils.FastCache.ConcurrentDictionary +{ + /// + /// Use concurrent dictionaries to achieve high concurrency stability.
+ /// 使用并发词典来实现高并发的稳定性。 + ///
+ public class CdCharDictionary + { + /// + /// Thread-safe dictionary to store cache data.
+ /// 线程安全的字典,用于存储缓存数据。 + ///
+ private readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); + + /// + /// Synchronization method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual char GetByKey(string key) + { + _cache.TryGetValue(key, out var value); + return value; + } + + /// + /// Asynchronous method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual async Task GetByKeyAsync(string key) => await Task.FromResult(GetByKey(key)); + + /// + /// Get a list of keys that correspond to a specific value.
+ /// 获取与指定值对应的所有键的列表。 + ///
+ /// Value to match
要匹配的值 + /// List of keys
键的列表
+ public virtual List GetKeysByValue(char value) + { + var keys = new List(); + foreach (var kvp in _cache) + { + if (kvp.Value == value) + { + keys.Add(kvp.Key); + } + } + return keys; + } + + /// + /// Get a list of keys that correspond to a specific value.
+ /// 获取与指定值对应的所有键的列表。 + ///
+ /// Value to match
要匹配的值 + /// List of keys
键的列表
+ public virtual async Task> GetKeysByValueAsync(char value) => await Task.FromResult(GetKeysByValue(value)); + + /// + /// Set a cache entry for a specified key.
+ /// 为指定键设置缓存项。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the operation is successful.
操作是否成功。
+ public virtual bool Set(string key, char value) + { + // Using AddOrUpdate to ensure atomic insert or update operation + _cache.AddOrUpdate(key, value, (existingKey, existingValue) => value); + return true; + } + + /// + /// Set a cache entry for a specified key.
+ /// 为指定键设置缓存项。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the operation is successful.
操作是否成功。
+ public virtual async Task SetAsync(string key, char value) => await Task.FromResult(Set(key, value)); + + /// + /// Remove a cache entry by its key.
+ /// 根据键移除缓存项。 + ///
+ /// Key
键 + /// Whether the removal is successful.
移除操作是否成功。
+ public virtual bool RemoveByKey(string key) => _cache.TryRemove(key, out _); + + /// + /// Remove a cache entry by its key.
+ /// 根据键移除缓存项。 + ///
+ /// Key
键 + /// Whether the removal is successful.
移除操作是否成功。
+ public virtual async Task RemoveByKeyAsync(string key) => await Task.FromResult(RemoveByKey(key)); + + /// + /// Clear all cache entries.
+ /// 清空所有缓存项。 + ///
+ public virtual void Clear() => _cache.Clear(); + + /// + /// Clear all cache entries.
+ /// 清空所有缓存项。 + ///
+ public virtual async Task ClearAsync() => await Task.Run(() => Clear()); + + /// + /// Get all cache entries as a dictionary.
+ /// 获取所有缓存项,返回字典。 + ///
+ /// All cache entries as a dictionary.
所有缓存项的字典。
+ public virtual Dictionary GetAllCache() => new Dictionary(_cache); + } +} diff --git a/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdDecimalDictionary.cs b/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdDecimalDictionary.cs new file mode 100644 index 0000000..aa0a9d5 --- /dev/null +++ b/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdDecimalDictionary.cs @@ -0,0 +1,124 @@ +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace TYLDDB.Utils.FastCache.ConcurrentDictionary +{ + /// + /// Use concurrent dictionaries to achieve high concurrency stability.
+ /// 使用并发词典来实现高并发的稳定性。 + ///
+ public class CdDecimalDictionary + { + /// + /// Thread-safe dictionary to store cache data.
+ /// 线程安全的字典,用于存储缓存数据。 + ///
+ private readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); + + /// + /// Synchronization method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual decimal GetByKey(string key) + { + _cache.TryGetValue(key, out var value); + return value; + } + + /// + /// Asynchronous method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual async Task GetByKeyAsync(string key) => await Task.FromResult(GetByKey(key)); + + /// + /// Get a list of keys that correspond to a specific value.
+ /// 获取与指定值对应的所有键的列表。 + ///
+ /// Value to match
要匹配的值 + /// List of keys
键的列表
+ public virtual List GetKeysByValue(decimal value) + { + var keys = new List(); + foreach (var kvp in _cache) + { + if (kvp.Value == value) + { + keys.Add(kvp.Key); + } + } + return keys; + } + + /// + /// Get a list of keys that correspond to a specific value.
+ /// 获取与指定值对应的所有键的列表。 + ///
+ /// Value to match
要匹配的值 + /// List of keys
键的列表
+ public virtual async Task> GetKeysByValueAsync(decimal value) => await Task.FromResult(GetKeysByValue(value)); + + /// + /// Set a cache entry for a specified key.
+ /// 为指定键设置缓存项。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the operation is successful.
操作是否成功。
+ public virtual bool Set(string key, decimal value) + { + // Using AddOrUpdate to ensure atomic insert or update operation + _cache.AddOrUpdate(key, value, (existingKey, existingValue) => value); + return true; + } + + /// + /// Set a cache entry for a specified key.
+ /// 为指定键设置缓存项。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the operation is successful.
操作是否成功。
+ public virtual async Task SetAsync(string key, decimal value) => await Task.FromResult(Set(key, value)); + + /// + /// Remove a cache entry by its key.
+ /// 根据键移除缓存项。 + ///
+ /// Key
键 + /// Whether the removal is successful.
移除操作是否成功。
+ public virtual bool RemoveByKey(string key) => _cache.TryRemove(key, out _); + + /// + /// Remove a cache entry by its key.
+ /// 根据键移除缓存项。 + ///
+ /// Key
键 + /// Whether the removal is successful.
移除操作是否成功。
+ public virtual async Task RemoveByKeyAsync(string key) => await Task.FromResult(RemoveByKey(key)); + + /// + /// Clear all cache entries.
+ /// 清空所有缓存项。 + ///
+ public virtual void Clear() => _cache.Clear(); + + /// + /// Clear all cache entries.
+ /// 清空所有缓存项。 + ///
+ public virtual async Task ClearAsync() => await Task.Run(() => Clear()); + + /// + /// Get all cache entries as a dictionary.
+ /// 获取所有缓存项,返回字典。 + ///
+ /// All cache entries as a dictionary.
所有缓存项的字典。
+ public virtual Dictionary GetAllCache() => new Dictionary(_cache); + } +} diff --git a/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdDoubleDictionary.cs b/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdDoubleDictionary.cs new file mode 100644 index 0000000..c2c09e7 --- /dev/null +++ b/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdDoubleDictionary.cs @@ -0,0 +1,124 @@ +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace TYLDDB.Utils.FastCache.ConcurrentDictionary +{ + /// + /// Use concurrent dictionaries to achieve high concurrency stability.
+ /// 使用并发词典来实现高并发的稳定性。 + ///
+ public class CdDoubleDictionary + { + /// + /// Thread-safe dictionary to store cache data.
+ /// 线程安全的字典,用于存储缓存数据。 + ///
+ private readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); + + /// + /// Synchronization method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual double GetByKey(string key) + { + _cache.TryGetValue(key, out var value); + return value; + } + + /// + /// Asynchronous method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual async Task GetByKeyAsync(string key) => await Task.FromResult(GetByKey(key)); + + /// + /// Get a list of keys that correspond to a specific value.
+ /// 获取与指定值对应的所有键的列表。 + ///
+ /// Value to match
要匹配的值 + /// List of keys
键的列表
+ public virtual List GetKeysByValue(double value) + { + var keys = new List(); + foreach (var kvp in _cache) + { + if (kvp.Value == value) + { + keys.Add(kvp.Key); + } + } + return keys; + } + + /// + /// Get a list of keys that correspond to a specific value.
+ /// 获取与指定值对应的所有键的列表。 + ///
+ /// Value to match
要匹配的值 + /// List of keys
键的列表
+ public virtual async Task> GetKeysByValueAsync(double value) => await Task.FromResult(GetKeysByValue(value)); + + /// + /// Set a cache entry for a specified key.
+ /// 为指定键设置缓存项。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the operation is successful.
操作是否成功。
+ public virtual bool Set(string key, double value) + { + // Using AddOrUpdate to ensure atomic insert or update operation + _cache.AddOrUpdate(key, value, (existingKey, existingValue) => value); + return true; + } + + /// + /// Set a cache entry for a specified key.
+ /// 为指定键设置缓存项。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the operation is successful.
操作是否成功。
+ public virtual async Task SetAsync(string key, double value) => await Task.FromResult(Set(key, value)); + + /// + /// Remove a cache entry by its key.
+ /// 根据键移除缓存项。 + ///
+ /// Key
键 + /// Whether the removal is successful.
移除操作是否成功。
+ public virtual bool RemoveByKey(string key) => _cache.TryRemove(key, out _); + + /// + /// Remove a cache entry by its key.
+ /// 根据键移除缓存项。 + ///
+ /// Key
键 + /// Whether the removal is successful.
移除操作是否成功。
+ public virtual async Task RemoveByKeyAsync(string key) => await Task.FromResult(RemoveByKey(key)); + + /// + /// Clear all cache entries.
+ /// 清空所有缓存项。 + ///
+ public virtual void Clear() => _cache.Clear(); + + /// + /// Clear all cache entries.
+ /// 清空所有缓存项。 + ///
+ public virtual async Task ClearAsync() => await Task.Run(() => Clear()); + + /// + /// Get all cache entries as a dictionary.
+ /// 获取所有缓存项,返回字典。 + ///
+ /// All cache entries as a dictionary.
所有缓存项的字典。
+ public virtual Dictionary GetAllCache() => new Dictionary(_cache); + } +} diff --git a/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdFloatDictionary.cs b/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdFloatDictionary.cs new file mode 100644 index 0000000..bed0e42 --- /dev/null +++ b/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdFloatDictionary.cs @@ -0,0 +1,124 @@ +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace TYLDDB.Utils.FastCache.ConcurrentDictionary +{ + /// + /// Use concurrent dictionaries to achieve high concurrency stability.
+ /// 使用并发词典来实现高并发的稳定性。 + ///
+ public class CdFloatDictionary + { + /// + /// Thread-safe dictionary to store cache data.
+ /// 线程安全的字典,用于存储缓存数据。 + ///
+ private readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); + + /// + /// Synchronization method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual float GetByKey(string key) + { + _cache.TryGetValue(key, out var value); + return value; + } + + /// + /// Asynchronous method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual async Task GetByKeyAsync(string key) => await Task.FromResult(GetByKey(key)); + + /// + /// Get a list of keys that correspond to a specific value.
+ /// 获取与指定值对应的所有键的列表。 + ///
+ /// Value to match
要匹配的值 + /// List of keys
键的列表
+ public virtual List GetKeysByValue(float value) + { + var keys = new List(); + foreach (var kvp in _cache) + { + if (kvp.Value == value) + { + keys.Add(kvp.Key); + } + } + return keys; + } + + /// + /// Get a list of keys that correspond to a specific value.
+ /// 获取与指定值对应的所有键的列表。 + ///
+ /// Value to match
要匹配的值 + /// List of keys
键的列表
+ public virtual async Task> GetKeysByValueAsync(float value) => await Task.FromResult(GetKeysByValue(value)); + + /// + /// Set a cache entry for a specified key.
+ /// 为指定键设置缓存项。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the operation is successful.
操作是否成功。
+ public virtual bool Set(string key, float value) + { + // Using AddOrUpdate to ensure atomic insert or update operation + _cache.AddOrUpdate(key, value, (existingKey, existingValue) => value); + return true; + } + + /// + /// Set a cache entry for a specified key.
+ /// 为指定键设置缓存项。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the operation is successful.
操作是否成功。
+ public virtual async Task SetAsync(string key, float value) => await Task.FromResult(Set(key, value)); + + /// + /// Remove a cache entry by its key.
+ /// 根据键移除缓存项。 + ///
+ /// Key
键 + /// Whether the removal is successful.
移除操作是否成功。
+ public virtual bool RemoveByKey(string key) => _cache.TryRemove(key, out _); + + /// + /// Remove a cache entry by its key.
+ /// 根据键移除缓存项。 + ///
+ /// Key
键 + /// Whether the removal is successful.
移除操作是否成功。
+ public virtual async Task RemoveByKeyAsync(string key) => await Task.FromResult(RemoveByKey(key)); + + /// + /// Clear all cache entries.
+ /// 清空所有缓存项。 + ///
+ public virtual void Clear() => _cache.Clear(); + + /// + /// Clear all cache entries.
+ /// 清空所有缓存项。 + ///
+ public virtual async Task ClearAsync() => await Task.Run(() => Clear()); + + /// + /// Get all cache entries as a dictionary.
+ /// 获取所有缓存项,返回字典。 + ///
+ /// All cache entries as a dictionary.
所有缓存项的字典。
+ public virtual Dictionary GetAllCache() => new Dictionary(_cache); + } +} diff --git a/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdIntegerDictionary.cs b/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdIntegerDictionary.cs new file mode 100644 index 0000000..feb3f2f --- /dev/null +++ b/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdIntegerDictionary.cs @@ -0,0 +1,124 @@ +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace TYLDDB.Utils.FastCache.ConcurrentDictionary +{ + /// + /// Use concurrent dictionaries to achieve high concurrency stability.
+ /// 使用并发词典来实现高并发的稳定性。 + ///
+ public class CdIntegerDictionary + { + /// + /// Thread-safe dictionary to store cache data.
+ /// 线程安全的字典,用于存储缓存数据。 + ///
+ private readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); + + /// + /// Synchronization method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual int GetByKey(string key) + { + _cache.TryGetValue(key, out var value); + return value; + } + + /// + /// Asynchronous method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual async Task GetByKeyAsync(string key) => await Task.FromResult(GetByKey(key)); + + /// + /// Get a list of keys that correspond to a specific value.
+ /// 获取与指定值对应的所有键的列表。 + ///
+ /// Value to match
要匹配的值 + /// List of keys
键的列表
+ public virtual List GetKeysByValue(int value) + { + var keys = new List(); + foreach (var kvp in _cache) + { + if (kvp.Value == value) + { + keys.Add(kvp.Key); + } + } + return keys; + } + + /// + /// Get a list of keys that correspond to a specific value.
+ /// 获取与指定值对应的所有键的列表。 + ///
+ /// Value to match
要匹配的值 + /// List of keys
键的列表
+ public virtual async Task> GetKeysByValueAsync(int value) => await Task.FromResult(GetKeysByValue(value)); + + /// + /// Set a cache entry for a specified key.
+ /// 为指定键设置缓存项。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the operation is successful.
操作是否成功。
+ public virtual bool Set(string key, int value) + { + // Using AddOrUpdate to ensure atomic insert or update operation + _cache.AddOrUpdate(key, value, (existingKey, existingValue) => value); + return true; + } + + /// + /// Set a cache entry for a specified key.
+ /// 为指定键设置缓存项。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the operation is successful.
操作是否成功。
+ public virtual async Task SetAsync(string key, int value) => await Task.FromResult(Set(key, value)); + + /// + /// Remove a cache entry by its key.
+ /// 根据键移除缓存项。 + ///
+ /// Key
键 + /// Whether the removal is successful.
移除操作是否成功。
+ public virtual bool RemoveByKey(string key) => _cache.TryRemove(key, out _); + + /// + /// Remove a cache entry by its key.
+ /// 根据键移除缓存项。 + ///
+ /// Key
键 + /// Whether the removal is successful.
移除操作是否成功。
+ public virtual async Task RemoveByKeyAsync(string key) => await Task.FromResult(RemoveByKey(key)); + + /// + /// Clear all cache entries.
+ /// 清空所有缓存项。 + ///
+ public virtual void Clear() => _cache.Clear(); + + /// + /// Clear all cache entries.
+ /// 清空所有缓存项。 + ///
+ public virtual async Task ClearAsync() => await Task.Run(() => Clear()); + + /// + /// Get all cache entries as a dictionary.
+ /// 获取所有缓存项,返回字典。 + ///
+ /// All cache entries as a dictionary.
所有缓存项的字典。
+ public virtual Dictionary GetAllCache() => new Dictionary(_cache); + } +} diff --git a/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdLongDictionary.cs b/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdLongDictionary.cs new file mode 100644 index 0000000..43656d6 --- /dev/null +++ b/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdLongDictionary.cs @@ -0,0 +1,124 @@ +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace TYLDDB.Utils.FastCache.ConcurrentDictionary +{ + /// + /// Use concurrent dictionaries to achieve high concurrency stability.
+ /// 使用并发词典来实现高并发的稳定性。 + ///
+ public class CdLongDictionary + { + /// + /// Thread-safe dictionary to store cache data.
+ /// 线程安全的字典,用于存储缓存数据。 + ///
+ private readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); + + /// + /// Synchronization method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual long GetByKey(string key) + { + _cache.TryGetValue(key, out var value); + return value; + } + + /// + /// Asynchronous method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual async Task GetByKeyAsync(string key) => await Task.FromResult(GetByKey(key)); + + /// + /// Get a list of keys that correspond to a specific value.
+ /// 获取与指定值对应的所有键的列表。 + ///
+ /// Value to match
要匹配的值 + /// List of keys
键的列表
+ public virtual List GetKeysByValue(long value) + { + var keys = new List(); + foreach (var kvp in _cache) + { + if (kvp.Value == value) + { + keys.Add(kvp.Key); + } + } + return keys; + } + + /// + /// Get a list of keys that correspond to a specific value.
+ /// 获取与指定值对应的所有键的列表。 + ///
+ /// Value to match
要匹配的值 + /// List of keys
键的列表
+ public virtual async Task> GetKeysByValueAsync(long value) => await Task.FromResult(GetKeysByValue(value)); + + /// + /// Set a cache entry for a specified key.
+ /// 为指定键设置缓存项。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the operation is successful.
操作是否成功。
+ public virtual bool Set(string key, long value) + { + // Using AddOrUpdate to ensure atomic insert or update operation + _cache.AddOrUpdate(key, value, (existingKey, existingValue) => value); + return true; + } + + /// + /// Set a cache entry for a specified key.
+ /// 为指定键设置缓存项。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the operation is successful.
操作是否成功。
+ public virtual async Task SetAsync(string key, long value) => await Task.FromResult(Set(key, value)); + + /// + /// Remove a cache entry by its key.
+ /// 根据键移除缓存项。 + ///
+ /// Key
键 + /// Whether the removal is successful.
移除操作是否成功。
+ public virtual bool RemoveByKey(string key) => _cache.TryRemove(key, out _); + + /// + /// Remove a cache entry by its key.
+ /// 根据键移除缓存项。 + ///
+ /// Key
键 + /// Whether the removal is successful.
移除操作是否成功。
+ public virtual async Task RemoveByKeyAsync(string key) => await Task.FromResult(RemoveByKey(key)); + + /// + /// Clear all cache entries.
+ /// 清空所有缓存项。 + ///
+ public virtual void Clear() => _cache.Clear(); + + /// + /// Clear all cache entries.
+ /// 清空所有缓存项。 + ///
+ public virtual async Task ClearAsync() => await Task.Run(() => Clear()); + + /// + /// Get all cache entries as a dictionary.
+ /// 获取所有缓存项,返回字典。 + ///
+ /// All cache entries as a dictionary.
所有缓存项的字典。
+ public virtual Dictionary GetAllCache() => new Dictionary(_cache); + } +} diff --git a/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdShortDictionary.cs b/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdShortDictionary.cs new file mode 100644 index 0000000..8ad8464 --- /dev/null +++ b/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdShortDictionary.cs @@ -0,0 +1,124 @@ +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace TYLDDB.Utils.FastCache.ConcurrentDictionary +{ + /// + /// Use concurrent dictionaries to achieve high concurrency stability.
+ /// 使用并发词典来实现高并发的稳定性。 + ///
+ public class CdShortDictionary + { + /// + /// Thread-safe dictionary to store cache data.
+ /// 线程安全的字典,用于存储缓存数据。 + ///
+ private readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); + + /// + /// Synchronization method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual short GetByKey(string key) + { + _cache.TryGetValue(key, out var value); + return value; + } + + /// + /// Asynchronous method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual async Task GetByKeyAsync(string key) => await Task.FromResult(GetByKey(key)); + + /// + /// Get a list of keys that correspond to a specific value.
+ /// 获取与指定值对应的所有键的列表。 + ///
+ /// Value to match
要匹配的值 + /// List of keys
键的列表
+ public virtual List GetKeysByValue(short value) + { + var keys = new List(); + foreach (var kvp in _cache) + { + if (kvp.Value == value) + { + keys.Add(kvp.Key); + } + } + return keys; + } + + /// + /// Get a list of keys that correspond to a specific value.
+ /// 获取与指定值对应的所有键的列表。 + ///
+ /// Value to match
要匹配的值 + /// List of keys
键的列表
+ public virtual async Task> GetKeysByValueAsync(short value) => await Task.FromResult(GetKeysByValue(value)); + + /// + /// Set a cache entry for a specified key.
+ /// 为指定键设置缓存项。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the operation is successful.
操作是否成功。
+ public virtual bool Set(string key, short value) + { + // Using AddOrUpdate to ensure atomic insert or update operation + _cache.AddOrUpdate(key, value, (existingKey, existingValue) => value); + return true; + } + + /// + /// Set a cache entry for a specified key.
+ /// 为指定键设置缓存项。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the operation is successful.
操作是否成功。
+ public virtual async Task SetAsync(string key, short value) => await Task.FromResult(Set(key, value)); + + /// + /// Remove a cache entry by its key.
+ /// 根据键移除缓存项。 + ///
+ /// Key
键 + /// Whether the removal is successful.
移除操作是否成功。
+ public virtual bool RemoveByKey(string key) => _cache.TryRemove(key, out _); + + /// + /// Remove a cache entry by its key.
+ /// 根据键移除缓存项。 + ///
+ /// Key
键 + /// Whether the removal is successful.
移除操作是否成功。
+ public virtual async Task RemoveByKeyAsync(string key) => await Task.FromResult(RemoveByKey(key)); + + /// + /// Clear all cache entries.
+ /// 清空所有缓存项。 + ///
+ public virtual void Clear() => _cache.Clear(); + + /// + /// Clear all cache entries.
+ /// 清空所有缓存项。 + ///
+ public virtual async Task ClearAsync() => await Task.Run(() => Clear()); + + /// + /// Get all cache entries as a dictionary.
+ /// 获取所有缓存项,返回字典。 + ///
+ /// All cache entries as a dictionary.
所有缓存项的字典。
+ public virtual Dictionary GetAllCache() => new Dictionary(_cache); + } +} diff --git a/TYLDDB/Utils/FastCache/ConcurrentDictionary.cs b/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdStringDictionary.cs similarity index 50% rename from TYLDDB/Utils/FastCache/ConcurrentDictionary.cs rename to TYLDDB/Utils/FastCache/ConcurrentDictionary/CdStringDictionary.cs index ba98a93..a859eae 100644 --- a/TYLDDB/Utils/FastCache/ConcurrentDictionary.cs +++ b/TYLDDB/Utils/FastCache/ConcurrentDictionary/CdStringDictionary.cs @@ -1,13 +1,14 @@ using System.Collections.Concurrent; using System.Collections.Generic; +using System.Threading.Tasks; -namespace TYLDDB.Utils.FastCache +namespace TYLDDB.Utils.FastCache.ConcurrentDictionary { /// - /// Use thread locks based on concurrent dictionaries to achieve high concurrency stability.
- /// 使用基于信号量的线程锁来实现高并发的稳定性。 + /// Use concurrent dictionaries to achieve high concurrency stability.
+ /// 使用并发词典来实现高并发的稳定性。 ///
- public class ConcurrentDictionary : Cache + public class CdStringDictionary { /// /// Thread-safe dictionary to store cache data.
@@ -21,19 +22,27 @@ public class ConcurrentDictionary : Cache ///
/// Key
键 /// Value
- public override string GetByKey(string key) + public virtual string GetByKey(string key) { _cache.TryGetValue(key, out var value); return value; } + /// + /// Asynchronous method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual async Task GetByKeyAsync(string key) => await Task.FromResult(GetByKey(key)); + /// /// Get a list of keys that correspond to a specific value.
/// 获取与指定值对应的所有键的列表。 ///
/// Value to match
要匹配的值 /// List of keys
键的列表
- public override List GetKeysByValue(string value) + public virtual List GetKeysByValue(string value) { var keys = new List(); foreach (var kvp in _cache) @@ -46,6 +55,14 @@ public override List GetKeysByValue(string value) return keys; } + /// + /// Get a list of keys that correspond to a specific value.
+ /// 获取与指定值对应的所有键的列表。 + ///
+ /// Value to match
要匹配的值 + /// List of keys
键的列表
+ public virtual async Task> GetKeysByValueAsync(string value) => await Task.FromResult(GetKeysByValue(value)); + /// /// Set a cache entry for a specified key.
/// 为指定键设置缓存项。 @@ -53,35 +70,55 @@ public override List GetKeysByValue(string value) /// Key
键 /// Value
值 /// Whether the operation is successful.
操作是否成功。
- public override bool Set(string key, string value) + public virtual bool Set(string key, string value) { // Using AddOrUpdate to ensure atomic insert or update operation _cache.AddOrUpdate(key, value, (existingKey, existingValue) => value); return true; } + /// + /// Set a cache entry for a specified key.
+ /// 为指定键设置缓存项。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the operation is successful.
操作是否成功。
+ public virtual async Task SetAsync(string key, string value) => await Task.FromResult(Set(key, value)); + /// /// Remove a cache entry by its key.
/// 根据键移除缓存项。 ///
/// Key
键 /// Whether the removal is successful.
移除操作是否成功。
- public override bool RemoveByKey(string key) - { - return _cache.TryRemove(key, out _); - } + public virtual bool RemoveByKey(string key) => _cache.TryRemove(key, out _); + + /// + /// Remove a cache entry by its key.
+ /// 根据键移除缓存项。 + ///
+ /// Key
键 + /// Whether the removal is successful.
移除操作是否成功。
+ public virtual async Task RemoveByKeyAsync(string key) => await Task.FromResult(RemoveByKey(key)); + + /// + /// Clear all cache entries.
+ /// 清空所有缓存项。 + ///
+ public virtual void Clear() => _cache.Clear(); /// /// Clear all cache entries.
/// 清空所有缓存项。 ///
- public override void Clear() => _cache.Clear(); + public virtual async Task ClearAsync() => await Task.Run(() => Clear()); /// /// Get all cache entries as a dictionary.
/// 获取所有缓存项,返回字典。 ///
/// All cache entries as a dictionary.
所有缓存项的字典。
- public override Dictionary GetAllCache() => new Dictionary(_cache); + public virtual Dictionary GetAllCache() => new Dictionary(_cache); } } diff --git a/TYLDDB/Utils/FastCache/ICache.cs b/TYLDDB/Utils/FastCache/ICache.cs deleted file mode 100644 index ea3ecce..0000000 --- a/TYLDDB/Utils/FastCache/ICache.cs +++ /dev/null @@ -1,104 +0,0 @@ -using System.Collections.Generic; -using System.Threading.Tasks; - -namespace TYLDDB.Utils.FastCache -{ - /// - /// Use cached key-value pairs for fast reads and writes.
- /// 使用缓存的键值对来快速读写。 - ///
- public interface ICache - { - /// - /// Synchronization method: Obtain the corresponding value by key.
- /// 同步方法:根据键获取对应的值。 - ///
- /// Key
键 - /// Value
- string GetByKey(string key); - - /// - /// Asynchronous method: Obtains the corresponding value based on the key.
- /// 异步方法:根据键获取对应的值。 - ///
- /// Key
键 - /// Value
- Task GetByKeyAsync(string key); - - /// - /// Synchronization method: Obtains one or more keys according to the value.
- /// 同步方法:根据值获取对应的一个或多个键。 - ///
- /// Value
值 - /// Key (List)
键 (List)
- List GetKeysByValue(string value); - - /// - /// Asynchronous method: Get one or more keys based on the value.
- /// 异步方法:根据值获取对应的一个或多个键。 - ///
- /// Value
值 - /// Key (List)
键 (List)
- Task> GetKeysByValueAsync(string value); - - /// - /// Set a key-value pair.
- /// 设置一个键值对。 - ///
- /// Key
键 - /// Value
值 - /// Whether the Settings are successful.
是否设置成功。
- bool Set(string key, string value); - - /// - /// Set a key-value pair.
- /// 设置一个键值对。 - ///
- /// Key
键 - /// Value
值 - /// Whether the Settings are successful.
是否设置成功。
- Task SetAsync(string key, string value); - - /// - /// Remove a key-value pair.
- /// 移除一个键值对。 - ///
- /// Key
键 - /// Whether the removal was successful or not.
是否移除成功。
- bool RemoveByKey(string key); - - /// - /// Remove a key-value pair.
- /// 移除一个键值对。 - ///
- /// Key
键 - /// Whether the removal was successful or not.
是否移除成功。
- Task RemoveByKeyAsync(string key); - - /// - /// Clear the cache.
- /// 清空缓存。 - ///
- void Clear(); - - /// - /// Clear the cache.
- /// 清空缓存。 - ///
- Task ClearAsync(); - - /// - /// Gets all key-value pairs.
- /// 获取所有的键值对。 - ///
- /// Key-value pair
键值对
- Dictionary GetAllCache(); - - /// - /// Gets all key-value pairs.
- /// 获取所有的键值对。 - ///
- /// Key-value pair
键值对
- Task> GetAllCacheAsync(); - } -} diff --git a/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlBooleanDictionary.cs b/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlBooleanDictionary.cs new file mode 100644 index 0000000..6e41075 --- /dev/null +++ b/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlBooleanDictionary.cs @@ -0,0 +1,291 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace TYLDDB.Utils.FastCache.SemaphoreThreadLock +{ + /// + /// Use semaphore based thread locks to achieve high concurrency stability.
+ /// 使用基于信号量的线程锁来实现高并发的稳定性。 + ///
+ public class StlBooleanDictionary + { + private readonly Dictionary keyValueDict; // 存储键->值映射 + private readonly Dictionary> valueKeyDict; // 存储值->键的映射 + private readonly SemaphoreSlim semaphore; // 控制并发访问 + + /// + /// Use cached key-value pairs for fast reads and writes.
+ /// 使用缓存的键值对来快速读写。 + ///
+ public StlBooleanDictionary() + { + keyValueDict = new Dictionary(); + valueKeyDict = new Dictionary>(); + semaphore = new SemaphoreSlim(1, 1); // 使用信号量来同步 + } + + /// + /// Synchronization method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual bool GetByKey(string key) + { + lock (semaphore) + { + keyValueDict.TryGetValue(key, out var value); + return value; + } + } + + /// + /// Asynchronous method: Obtains the corresponding value based on the key.
+ /// 异步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual async Task GetByKeyAsync(string key) + { + await semaphore.WaitAsync(); + try + { + keyValueDict.TryGetValue(key, out var value); + return value; + } + finally + { + semaphore.Release(); + } + } + + /// + /// Synchronization method: Obtains one or more keys according to the value.
+ /// 同步方法:根据值获取对应的一个或多个键。 + ///
+ /// Value
值 + /// Key (List)
键 (List)
+ public virtual List GetKeysByValue(bool value) + { + lock (semaphore) + { + if (valueKeyDict.ContainsKey(value)) + { + return valueKeyDict[value].ToList(); + } + return new List(); + } + } + + /// + /// Asynchronous method: Get one or more keys based on the value.
+ /// 异步方法:根据值获取对应的一个或多个键。 + ///
+ /// Value
值 + /// Key (List)
键 (List)
+ public virtual async Task> GetKeysByValueAsync(bool value) + { + await semaphore.WaitAsync(); + try + { + if (valueKeyDict.ContainsKey(value)) + { + return valueKeyDict[value].ToList(); + } + return new List(); + } + finally + { + semaphore.Release(); + } + } + + /// + /// Set a key-value pair.
+ /// 设置一个键值对。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the Settings are successful.
是否设置成功
+ public virtual bool Set(string key, bool value) + { + lock (semaphore) + { + if (keyValueDict.ContainsKey(key)) + { + return false; // 键已存在,不允许重复的键 + } + + keyValueDict[key] = value; + + if (!valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value] = new HashSet(); + } + valueKeyDict[value].Add(key); + return true; + } + } + + /// + /// Set a key-value pair.
+ /// 设置一个键值对。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the Settings are successful.
是否设置成功
+ public virtual async Task SetAsync(string key, bool value) + { + await semaphore.WaitAsync(); + try + { + if (keyValueDict.ContainsKey(key)) + { + return false; // 键已存在,不允许重复的键 + } + + keyValueDict[key] = value; + + if (!valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value] = new HashSet(); + } + valueKeyDict[value].Add(key); + return true; + } + finally + { + semaphore.Release(); + } + } + + /// + /// Remove a key-value pair.
+ /// 移除一个键值对。 + ///
+ /// Key
键 + /// Check whether the removal is successful.
是否移除成功。
+ public virtual bool RemoveByKey(string key) + { + lock (semaphore) + { + if (!keyValueDict.ContainsKey(key)) + { + return false; + } + var value = keyValueDict[key]; + keyValueDict.Remove(key); + + if (valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value].Remove(key); + if (valueKeyDict[value].Count == 0) + { + valueKeyDict.Remove(value); + } + } + return true; + } + } + + /// + /// Remove a key-value pair.
+ /// 移除一个键值对。 + ///
+ /// Key
键 + /// Check whether the removal is successful.
是否移除成功。
+ public virtual async Task RemoveByKeyAsync(string key) + { + await semaphore.WaitAsync(); + try + { + if (!keyValueDict.ContainsKey(key)) + { + return false; + } + var value = keyValueDict[key]; + keyValueDict.Remove(key); + + if (valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value].Remove(key); + if (valueKeyDict[value].Count == 0) + { + valueKeyDict.Remove(value); + } + } + return true; + } + finally + { + semaphore.Release(); + } + } + + /// + /// Empty the cache.
+ /// 清空缓存。 + ///
+ public virtual void Clear() + { + lock (semaphore) + { + keyValueDict.Clear(); + valueKeyDict.Clear(); + } + } + + /// + /// Empty the cache.
+ /// 清空缓存。 + ///
+ public virtual async void ClearAsync() + { + await semaphore.WaitAsync(); + try + { + keyValueDict.Clear(); + valueKeyDict.Clear(); + } + finally + { + semaphore.Release(); + } + } + + /// + /// Gets all key-value pairs.
+ /// 获取所有的键值对。 + ///
+ /// Key-value pair
键值对
+ public virtual Dictionary GetAllCache() + { + lock (semaphore) + { + // 返回完整的键值对字典 + return new Dictionary(keyValueDict); + } + } + + /// + /// Gets all key-value pairs.
+ /// 获取所有的键值对。 + ///
+ /// Key-value pair
键值对
+ public virtual async Task> GetAllCacheAsync() + { + await semaphore.WaitAsync(); + try + { + // 返回完整的键值对字典 + return new Dictionary(keyValueDict); + } + finally + { + semaphore.Release(); + } + } + } +} diff --git a/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlCharDictionary.cs b/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlCharDictionary.cs new file mode 100644 index 0000000..c2695e1 --- /dev/null +++ b/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlCharDictionary.cs @@ -0,0 +1,291 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace TYLDDB.Utils.FastCache.SemaphoreThreadLock +{ + /// + /// Use semaphore based thread locks to achieve high concurrency stability.
+ /// 使用基于信号量的线程锁来实现高并发的稳定性。 + ///
+ public class StlCharDictionary + { + private readonly Dictionary keyValueDict; // 存储键->值映射 + private readonly Dictionary> valueKeyDict; // 存储值->键的映射 + private readonly SemaphoreSlim semaphore; // 控制并发访问 + + /// + /// Use cached key-value pairs for fast reads and writes.
+ /// 使用缓存的键值对来快速读写。 + ///
+ public StlCharDictionary() + { + keyValueDict = new Dictionary(); + valueKeyDict = new Dictionary>(); + semaphore = new SemaphoreSlim(1, 1); // 使用信号量来同步 + } + + /// + /// Synchronization method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual char GetByKey(string key) + { + lock (semaphore) + { + keyValueDict.TryGetValue(key, out var value); + return value; + } + } + + /// + /// Asynchronous method: Obtains the corresponding value based on the key.
+ /// 异步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual async Task GetByKeyAsync(string key) + { + await semaphore.WaitAsync(); + try + { + keyValueDict.TryGetValue(key, out var value); + return value; + } + finally + { + semaphore.Release(); + } + } + + /// + /// Synchronization method: Obtains one or more keys according to the value.
+ /// 同步方法:根据值获取对应的一个或多个键。 + ///
+ /// Value
值 + /// Key (List)
键 (List)
+ public virtual List GetKeysByValue(char value) + { + lock (semaphore) + { + if (valueKeyDict.ContainsKey(value)) + { + return valueKeyDict[value].ToList(); + } + return new List(); + } + } + + /// + /// Asynchronous method: Get one or more keys based on the value.
+ /// 异步方法:根据值获取对应的一个或多个键。 + ///
+ /// Value
值 + /// Key (List)
键 (List)
+ public virtual async Task> GetKeysByValueAsync(char value) + { + await semaphore.WaitAsync(); + try + { + if (valueKeyDict.ContainsKey(value)) + { + return valueKeyDict[value].ToList(); + } + return new List(); + } + finally + { + semaphore.Release(); + } + } + + /// + /// Set a key-value pair.
+ /// 设置一个键值对。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the Settings are successful.
是否设置成功
+ public virtual bool Set(string key, char value) + { + lock (semaphore) + { + if (keyValueDict.ContainsKey(key)) + { + return false; // 键已存在,不允许重复的键 + } + + keyValueDict[key] = value; + + if (!valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value] = new HashSet(); + } + valueKeyDict[value].Add(key); + return true; + } + } + + /// + /// Set a key-value pair.
+ /// 设置一个键值对。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the Settings are successful.
是否设置成功
+ public virtual async Task SetAsync(string key, char value) + { + await semaphore.WaitAsync(); + try + { + if (keyValueDict.ContainsKey(key)) + { + return false; // 键已存在,不允许重复的键 + } + + keyValueDict[key] = value; + + if (!valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value] = new HashSet(); + } + valueKeyDict[value].Add(key); + return true; + } + finally + { + semaphore.Release(); + } + } + + /// + /// Remove a key-value pair.
+ /// 移除一个键值对。 + ///
+ /// Key
键 + /// Check whether the removal is successful.
是否移除成功。
+ public virtual bool RemoveByKey(string key) + { + lock (semaphore) + { + if (!keyValueDict.ContainsKey(key)) + { + return false; + } + var value = keyValueDict[key]; + keyValueDict.Remove(key); + + if (valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value].Remove(key); + if (valueKeyDict[value].Count == 0) + { + valueKeyDict.Remove(value); + } + } + return true; + } + } + + /// + /// Remove a key-value pair.
+ /// 移除一个键值对。 + ///
+ /// Key
键 + /// Check whether the removal is successful.
是否移除成功。
+ public virtual async Task RemoveByKeyAsync(string key) + { + await semaphore.WaitAsync(); + try + { + if (!keyValueDict.ContainsKey(key)) + { + return false; + } + var value = keyValueDict[key]; + keyValueDict.Remove(key); + + if (valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value].Remove(key); + if (valueKeyDict[value].Count == 0) + { + valueKeyDict.Remove(value); + } + } + return true; + } + finally + { + semaphore.Release(); + } + } + + /// + /// Empty the cache.
+ /// 清空缓存。 + ///
+ public virtual void Clear() + { + lock (semaphore) + { + keyValueDict.Clear(); + valueKeyDict.Clear(); + } + } + + /// + /// Empty the cache.
+ /// 清空缓存。 + ///
+ public virtual async void ClearAsync() + { + await semaphore.WaitAsync(); + try + { + keyValueDict.Clear(); + valueKeyDict.Clear(); + } + finally + { + semaphore.Release(); + } + } + + /// + /// Gets all key-value pairs.
+ /// 获取所有的键值对。 + ///
+ /// Key-value pair
键值对
+ public virtual Dictionary GetAllCache() + { + lock (semaphore) + { + // 返回完整的键值对字典 + return new Dictionary(keyValueDict); + } + } + + /// + /// Gets all key-value pairs.
+ /// 获取所有的键值对。 + ///
+ /// Key-value pair
键值对
+ public virtual async Task> GetAllCacheAsync() + { + await semaphore.WaitAsync(); + try + { + // 返回完整的键值对字典 + return new Dictionary(keyValueDict); + } + finally + { + semaphore.Release(); + } + } + } +} diff --git a/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlDecimalDictionary.cs b/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlDecimalDictionary.cs new file mode 100644 index 0000000..ebd14bd --- /dev/null +++ b/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlDecimalDictionary.cs @@ -0,0 +1,291 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace TYLDDB.Utils.FastCache.SemaphoreThreadLock +{ + /// + /// Use semaphore based thread locks to achieve high concurrency stability.
+ /// 使用基于信号量的线程锁来实现高并发的稳定性。 + ///
+ public class StlDecimalDictionary + { + private readonly Dictionary keyValueDict; // 存储键->值映射 + private readonly Dictionary> valueKeyDict; // 存储值->键的映射 + private readonly SemaphoreSlim semaphore; // 控制并发访问 + + /// + /// Use cached key-value pairs for fast reads and writes.
+ /// 使用缓存的键值对来快速读写。 + ///
+ public StlDecimalDictionary() + { + keyValueDict = new Dictionary(); + valueKeyDict = new Dictionary>(); + semaphore = new SemaphoreSlim(1, 1); // 使用信号量来同步 + } + + /// + /// Synchronization method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual decimal GetByKey(string key) + { + lock (semaphore) + { + keyValueDict.TryGetValue(key, out var value); + return value; + } + } + + /// + /// Asynchronous method: Obtains the corresponding value based on the key.
+ /// 异步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual async Task GetByKeyAsync(string key) + { + await semaphore.WaitAsync(); + try + { + keyValueDict.TryGetValue(key, out var value); + return value; + } + finally + { + semaphore.Release(); + } + } + + /// + /// Synchronization method: Obtains one or more keys according to the value.
+ /// 同步方法:根据值获取对应的一个或多个键。 + ///
+ /// Value
值 + /// Key (List)
键 (List)
+ public virtual List GetKeysByValue(decimal value) + { + lock (semaphore) + { + if (valueKeyDict.ContainsKey(value)) + { + return valueKeyDict[value].ToList(); + } + return new List(); + } + } + + /// + /// Asynchronous method: Get one or more keys based on the value.
+ /// 异步方法:根据值获取对应的一个或多个键。 + ///
+ /// Value
值 + /// Key (List)
键 (List)
+ public virtual async Task> GetKeysByValueAsync(decimal value) + { + await semaphore.WaitAsync(); + try + { + if (valueKeyDict.ContainsKey(value)) + { + return valueKeyDict[value].ToList(); + } + return new List(); + } + finally + { + semaphore.Release(); + } + } + + /// + /// Set a key-value pair.
+ /// 设置一个键值对。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the Settings are successful.
是否设置成功
+ public virtual bool Set(string key, decimal value) + { + lock (semaphore) + { + if (keyValueDict.ContainsKey(key)) + { + return false; // 键已存在,不允许重复的键 + } + + keyValueDict[key] = value; + + if (!valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value] = new HashSet(); + } + valueKeyDict[value].Add(key); + return true; + } + } + + /// + /// Set a key-value pair.
+ /// 设置一个键值对。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the Settings are successful.
是否设置成功
+ public virtual async Task SetAsync(string key, decimal value) + { + await semaphore.WaitAsync(); + try + { + if (keyValueDict.ContainsKey(key)) + { + return false; // 键已存在,不允许重复的键 + } + + keyValueDict[key] = value; + + if (!valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value] = new HashSet(); + } + valueKeyDict[value].Add(key); + return true; + } + finally + { + semaphore.Release(); + } + } + + /// + /// Remove a key-value pair.
+ /// 移除一个键值对。 + ///
+ /// Key
键 + /// Check whether the removal is successful.
是否移除成功。
+ public virtual bool RemoveByKey(string key) + { + lock (semaphore) + { + if (!keyValueDict.ContainsKey(key)) + { + return false; + } + var value = keyValueDict[key]; + keyValueDict.Remove(key); + + if (valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value].Remove(key); + if (valueKeyDict[value].Count == 0) + { + valueKeyDict.Remove(value); + } + } + return true; + } + } + + /// + /// Remove a key-value pair.
+ /// 移除一个键值对。 + ///
+ /// Key
键 + /// Check whether the removal is successful.
是否移除成功。
+ public virtual async Task RemoveByKeyAsync(string key) + { + await semaphore.WaitAsync(); + try + { + if (!keyValueDict.ContainsKey(key)) + { + return false; + } + var value = keyValueDict[key]; + keyValueDict.Remove(key); + + if (valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value].Remove(key); + if (valueKeyDict[value].Count == 0) + { + valueKeyDict.Remove(value); + } + } + return true; + } + finally + { + semaphore.Release(); + } + } + + /// + /// Empty the cache.
+ /// 清空缓存。 + ///
+ public virtual void Clear() + { + lock (semaphore) + { + keyValueDict.Clear(); + valueKeyDict.Clear(); + } + } + + /// + /// Empty the cache.
+ /// 清空缓存。 + ///
+ public virtual async void ClearAsync() + { + await semaphore.WaitAsync(); + try + { + keyValueDict.Clear(); + valueKeyDict.Clear(); + } + finally + { + semaphore.Release(); + } + } + + /// + /// Gets all key-value pairs.
+ /// 获取所有的键值对。 + ///
+ /// Key-value pair
键值对
+ public virtual Dictionary GetAllCache() + { + lock (semaphore) + { + // 返回完整的键值对字典 + return new Dictionary(keyValueDict); + } + } + + /// + /// Gets all key-value pairs.
+ /// 获取所有的键值对。 + ///
+ /// Key-value pair
键值对
+ public virtual async Task> GetAllCacheAsync() + { + await semaphore.WaitAsync(); + try + { + // 返回完整的键值对字典 + return new Dictionary(keyValueDict); + } + finally + { + semaphore.Release(); + } + } + } +} diff --git a/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlDoubleDictionary.cs b/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlDoubleDictionary.cs new file mode 100644 index 0000000..a2b624d --- /dev/null +++ b/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlDoubleDictionary.cs @@ -0,0 +1,291 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace TYLDDB.Utils.FastCache.SemaphoreThreadLock +{ + /// + /// Use semaphore based thread locks to achieve high concurrency stability.
+ /// 使用基于信号量的线程锁来实现高并发的稳定性。 + ///
+ public class StlDoubleDictionary + { + private readonly Dictionary keyValueDict; // 存储键->值映射 + private readonly Dictionary> valueKeyDict; // 存储值->键的映射 + private readonly SemaphoreSlim semaphore; // 控制并发访问 + + /// + /// Use cached key-value pairs for fast reads and writes.
+ /// 使用缓存的键值对来快速读写。 + ///
+ public StlDoubleDictionary() + { + keyValueDict = new Dictionary(); + valueKeyDict = new Dictionary>(); + semaphore = new SemaphoreSlim(1, 1); // 使用信号量来同步 + } + + /// + /// Synchronization method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual double GetByKey(string key) + { + lock (semaphore) + { + keyValueDict.TryGetValue(key, out var value); + return value; + } + } + + /// + /// Asynchronous method: Obtains the corresponding value based on the key.
+ /// 异步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual async Task GetByKeyAsync(string key) + { + await semaphore.WaitAsync(); + try + { + keyValueDict.TryGetValue(key, out var value); + return value; + } + finally + { + semaphore.Release(); + } + } + + /// + /// Synchronization method: Obtains one or more keys according to the value.
+ /// 同步方法:根据值获取对应的一个或多个键。 + ///
+ /// Value
值 + /// Key (List)
键 (List)
+ public virtual List GetKeysByValue(double value) + { + lock (semaphore) + { + if (valueKeyDict.ContainsKey(value)) + { + return valueKeyDict[value].ToList(); + } + return new List(); + } + } + + /// + /// Asynchronous method: Get one or more keys based on the value.
+ /// 异步方法:根据值获取对应的一个或多个键。 + ///
+ /// Value
值 + /// Key (List)
键 (List)
+ public virtual async Task> GetKeysByValueAsync(double value) + { + await semaphore.WaitAsync(); + try + { + if (valueKeyDict.ContainsKey(value)) + { + return valueKeyDict[value].ToList(); + } + return new List(); + } + finally + { + semaphore.Release(); + } + } + + /// + /// Set a key-value pair.
+ /// 设置一个键值对。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the Settings are successful.
是否设置成功
+ public virtual bool Set(string key, double value) + { + lock (semaphore) + { + if (keyValueDict.ContainsKey(key)) + { + return false; // 键已存在,不允许重复的键 + } + + keyValueDict[key] = value; + + if (!valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value] = new HashSet(); + } + valueKeyDict[value].Add(key); + return true; + } + } + + /// + /// Set a key-value pair.
+ /// 设置一个键值对。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the Settings are successful.
是否设置成功
+ public virtual async Task SetAsync(string key, double value) + { + await semaphore.WaitAsync(); + try + { + if (keyValueDict.ContainsKey(key)) + { + return false; // 键已存在,不允许重复的键 + } + + keyValueDict[key] = value; + + if (!valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value] = new HashSet(); + } + valueKeyDict[value].Add(key); + return true; + } + finally + { + semaphore.Release(); + } + } + + /// + /// Remove a key-value pair.
+ /// 移除一个键值对。 + ///
+ /// Key
键 + /// Check whether the removal is successful.
是否移除成功。
+ public virtual bool RemoveByKey(string key) + { + lock (semaphore) + { + if (!keyValueDict.ContainsKey(key)) + { + return false; + } + var value = keyValueDict[key]; + keyValueDict.Remove(key); + + if (valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value].Remove(key); + if (valueKeyDict[value].Count == 0) + { + valueKeyDict.Remove(value); + } + } + return true; + } + } + + /// + /// Remove a key-value pair.
+ /// 移除一个键值对。 + ///
+ /// Key
键 + /// Check whether the removal is successful.
是否移除成功。
+ public virtual async Task RemoveByKeyAsync(string key) + { + await semaphore.WaitAsync(); + try + { + if (!keyValueDict.ContainsKey(key)) + { + return false; + } + var value = keyValueDict[key]; + keyValueDict.Remove(key); + + if (valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value].Remove(key); + if (valueKeyDict[value].Count == 0) + { + valueKeyDict.Remove(value); + } + } + return true; + } + finally + { + semaphore.Release(); + } + } + + /// + /// Empty the cache.
+ /// 清空缓存。 + ///
+ public virtual void Clear() + { + lock (semaphore) + { + keyValueDict.Clear(); + valueKeyDict.Clear(); + } + } + + /// + /// Empty the cache.
+ /// 清空缓存。 + ///
+ public virtual async void ClearAsync() + { + await semaphore.WaitAsync(); + try + { + keyValueDict.Clear(); + valueKeyDict.Clear(); + } + finally + { + semaphore.Release(); + } + } + + /// + /// Gets all key-value pairs.
+ /// 获取所有的键值对。 + ///
+ /// Key-value pair
键值对
+ public virtual Dictionary GetAllCache() + { + lock (semaphore) + { + // 返回完整的键值对字典 + return new Dictionary(keyValueDict); + } + } + + /// + /// Gets all key-value pairs.
+ /// 获取所有的键值对。 + ///
+ /// Key-value pair
键值对
+ public virtual async Task> GetAllCacheAsync() + { + await semaphore.WaitAsync(); + try + { + // 返回完整的键值对字典 + return new Dictionary(keyValueDict); + } + finally + { + semaphore.Release(); + } + } + } +} diff --git a/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlFloatDictionary.cs b/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlFloatDictionary.cs new file mode 100644 index 0000000..e746d61 --- /dev/null +++ b/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlFloatDictionary.cs @@ -0,0 +1,291 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace TYLDDB.Utils.FastCache.SemaphoreThreadLock +{ + /// + /// Use semaphore based thread locks to achieve high concurrency stability.
+ /// 使用基于信号量的线程锁来实现高并发的稳定性。 + ///
+ public class StlFloatDictionary + { + private readonly Dictionary keyValueDict; // 存储键->值映射 + private readonly Dictionary> valueKeyDict; // 存储值->键的映射 + private readonly SemaphoreSlim semaphore; // 控制并发访问 + + /// + /// Use cached key-value pairs for fast reads and writes.
+ /// 使用缓存的键值对来快速读写。 + ///
+ public StlFloatDictionary() + { + keyValueDict = new Dictionary(); + valueKeyDict = new Dictionary>(); + semaphore = new SemaphoreSlim(1, 1); // 使用信号量来同步 + } + + /// + /// Synchronization method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual float GetByKey(string key) + { + lock (semaphore) + { + keyValueDict.TryGetValue(key, out var value); + return value; + } + } + + /// + /// Asynchronous method: Obtains the corresponding value based on the key.
+ /// 异步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual async Task GetByKeyAsync(string key) + { + await semaphore.WaitAsync(); + try + { + keyValueDict.TryGetValue(key, out var value); + return value; + } + finally + { + semaphore.Release(); + } + } + + /// + /// Synchronization method: Obtains one or more keys according to the value.
+ /// 同步方法:根据值获取对应的一个或多个键。 + ///
+ /// Value
值 + /// Key (List)
键 (List)
+ public virtual List GetKeysByValue(float value) + { + lock (semaphore) + { + if (valueKeyDict.ContainsKey(value)) + { + return valueKeyDict[value].ToList(); + } + return new List(); + } + } + + /// + /// Asynchronous method: Get one or more keys based on the value.
+ /// 异步方法:根据值获取对应的一个或多个键。 + ///
+ /// Value
值 + /// Key (List)
键 (List)
+ public virtual async Task> GetKeysByValueAsync(float value) + { + await semaphore.WaitAsync(); + try + { + if (valueKeyDict.ContainsKey(value)) + { + return valueKeyDict[value].ToList(); + } + return new List(); + } + finally + { + semaphore.Release(); + } + } + + /// + /// Set a key-value pair.
+ /// 设置一个键值对。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the Settings are successful.
是否设置成功
+ public virtual bool Set(string key, float value) + { + lock (semaphore) + { + if (keyValueDict.ContainsKey(key)) + { + return false; // 键已存在,不允许重复的键 + } + + keyValueDict[key] = value; + + if (!valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value] = new HashSet(); + } + valueKeyDict[value].Add(key); + return true; + } + } + + /// + /// Set a key-value pair.
+ /// 设置一个键值对。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the Settings are successful.
是否设置成功
+ public virtual async Task SetAsync(string key, float value) + { + await semaphore.WaitAsync(); + try + { + if (keyValueDict.ContainsKey(key)) + { + return false; // 键已存在,不允许重复的键 + } + + keyValueDict[key] = value; + + if (!valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value] = new HashSet(); + } + valueKeyDict[value].Add(key); + return true; + } + finally + { + semaphore.Release(); + } + } + + /// + /// Remove a key-value pair.
+ /// 移除一个键值对。 + ///
+ /// Key
键 + /// Check whether the removal is successful.
是否移除成功。
+ public virtual bool RemoveByKey(string key) + { + lock (semaphore) + { + if (!keyValueDict.ContainsKey(key)) + { + return false; + } + var value = keyValueDict[key]; + keyValueDict.Remove(key); + + if (valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value].Remove(key); + if (valueKeyDict[value].Count == 0) + { + valueKeyDict.Remove(value); + } + } + return true; + } + } + + /// + /// Remove a key-value pair.
+ /// 移除一个键值对。 + ///
+ /// Key
键 + /// Check whether the removal is successful.
是否移除成功。
+ public virtual async Task RemoveByKeyAsync(string key) + { + await semaphore.WaitAsync(); + try + { + if (!keyValueDict.ContainsKey(key)) + { + return false; + } + var value = keyValueDict[key]; + keyValueDict.Remove(key); + + if (valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value].Remove(key); + if (valueKeyDict[value].Count == 0) + { + valueKeyDict.Remove(value); + } + } + return true; + } + finally + { + semaphore.Release(); + } + } + + /// + /// Empty the cache.
+ /// 清空缓存。 + ///
+ public virtual void Clear() + { + lock (semaphore) + { + keyValueDict.Clear(); + valueKeyDict.Clear(); + } + } + + /// + /// Empty the cache.
+ /// 清空缓存。 + ///
+ public virtual async void ClearAsync() + { + await semaphore.WaitAsync(); + try + { + keyValueDict.Clear(); + valueKeyDict.Clear(); + } + finally + { + semaphore.Release(); + } + } + + /// + /// Gets all key-value pairs.
+ /// 获取所有的键值对。 + ///
+ /// Key-value pair
键值对
+ public virtual Dictionary GetAllCache() + { + lock (semaphore) + { + // 返回完整的键值对字典 + return new Dictionary(keyValueDict); + } + } + + /// + /// Gets all key-value pairs.
+ /// 获取所有的键值对。 + ///
+ /// Key-value pair
键值对
+ public virtual async Task> GetAllCacheAsync() + { + await semaphore.WaitAsync(); + try + { + // 返回完整的键值对字典 + return new Dictionary(keyValueDict); + } + finally + { + semaphore.Release(); + } + } + } +} diff --git a/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlIntegerDictionary.cs b/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlIntegerDictionary.cs new file mode 100644 index 0000000..b2e458d --- /dev/null +++ b/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlIntegerDictionary.cs @@ -0,0 +1,291 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace TYLDDB.Utils.FastCache.SemaphoreThreadLock +{ + /// + /// Use semaphore based thread locks to achieve high concurrency stability.
+ /// 使用基于信号量的线程锁来实现高并发的稳定性。 + ///
+ public class StlIntegerDictionary + { + private readonly Dictionary keyValueDict; // 存储键->值映射 + private readonly Dictionary> valueKeyDict; // 存储值->键的映射 + private readonly SemaphoreSlim semaphore; // 控制并发访问 + + /// + /// Use cached key-value pairs for fast reads and writes.
+ /// 使用缓存的键值对来快速读写。 + ///
+ public StlIntegerDictionary() + { + keyValueDict = new Dictionary(); + valueKeyDict = new Dictionary>(); + semaphore = new SemaphoreSlim(1, 1); // 使用信号量来同步 + } + + /// + /// Synchronization method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual int GetByKey(string key) + { + lock (semaphore) + { + keyValueDict.TryGetValue(key, out var value); + return value; + } + } + + /// + /// Asynchronous method: Obtains the corresponding value based on the key.
+ /// 异步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual async Task GetByKeyAsync(string key) + { + await semaphore.WaitAsync(); + try + { + keyValueDict.TryGetValue(key, out var value); + return value; + } + finally + { + semaphore.Release(); + } + } + + /// + /// Synchronization method: Obtains one or more keys according to the value.
+ /// 同步方法:根据值获取对应的一个或多个键。 + ///
+ /// Value
值 + /// Key (List)
键 (List)
+ public virtual List GetKeysByValue(int value) + { + lock (semaphore) + { + if (valueKeyDict.ContainsKey(value)) + { + return valueKeyDict[value].ToList(); + } + return new List(); + } + } + + /// + /// Asynchronous method: Get one or more keys based on the value.
+ /// 异步方法:根据值获取对应的一个或多个键。 + ///
+ /// Value
值 + /// Key (List)
键 (List)
+ public virtual async Task> GetKeysByValueAsync(int value) + { + await semaphore.WaitAsync(); + try + { + if (valueKeyDict.ContainsKey(value)) + { + return valueKeyDict[value].ToList(); + } + return new List(); + } + finally + { + semaphore.Release(); + } + } + + /// + /// Set a key-value pair.
+ /// 设置一个键值对。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the Settings are successful.
是否设置成功
+ public virtual bool Set(string key, int value) + { + lock (semaphore) + { + if (keyValueDict.ContainsKey(key)) + { + return false; // 键已存在,不允许重复的键 + } + + keyValueDict[key] = value; + + if (!valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value] = new HashSet(); + } + valueKeyDict[value].Add(key); + return true; + } + } + + /// + /// Set a key-value pair.
+ /// 设置一个键值对。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the Settings are successful.
是否设置成功
+ public virtual async Task SetAsync(string key, int value) + { + await semaphore.WaitAsync(); + try + { + if (keyValueDict.ContainsKey(key)) + { + return false; // 键已存在,不允许重复的键 + } + + keyValueDict[key] = value; + + if (!valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value] = new HashSet(); + } + valueKeyDict[value].Add(key); + return true; + } + finally + { + semaphore.Release(); + } + } + + /// + /// Remove a key-value pair.
+ /// 移除一个键值对。 + ///
+ /// Key
键 + /// Check whether the removal is successful.
是否移除成功。
+ public virtual bool RemoveByKey(string key) + { + lock (semaphore) + { + if (!keyValueDict.ContainsKey(key)) + { + return false; + } + var value = keyValueDict[key]; + keyValueDict.Remove(key); + + if (valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value].Remove(key); + if (valueKeyDict[value].Count == 0) + { + valueKeyDict.Remove(value); + } + } + return true; + } + } + + /// + /// Remove a key-value pair.
+ /// 移除一个键值对。 + ///
+ /// Key
键 + /// Check whether the removal is successful.
是否移除成功。
+ public virtual async Task RemoveByKeyAsync(string key) + { + await semaphore.WaitAsync(); + try + { + if (!keyValueDict.ContainsKey(key)) + { + return false; + } + var value = keyValueDict[key]; + keyValueDict.Remove(key); + + if (valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value].Remove(key); + if (valueKeyDict[value].Count == 0) + { + valueKeyDict.Remove(value); + } + } + return true; + } + finally + { + semaphore.Release(); + } + } + + /// + /// Empty the cache.
+ /// 清空缓存。 + ///
+ public virtual void Clear() + { + lock (semaphore) + { + keyValueDict.Clear(); + valueKeyDict.Clear(); + } + } + + /// + /// Empty the cache.
+ /// 清空缓存。 + ///
+ public virtual async void ClearAsync() + { + await semaphore.WaitAsync(); + try + { + keyValueDict.Clear(); + valueKeyDict.Clear(); + } + finally + { + semaphore.Release(); + } + } + + /// + /// Gets all key-value pairs.
+ /// 获取所有的键值对。 + ///
+ /// Key-value pair
键值对
+ public virtual Dictionary GetAllCache() + { + lock (semaphore) + { + // 返回完整的键值对字典 + return new Dictionary(keyValueDict); + } + } + + /// + /// Gets all key-value pairs.
+ /// 获取所有的键值对。 + ///
+ /// Key-value pair
键值对
+ public virtual async Task> GetAllCacheAsync() + { + await semaphore.WaitAsync(); + try + { + // 返回完整的键值对字典 + return new Dictionary(keyValueDict); + } + finally + { + semaphore.Release(); + } + } + } +} diff --git a/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlLongDictionary.cs b/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlLongDictionary.cs new file mode 100644 index 0000000..91e7572 --- /dev/null +++ b/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlLongDictionary.cs @@ -0,0 +1,291 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace TYLDDB.Utils.FastCache.SemaphoreThreadLock +{ + /// + /// Use semaphore based thread locks to achieve high concurrency stability.
+ /// 使用基于信号量的线程锁来实现高并发的稳定性。 + ///
+ public class StlLongDictionary + { + private readonly Dictionary keyValueDict; // 存储键->值映射 + private readonly Dictionary> valueKeyDict; // 存储值->键的映射 + private readonly SemaphoreSlim semaphore; // 控制并发访问 + + /// + /// Use cached key-value pairs for fast reads and writes.
+ /// 使用缓存的键值对来快速读写。 + ///
+ public StlLongDictionary() + { + keyValueDict = new Dictionary(); + valueKeyDict = new Dictionary>(); + semaphore = new SemaphoreSlim(1, 1); // 使用信号量来同步 + } + + /// + /// Synchronization method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual long GetByKey(string key) + { + lock (semaphore) + { + keyValueDict.TryGetValue(key, out var value); + return value; + } + } + + /// + /// Asynchronous method: Obtains the corresponding value based on the key.
+ /// 异步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual async Task GetByKeyAsync(string key) + { + await semaphore.WaitAsync(); + try + { + keyValueDict.TryGetValue(key, out var value); + return value; + } + finally + { + semaphore.Release(); + } + } + + /// + /// Synchronization method: Obtains one or more keys according to the value.
+ /// 同步方法:根据值获取对应的一个或多个键。 + ///
+ /// Value
值 + /// Key (List)
键 (List)
+ public virtual List GetKeysByValue(long value) + { + lock (semaphore) + { + if (valueKeyDict.ContainsKey(value)) + { + return valueKeyDict[value].ToList(); + } + return new List(); + } + } + + /// + /// Asynchronous method: Get one or more keys based on the value.
+ /// 异步方法:根据值获取对应的一个或多个键。 + ///
+ /// Value
值 + /// Key (List)
键 (List)
+ public virtual async Task> GetKeysByValueAsync(long value) + { + await semaphore.WaitAsync(); + try + { + if (valueKeyDict.ContainsKey(value)) + { + return valueKeyDict[value].ToList(); + } + return new List(); + } + finally + { + semaphore.Release(); + } + } + + /// + /// Set a key-value pair.
+ /// 设置一个键值对。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the Settings are successful.
是否设置成功
+ public virtual bool Set(string key, long value) + { + lock (semaphore) + { + if (keyValueDict.ContainsKey(key)) + { + return false; // 键已存在,不允许重复的键 + } + + keyValueDict[key] = value; + + if (!valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value] = new HashSet(); + } + valueKeyDict[value].Add(key); + return true; + } + } + + /// + /// Set a key-value pair.
+ /// 设置一个键值对。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the Settings are successful.
是否设置成功
+ public virtual async Task SetAsync(string key, long value) + { + await semaphore.WaitAsync(); + try + { + if (keyValueDict.ContainsKey(key)) + { + return false; // 键已存在,不允许重复的键 + } + + keyValueDict[key] = value; + + if (!valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value] = new HashSet(); + } + valueKeyDict[value].Add(key); + return true; + } + finally + { + semaphore.Release(); + } + } + + /// + /// Remove a key-value pair.
+ /// 移除一个键值对。 + ///
+ /// Key
键 + /// Check whether the removal is successful.
是否移除成功。
+ public virtual bool RemoveByKey(string key) + { + lock (semaphore) + { + if (!keyValueDict.ContainsKey(key)) + { + return false; + } + var value = keyValueDict[key]; + keyValueDict.Remove(key); + + if (valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value].Remove(key); + if (valueKeyDict[value].Count == 0) + { + valueKeyDict.Remove(value); + } + } + return true; + } + } + + /// + /// Remove a key-value pair.
+ /// 移除一个键值对。 + ///
+ /// Key
键 + /// Check whether the removal is successful.
是否移除成功。
+ public virtual async Task RemoveByKeyAsync(string key) + { + await semaphore.WaitAsync(); + try + { + if (!keyValueDict.ContainsKey(key)) + { + return false; + } + var value = keyValueDict[key]; + keyValueDict.Remove(key); + + if (valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value].Remove(key); + if (valueKeyDict[value].Count == 0) + { + valueKeyDict.Remove(value); + } + } + return true; + } + finally + { + semaphore.Release(); + } + } + + /// + /// Empty the cache.
+ /// 清空缓存。 + ///
+ public virtual void Clear() + { + lock (semaphore) + { + keyValueDict.Clear(); + valueKeyDict.Clear(); + } + } + + /// + /// Empty the cache.
+ /// 清空缓存。 + ///
+ public virtual async void ClearAsync() + { + await semaphore.WaitAsync(); + try + { + keyValueDict.Clear(); + valueKeyDict.Clear(); + } + finally + { + semaphore.Release(); + } + } + + /// + /// Gets all key-value pairs.
+ /// 获取所有的键值对。 + ///
+ /// Key-value pair
键值对
+ public virtual Dictionary GetAllCache() + { + lock (semaphore) + { + // 返回完整的键值对字典 + return new Dictionary(keyValueDict); + } + } + + /// + /// Gets all key-value pairs.
+ /// 获取所有的键值对。 + ///
+ /// Key-value pair
键值对
+ public virtual async Task> GetAllCacheAsync() + { + await semaphore.WaitAsync(); + try + { + // 返回完整的键值对字典 + return new Dictionary(keyValueDict); + } + finally + { + semaphore.Release(); + } + } + } +} diff --git a/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlShortDictionary.cs b/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlShortDictionary.cs new file mode 100644 index 0000000..0e74faf --- /dev/null +++ b/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlShortDictionary.cs @@ -0,0 +1,291 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace TYLDDB.Utils.FastCache.SemaphoreThreadLock +{ + /// + /// Use semaphore based thread locks to achieve high concurrency stability.
+ /// 使用基于信号量的线程锁来实现高并发的稳定性。 + ///
+ public class StlShortDictionary + { + private readonly Dictionary keyValueDict; // 存储键->值映射 + private readonly Dictionary> valueKeyDict; // 存储值->键的映射 + private readonly SemaphoreSlim semaphore; // 控制并发访问 + + /// + /// Use cached key-value pairs for fast reads and writes.
+ /// 使用缓存的键值对来快速读写。 + ///
+ public StlShortDictionary() + { + keyValueDict = new Dictionary(); + valueKeyDict = new Dictionary>(); + semaphore = new SemaphoreSlim(1, 1); // 使用信号量来同步 + } + + /// + /// Synchronization method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual short GetByKey(string key) + { + lock (semaphore) + { + keyValueDict.TryGetValue(key, out var value); + return value; + } + } + + /// + /// Asynchronous method: Obtains the corresponding value based on the key.
+ /// 异步方法:根据键获取对应的值。 + ///
+ /// Key
键 + /// Value
+ public virtual async Task GetByKeyAsync(string key) + { + await semaphore.WaitAsync(); + try + { + keyValueDict.TryGetValue(key, out var value); + return value; + } + finally + { + semaphore.Release(); + } + } + + /// + /// Synchronization method: Obtains one or more keys according to the value.
+ /// 同步方法:根据值获取对应的一个或多个键。 + ///
+ /// Value
值 + /// Key (List)
键 (List)
+ public virtual List GetKeysByValue(short value) + { + lock (semaphore) + { + if (valueKeyDict.ContainsKey(value)) + { + return valueKeyDict[value].ToList(); + } + return new List(); + } + } + + /// + /// Asynchronous method: Get one or more keys based on the value.
+ /// 异步方法:根据值获取对应的一个或多个键。 + ///
+ /// Value
值 + /// Key (List)
键 (List)
+ public virtual async Task> GetKeysByValueAsync(short value) + { + await semaphore.WaitAsync(); + try + { + if (valueKeyDict.ContainsKey(value)) + { + return valueKeyDict[value].ToList(); + } + return new List(); + } + finally + { + semaphore.Release(); + } + } + + /// + /// Set a key-value pair.
+ /// 设置一个键值对。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the Settings are successful.
是否设置成功
+ public virtual bool Set(string key, short value) + { + lock (semaphore) + { + if (keyValueDict.ContainsKey(key)) + { + return false; // 键已存在,不允许重复的键 + } + + keyValueDict[key] = value; + + if (!valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value] = new HashSet(); + } + valueKeyDict[value].Add(key); + return true; + } + } + + /// + /// Set a key-value pair.
+ /// 设置一个键值对。 + ///
+ /// Key
键 + /// Value
值 + /// Whether the Settings are successful.
是否设置成功
+ public virtual async Task SetAsync(string key, short value) + { + await semaphore.WaitAsync(); + try + { + if (keyValueDict.ContainsKey(key)) + { + return false; // 键已存在,不允许重复的键 + } + + keyValueDict[key] = value; + + if (!valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value] = new HashSet(); + } + valueKeyDict[value].Add(key); + return true; + } + finally + { + semaphore.Release(); + } + } + + /// + /// Remove a key-value pair.
+ /// 移除一个键值对。 + ///
+ /// Key
键 + /// Check whether the removal is successful.
是否移除成功。
+ public virtual bool RemoveByKey(string key) + { + lock (semaphore) + { + if (!keyValueDict.ContainsKey(key)) + { + return false; + } + var value = keyValueDict[key]; + keyValueDict.Remove(key); + + if (valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value].Remove(key); + if (valueKeyDict[value].Count == 0) + { + valueKeyDict.Remove(value); + } + } + return true; + } + } + + /// + /// Remove a key-value pair.
+ /// 移除一个键值对。 + ///
+ /// Key
键 + /// Check whether the removal is successful.
是否移除成功。
+ public virtual async Task RemoveByKeyAsync(string key) + { + await semaphore.WaitAsync(); + try + { + if (!keyValueDict.ContainsKey(key)) + { + return false; + } + var value = keyValueDict[key]; + keyValueDict.Remove(key); + + if (valueKeyDict.ContainsKey(value)) + { + valueKeyDict[value].Remove(key); + if (valueKeyDict[value].Count == 0) + { + valueKeyDict.Remove(value); + } + } + return true; + } + finally + { + semaphore.Release(); + } + } + + /// + /// Empty the cache.
+ /// 清空缓存。 + ///
+ public virtual void Clear() + { + lock (semaphore) + { + keyValueDict.Clear(); + valueKeyDict.Clear(); + } + } + + /// + /// Empty the cache.
+ /// 清空缓存。 + ///
+ public virtual async void ClearAsync() + { + await semaphore.WaitAsync(); + try + { + keyValueDict.Clear(); + valueKeyDict.Clear(); + } + finally + { + semaphore.Release(); + } + } + + /// + /// Gets all key-value pairs.
+ /// 获取所有的键值对。 + ///
+ /// Key-value pair
键值对
+ public virtual Dictionary GetAllCache() + { + lock (semaphore) + { + // 返回完整的键值对字典 + return new Dictionary(keyValueDict); + } + } + + /// + /// Gets all key-value pairs.
+ /// 获取所有的键值对。 + ///
+ /// Key-value pair
键值对
+ public virtual async Task> GetAllCacheAsync() + { + await semaphore.WaitAsync(); + try + { + // 返回完整的键值对字典 + return new Dictionary(keyValueDict); + } + finally + { + semaphore.Release(); + } + } + } +} diff --git a/TYLDDB/Utils/FastCache/SemaphoreThreadLock.cs b/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlStringDictionary.cs similarity index 84% rename from TYLDDB/Utils/FastCache/SemaphoreThreadLock.cs rename to TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlStringDictionary.cs index c7ff0a5..09bc308 100644 --- a/TYLDDB/Utils/FastCache/SemaphoreThreadLock.cs +++ b/TYLDDB/Utils/FastCache/SemaphoreThreadLock/StlStringDictionary.cs @@ -3,13 +3,13 @@ using System.Threading; using System.Threading.Tasks; -namespace TYLDDB.Utils.FastCache +namespace TYLDDB.Utils.FastCache.SemaphoreThreadLock { /// /// Use semaphore based thread locks to achieve high concurrency stability.
/// 使用基于信号量的线程锁来实现高并发的稳定性。 ///
- public class SemaphoreThreadLock : Cache + public class StlStringDictionary { private readonly Dictionary keyValueDict; // 存储键->值映射 private readonly Dictionary> valueKeyDict; // 存储值->键的映射 @@ -19,7 +19,7 @@ public class SemaphoreThreadLock : Cache /// Use cached key-value pairs for fast reads and writes.
/// 使用缓存的键值对来快速读写。 ///
- public SemaphoreThreadLock() + public StlStringDictionary() { keyValueDict = new Dictionary(); valueKeyDict = new Dictionary>(); @@ -32,7 +32,7 @@ public SemaphoreThreadLock() /// /// Key
键 /// Value
- public override string GetByKey(string key) + public virtual string GetByKey(string key) { lock (semaphore) { @@ -47,7 +47,7 @@ public override string GetByKey(string key) /// /// Key
键 /// Value
- public override async Task GetByKeyAsync(string key) + public virtual async Task GetByKeyAsync(string key) { await semaphore.WaitAsync(); try @@ -67,7 +67,7 @@ public override async Task GetByKeyAsync(string key) /// /// Value
值 /// Key (List)
键 (List)
- public override List GetKeysByValue(string value) + public virtual List GetKeysByValue(string value) { lock (semaphore) { @@ -85,7 +85,7 @@ public override List GetKeysByValue(string value) /// /// Value
值 /// Key (List)
键 (List)
- public override async Task> GetKeysByValueAsync(string value) + public virtual async Task> GetKeysByValueAsync(string value) { await semaphore.WaitAsync(); try @@ -109,7 +109,7 @@ public override async Task> GetKeysByValueAsync(string value) /// Key
键 /// Value
值 /// Whether the Settings are successful.
是否设置成功
- public override bool Set(string key, string value) + public virtual bool Set(string key, string value) { lock (semaphore) { @@ -136,7 +136,7 @@ public override bool Set(string key, string value) /// Key
键 /// Value
值 /// Whether the Settings are successful.
是否设置成功
- public override async Task SetAsync(string key, string value) + public virtual async Task SetAsync(string key, string value) { await semaphore.WaitAsync(); try @@ -162,11 +162,12 @@ public override async Task SetAsync(string key, string value) } /// - /// 同步方法:移除一个键值对。 + /// Remove a key-value pair.
+ /// 移除一个键值对。 ///
- /// - /// - public override bool RemoveByKey(string key) + /// Key
键 + /// Check whether the removal is successful.
是否移除成功。
+ public virtual bool RemoveByKey(string key) { lock (semaphore) { @@ -190,11 +191,12 @@ public override bool RemoveByKey(string key) } /// - /// 异步方法:移除一个键值对。 + /// Remove a key-value pair.
+ /// 移除一个键值对。 ///
- /// - /// - public override async Task RemoveByKeyAsync(string key) + /// Key
键 + /// Check whether the removal is successful.
是否移除成功。
+ public virtual async Task RemoveByKeyAsync(string key) { await semaphore.WaitAsync(); try @@ -223,9 +225,10 @@ public override async Task RemoveByKeyAsync(string key) } /// - /// 同步方法:清空缓存。 + /// Empty the cache.
+ /// 清空缓存。 ///
- public override void Clear() + public virtual void Clear() { lock (semaphore) { @@ -235,10 +238,10 @@ public override void Clear() } /// - /// 异步方法:清空缓存。 + /// Empty the cache.
+ /// 清空缓存。 ///
- /// - public override async Task ClearAsync() + public virtual async void ClearAsync() { await semaphore.WaitAsync(); try @@ -257,7 +260,7 @@ public override async Task ClearAsync() /// 获取所有的键值对。 /// /// Key-value pair
键值对
- public override Dictionary GetAllCache() + public virtual Dictionary GetAllCache() { lock (semaphore) { @@ -271,7 +274,7 @@ public override Dictionary GetAllCache() /// 获取所有的键值对。 /// /// Key-value pair
键值对
- public override async Task> GetAllCacheAsync() + public virtual async Task> GetAllCacheAsync() { await semaphore.WaitAsync(); try diff --git a/TimeRecord/TimeRecord.csproj b/TimeRecord/TimeRecord.csproj index fa71b7a..58f574d 100644 --- a/TimeRecord/TimeRecord.csproj +++ b/TimeRecord/TimeRecord.csproj @@ -6,4 +6,12 @@ enable +<<<<<<<< HEAD:TimeRecord/TimeRecord.csproj +======== + + + + + +>>>>>>>> cdf9c1039cfeae32fce9aac87ceb4ce2409ec22d:TYLDDB.Utils.FastCache.Test/TYLDDB.Utils.FastCache.Test.csproj