diff --git a/TYLDDB/TYLDDB.csproj b/TYLDDB/TYLDDB.csproj index 7a9222e..cd2e836 100644 --- a/TYLDDB/TYLDDB.csproj +++ b/TYLDDB/TYLDDB.csproj @@ -1,10 +1,10 @@ - netstandard2.0;netstandard2.1;net6.0;net8.0;net9.0 + netstandard2.1;net6.0;net8.0;net9.0 True - TTYPDB.NET - 1.0.0-alpha.2 + TYLDDB.NET + 1.0.0-alpha.2r TYLDDB-Project LICENSE True @@ -19,6 +19,8 @@ DataBase QingYi-Studio README.md + Remove support for .netstandard 2.0. +Remove direct inheritance/derivation of the ICache interface and instead use abstract class relays, using virtual methods to trim asynchronous code in part of the concurrent dictionary. diff --git a/TYLDDB/Utils/FastCache/Cache.cs b/TYLDDB/Utils/FastCache/Cache.cs index 3551198..dc06ba2 100644 --- a/TYLDDB/Utils/FastCache/Cache.cs +++ b/TYLDDB/Utils/FastCache/Cache.cs @@ -1,286 +1,39 @@ -using System.Collections.Generic; -using System.Linq; -using System.Threading; +using System; +using System.Collections.Generic; +using System.Text; using System.Threading.Tasks; namespace TYLDDB.Utils.FastCache { /// - /// Use cached key-value pairs for fast reads and writes.
- /// 使用缓存的键值对来快速读写。 + /// For cached abstract classes, you need to inherit the class to do concrete implementation.
+ /// 对于缓存的抽象类,需要继承该类来做具体实现。 ///
- public class Cache + public abstract class Cache : ICache { - private readonly Dictionary keyValueDict; // 存储键->值映射 - private readonly Dictionary> valueKeyDict; // 存储值->键的映射 - private readonly SemaphoreSlim semaphore; // 控制并发访问 - - /// - /// Use cached key-value pairs for fast reads and writes.
- /// 使用缓存的键值对来快速读写。 - ///
- public Cache() - { - keyValueDict = new Dictionary(); - valueKeyDict = new Dictionary>(); - semaphore = new SemaphoreSlim(1, 1); // 使用信号量来同步 - } - - /// - /// Synchronization method: Obtain the corresponding value by key.
- /// 同步方法:根据键获取对应的值。 - ///
- /// Key
键 - /// Value
- public string 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 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 List GetKeysByValue(string 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 async Task> GetKeysByValueAsync(string value) - { - await semaphore.WaitAsync(); - try - { - if (valueKeyDict.ContainsKey(value)) - { - return valueKeyDict[value].ToList(); - } - return new List(); - } - finally - { - semaphore.Release(); - } - } - - /// - /// 同步方法:设置一个键值对。 - /// - /// - /// - /// - public bool Set(string key, string 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; - } - } - - /// - /// 异步方法:设置一个键值对。 - /// - /// - /// - /// - public async Task SetAsync(string key, string 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(); - } - } - - /// - /// 同步方法:移除一个键值对。 - /// - /// - /// - public bool RemoveByKey(string key) - { - lock (semaphore) - { - if (keyValueDict.ContainsKey(key)) - { - 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; - } - return false; - } - } - - /// - /// 异步方法:移除一个键值对。 - /// - /// - /// - public async Task RemoveByKeyAsync(string key) - { - await semaphore.WaitAsync(); - try - { - if (keyValueDict.ContainsKey(key)) - { - 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; - } - return false; - } - finally - { - semaphore.Release(); - } - } - - /// - /// 同步方法:清空缓存。 - /// - public void Clear() - { - lock (semaphore) - { - keyValueDict.Clear(); - valueKeyDict.Clear(); - } - } - - /// - /// 异步方法:清空缓存。 - /// - /// - public async Task ClearAsync() - { - await semaphore.WaitAsync(); - try - { - keyValueDict.Clear(); - valueKeyDict.Clear(); - } - finally - { - semaphore.Release(); - } - } - - /// - /// Gets all key-value pairs.
- /// 获取所有的键值对。 - ///
- /// Key-value pair
键值对
- public Dictionary GetAllCache() - { - lock (semaphore) - { - // 返回完整的键值对字典 - return new Dictionary(keyValueDict); - } - } - - /// - /// Gets all key-value pairs.
- /// 获取所有的键值对。 - ///
- /// Key-value pair
键值对
- public async Task> GetAllCacheAsync() - { - await semaphore.WaitAsync(); - try - { - // 返回完整的键值对字典 - return new Dictionary(keyValueDict); - } - finally - { - semaphore.Release(); - } - } + /// + 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.cs b/TYLDDB/Utils/FastCache/ConcurrentDictionary.cs index 55a4d5f..ba98a93 100644 --- a/TYLDDB/Utils/FastCache/ConcurrentDictionary.cs +++ b/TYLDDB/Utils/FastCache/ConcurrentDictionary.cs @@ -1,6 +1,5 @@ -using System.Collections.Concurrent; +using System.Collections.Concurrent; using System.Collections.Generic; -using System.Threading.Tasks; namespace TYLDDB.Utils.FastCache { @@ -8,7 +7,7 @@ namespace TYLDDB.Utils.FastCache /// Use thread locks based on concurrent dictionaries to achieve high concurrency stability.
/// 使用基于信号量的线程锁来实现高并发的稳定性。 /// - public class ConcurrentDictionary : ICache + public class ConcurrentDictionary : Cache { /// /// Thread-safe dictionary to store cache data.
@@ -22,30 +21,19 @@ public class ConcurrentDictionary : ICache ///
/// Key
键 /// Value
- public string GetByKey(string key) + public override string GetByKey(string key) { _cache.TryGetValue(key, out var value); return value; } - /// - /// Asynchronous method to get the corresponding value by key.
- /// 异步方法:根据键获取对应的值。 - ///
- /// Key
键 - /// Value
- public async Task GetByKeyAsync(string key) - { - return await Task.FromResult(GetByKey(key)); - } - /// /// Get a list of keys that correspond to a specific value.
/// 获取与指定值对应的所有键的列表。 ///
/// Value to match
要匹配的值 /// List of keys
键的列表
- public List GetKeysByValue(string value) + public override List GetKeysByValue(string value) { var keys = new List(); foreach (var kvp in _cache) @@ -58,17 +46,6 @@ public List GetKeysByValue(string value) return keys; } - /// - /// Asynchronous method to get a list of keys that correspond to a specific value.
- /// 异步方法:获取与指定值对应的所有键的列表。 - ///
- /// Value to match
要匹配的值 - /// List of keys
键的列表
- public async Task> GetKeysByValueAsync(string value) - { - return await Task.FromResult(GetKeysByValue(value)); - } - /// /// Set a cache entry for a specified key.
/// 为指定键设置缓存项。 @@ -76,84 +53,35 @@ public async Task> GetKeysByValueAsync(string value) /// Key
键 /// Value
值 /// Whether the operation is successful.
操作是否成功。
- public bool Set(string key, string value) + public override bool Set(string key, string value) { // Using AddOrUpdate to ensure atomic insert or update operation _cache.AddOrUpdate(key, value, (existingKey, existingValue) => value); return true; } - /// - /// Asynchronous method to set a cache entry for a specified key.
- /// 异步方法:为指定键设置缓存项。 - ///
- /// Key
键 - /// Value
值 - /// Whether the operation is successful.
操作是否成功。
- public async Task SetAsync(string key, string value) - { - return await Task.FromResult(Set(key, value)); - } - /// /// Remove a cache entry by its key.
/// 根据键移除缓存项。 ///
/// Key
键 /// Whether the removal is successful.
移除操作是否成功。
- public bool RemoveByKey(string key) + public override bool RemoveByKey(string key) { return _cache.TryRemove(key, out _); } - /// - /// Asynchronous method to remove a cache entry by its key.
- /// 异步方法:根据键移除缓存项。 - ///
- /// Key
键 - /// Whether the removal is successful.
移除操作是否成功。
- public async Task RemoveByKeyAsync(string key) - { - return await Task.FromResult(RemoveByKey(key)); - } - /// /// Clear all cache entries.
/// 清空所有缓存项。 ///
- public void Clear() - { - _cache.Clear(); - } - - /// - /// Asynchronous method to clear all cache entries.
- /// 异步方法:清空所有缓存项。 - ///
- /// Asynchronous task for clearing.
清空操作的异步任务。
- public async Task ClearAsync() - { - await Task.Run(() => Clear()); - } + public override void Clear() => _cache.Clear(); /// /// Get all cache entries as a dictionary.
/// 获取所有缓存项,返回字典。 ///
/// All cache entries as a dictionary.
所有缓存项的字典。
- public Dictionary GetAllCache() - { - return new Dictionary(_cache); - } - - /// - /// Asynchronous method to get all cache entries as a dictionary.
- /// 异步方法:获取所有缓存项,返回字典。 - ///
- /// All cache entries as a dictionary.
所有缓存项的字典。
- public async Task> GetAllCacheAsync() - { - return await Task.FromResult(GetAllCache()); - } + public override Dictionary GetAllCache() => new Dictionary(_cache); } } diff --git a/TYLDDB/Utils/FastCache/SemaphoreThreadLock.cs b/TYLDDB/Utils/FastCache/SemaphoreThreadLock.cs index ff661cd..20c9e8f 100644 --- a/TYLDDB/Utils/FastCache/SemaphoreThreadLock.cs +++ b/TYLDDB/Utils/FastCache/SemaphoreThreadLock.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -9,7 +9,7 @@ namespace TYLDDB.Utils.FastCache /// Use semaphore based thread locks to achieve high concurrency stability.
/// 使用基于信号量的线程锁来实现高并发的稳定性。 ///
- public class SemaphoreThreadLock : ICache + public class SemaphoreThreadLock : Cache { private readonly Dictionary keyValueDict; // 存储键->值映射 private readonly Dictionary> valueKeyDict; // 存储值->键的映射 @@ -32,7 +32,7 @@ public SemaphoreThreadLock() /// /// Key
键 /// Value
- public string GetByKey(string key) + public override string GetByKey(string key) { lock (semaphore) { @@ -47,7 +47,7 @@ public string GetByKey(string key) /// /// Key
键 /// Value
- public async Task GetByKeyAsync(string key) + public override async Task GetByKeyAsync(string key) { await semaphore.WaitAsync(); try @@ -67,7 +67,7 @@ public async Task GetByKeyAsync(string key) /// /// Value
值 /// Key (List)
键 (List)
- public List GetKeysByValue(string value) + public override List GetKeysByValue(string value) { lock (semaphore) { @@ -85,7 +85,7 @@ public List GetKeysByValue(string value) /// /// Value
值 /// Key (List)
键 (List)
- public async Task> GetKeysByValueAsync(string value) + public override async Task> GetKeysByValueAsync(string value) { await semaphore.WaitAsync(); try @@ -108,7 +108,7 @@ public async Task> GetKeysByValueAsync(string value) /// /// /// - public bool Set(string key, string value) + public override bool Set(string key, string value) { lock (semaphore) { @@ -134,7 +134,7 @@ public bool Set(string key, string value) /// /// /// - public async Task SetAsync(string key, string value) + public override async Task SetAsync(string key, string value) { await semaphore.WaitAsync(); try @@ -164,7 +164,7 @@ public async Task SetAsync(string key, string value) /// /// /// - public bool RemoveByKey(string key) + public override bool RemoveByKey(string key) { lock (semaphore) { @@ -192,7 +192,7 @@ public bool RemoveByKey(string key) /// /// /// - public async Task RemoveByKeyAsync(string key) + public override async Task RemoveByKeyAsync(string key) { await semaphore.WaitAsync(); try @@ -223,7 +223,7 @@ public async Task RemoveByKeyAsync(string key) /// /// 同步方法:清空缓存。 /// - public void Clear() + public override void Clear() { lock (semaphore) { @@ -236,7 +236,7 @@ public void Clear() /// 异步方法:清空缓存。 /// /// - public async Task ClearAsync() + public override async Task ClearAsync() { await semaphore.WaitAsync(); try @@ -255,7 +255,7 @@ public async Task ClearAsync() /// 获取所有的键值对。 /// /// Key-value pair
键值对
- public Dictionary GetAllCache() + public override Dictionary GetAllCache() { lock (semaphore) { @@ -269,7 +269,7 @@ public Dictionary GetAllCache() /// 获取所有的键值对。 /// /// Key-value pair
键值对
- public async Task> GetAllCacheAsync() + public override async Task> GetAllCacheAsync() { await semaphore.WaitAsync(); try