Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3fc1302
style: remove unused using
Grey-Wind Dec 21, 2024
e405fca
docs: change some comments
Grey-Wind Dec 21, 2024
f0f43e8
docs: add "How To Use" file
Grey-Wind Dec 22, 2024
9a0aa6c
写三个新方法用于读取不同大小的文件文件
Grey-Wind Dec 23, 2024
21800da
统一传参
Grey-Wind Dec 23, 2024
5cb8fe1
扩大缓存
Grey-Wind Dec 23, 2024
fa35dad
加入自动readfile判断
Grey-Wind Dec 23, 2024
9750411
删除无用代码
Grey-Wind Dec 24, 2024
ecc0291
移除对旧版本的支持
Grey-Wind Dec 24, 2024
c8e9a4d
优化读取代码
Grey-Wind Dec 24, 2024
692f409
优化测试代码
Grey-Wind Dec 24, 2024
41b856e
删除无法使用的测试代码
Grey-Wind Dec 24, 2024
097b1ef
优化读取代码
Grey-Wind Dec 24, 2024
364a659
优化测试代码
Grey-Wind Dec 24, 2024
d815c53
重命名文件
Grey-Wind Dec 24, 2024
a66e61e
快速缓存支持
Grey-Wind Dec 27, 2024
80eeca3
更新readme
Grey-Wind Dec 27, 2024
4920f4e
更新版本号
Grey-Wind Dec 27, 2024
9c48a8b
feat: 使用接口
Grey-Wind Dec 27, 2024
a218cc1
refactor: 代码略微优化
Grey-Wind Dec 27, 2024
870ca7f
docs: 更新版本号
Grey-Wind Dec 27, 2024
3449d16
docs: 调整打包设置
Grey-Wind Dec 27, 2024
54268c9
feat: 尝试使用更快的并发词典
Grey-Wind Dec 27, 2024
dd32742
Merge branch 'main' into v1.0.0
Grey-Wind Dec 27, 2024
510cbf4
refactor: 使用抽象类来过渡接口与实现
Grey-Wind Dec 28, 2024
d12e130
docs: 添加更新日志
Grey-Wind Dec 28, 2024
9b48b27
Merge branch 'v1.0.0' of https://github.com/Grey-Wind/TYLDDB-CSharp i…
Grey-Wind Dec 28, 2024
88c56c4
Merge branch 'main' into v1.0.0
Grey-Wind Dec 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions TYLDDB/TYLDDB.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net8.0;net9.0</TargetFrameworks>
<TargetFrameworks>netstandard2.1;net6.0;net8.0;net9.0</TargetFrameworks>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>TTYPDB.NET</Title>
<Version>1.0.0-alpha.2</Version>
<Title>TYLDDB.NET</Title>
<Version>1.0.0-alpha.2r</Version>
<Authors>TYLDDB-Project</Authors>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
Expand All @@ -19,6 +19,8 @@
<PackageTags>DataBase</PackageTags>
<Company>QingYi-Studio</Company>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageReleaseNotes>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.</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down
307 changes: 30 additions & 277 deletions TYLDDB/Utils/FastCache/Cache.cs
Original file line number Diff line number Diff line change
@@ -1,286 +1,39 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System;

Check warning

Code scanning / Sonarscharp (reported by Codacy)

Provide an 'AssemblyVersion' attribute for assembly 'srcassembly.dll'. Warning

Provide an 'AssemblyVersion' attribute for assembly 'srcassembly.dll'.

Check warning

Code scanning / Sonarscharp (reported by Codacy)

Add or update the header of this file. Warning

Add or update the header of this file.

Check warning

Code scanning / Sonarscharp (reported by Codacy)

Remove this unnecessary 'using'. Warning

Remove this unnecessary 'using'.

Check notice

Code scanning / Sonarscharp (reported by Codacy)

Provide a 'CLSCompliant' attribute for assembly 'srcassembly.dll'. Note

Provide a 'CLSCompliant' attribute for assembly 'srcassembly.dll'.

Check notice

Code scanning / Sonarscharp (reported by Codacy)

Provide a 'ComVisible' attribute for assembly 'srcassembly.dll'. Note

Provide a 'ComVisible' attribute for assembly 'srcassembly.dll'.
using System.Collections.Generic;
using System.Text;

Check warning

Code scanning / Sonarscharp (reported by Codacy)

Remove this unnecessary 'using'. Warning

Remove this unnecessary 'using'.
using System.Threading.Tasks;

namespace TYLDDB.Utils.FastCache
{
/// <summary>
/// Use cached key-value pairs for fast reads and writes.<br />
/// 使用缓存的键值对来快速读写
/// For cached abstract classes, you need to inherit the class to do concrete implementation.<br />
/// 对于缓存的抽象类,需要继承该类来做具体实现
/// </summary>
public class Cache
public abstract class Cache : ICache
{
private readonly Dictionary<string, string> keyValueDict; // 存储键->值映射
private readonly Dictionary<string, HashSet<string>> valueKeyDict; // 存储值->键的映射
private readonly SemaphoreSlim semaphore; // 控制并发访问

/// <summary>
/// Use cached key-value pairs for fast reads and writes.<br />
/// 使用缓存的键值对来快速读写。
/// </summary>
public Cache()
{
keyValueDict = new Dictionary<string, string>();
valueKeyDict = new Dictionary<string, HashSet<string>>();
semaphore = new SemaphoreSlim(1, 1); // 使用信号量来同步
}

/// <summary>
/// Synchronization method: Obtain the corresponding value by key.<br />
/// 同步方法:根据键获取对应的值。
/// </summary>
/// <param name="key">Key<br />键</param>
/// <returns>Value<br />值</returns>
public string GetByKey(string key)
{
lock (semaphore)
{
keyValueDict.TryGetValue(key, out var value);
return value;
}
}

/// <summary>
/// Asynchronous method: Obtains the corresponding value based on the key.<br />
/// 异步方法:根据键获取对应的值。
/// </summary>
/// <param name="key">Key<br />键</param>
/// <returns>Value<br />值</returns>
public async Task<string> GetByKeyAsync(string key)
{
await semaphore.WaitAsync();
try
{
keyValueDict.TryGetValue(key, out var value);
return value;
}
finally
{
semaphore.Release();
}
}

/// <summary>
/// Synchronization method: Obtains one or more keys according to the value.<br />
/// 同步方法:根据值获取对应的一个或多个键。
/// </summary>
/// <param name="value">Value<br />值</param>
/// <returns>Key (List)<br />键 (List)</returns>
public List<string> GetKeysByValue(string value)
{
lock (semaphore)
{
if (valueKeyDict.ContainsKey(value))
{
return valueKeyDict[value].ToList();
}
return new List<string>();
}
}

/// <summary>
/// Asynchronous method: Get one or more keys based on the value.<br />
/// 异步方法:根据值获取对应的一个或多个键。
/// </summary>
/// <param name="value">Value<br />值</param>
/// <returns>Key (List)<br />键 (List)</returns>
public async Task<List<string>> GetKeysByValueAsync(string value)
{
await semaphore.WaitAsync();
try
{
if (valueKeyDict.ContainsKey(value))
{
return valueKeyDict[value].ToList();
}
return new List<string>();
}
finally
{
semaphore.Release();
}
}

/// <summary>
/// 同步方法:设置一个键值对。
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
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<string>();
}
valueKeyDict[value].Add(key);
return true;
}
}

/// <summary>
/// 异步方法:设置一个键值对。
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public async Task<bool> 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<string>();
}
valueKeyDict[value].Add(key);
return true;
}
finally
{
semaphore.Release();
}
}

/// <summary>
/// 同步方法:移除一个键值对。
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
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;
}
}

/// <summary>
/// 异步方法:移除一个键值对。
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public async Task<bool> 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();
}
}

/// <summary>
/// 同步方法:清空缓存。
/// </summary>
public void Clear()
{
lock (semaphore)
{
keyValueDict.Clear();
valueKeyDict.Clear();
}
}

/// <summary>
/// 异步方法:清空缓存。
/// </summary>
/// <returns></returns>
public async Task ClearAsync()
{
await semaphore.WaitAsync();
try
{
keyValueDict.Clear();
valueKeyDict.Clear();
}
finally
{
semaphore.Release();
}
}

/// <summary>
/// Gets all key-value pairs.<br />
/// 获取所有的键值对。
/// </summary>
/// <returns>Key-value pair<br />键值对</returns>
public Dictionary<string, string> GetAllCache()
{
lock (semaphore)
{
// 返回完整的键值对字典
return new Dictionary<string, string>(keyValueDict);
}
}

/// <summary>
/// Gets all key-value pairs.<br />
/// 获取所有的键值对。
/// </summary>
/// <returns>Key-value pair<br />键值对</returns>
public async Task<Dictionary<string, string>> GetAllCacheAsync()
{
await semaphore.WaitAsync();
try
{
// 返回完整的键值对字典
return new Dictionary<string, string>(keyValueDict);
}
finally
{
semaphore.Release();
}
}
/// <inheritdoc/>
public abstract void Clear();
/// <inheritdoc/>
public virtual async Task ClearAsync() => await Task.Run(() => Clear());

Check warning

Code scanning / Sonarscharp (reported by Codacy)

Add '.ConfigureAwait(false)' to this call to allow execution to continue in any thread. Warning

Add '.ConfigureAwait(false)' to this call to allow execution to continue in any thread.
/// <inheritdoc/>
public abstract Dictionary<string, string> GetAllCache();

Check warning

Code scanning / Sonarscharp (reported by Codacy)

Consider making method 'GetAllCache' a property. Warning

Consider making method 'GetAllCache' a property.
/// <inheritdoc/>
public virtual async Task<Dictionary<string, string>> GetAllCacheAsync() => await Task.FromResult(GetAllCache());

Check warning

Code scanning / Sonarscharp (reported by Codacy)

Add '.ConfigureAwait(false)' to this call to allow execution to continue in any thread. Warning

Add '.ConfigureAwait(false)' to this call to allow execution to continue in any thread.
/// <inheritdoc/>
public abstract string GetByKey(string key);
/// <inheritdoc/>
public virtual async Task<string> GetByKeyAsync(string key) => await Task.FromResult(GetByKey(key));

Check warning

Code scanning / Sonarscharp (reported by Codacy)

Add '.ConfigureAwait(false)' to this call to allow execution to continue in any thread. Warning

Add '.ConfigureAwait(false)' to this call to allow execution to continue in any thread.
/// <inheritdoc/>
public abstract List<string> GetKeysByValue(string value);

Check notice

Code scanning / Sonarscharp (reported by Codacy)

Refactor this method to use a generic collection designed for inheritance. Note

Refactor this method to use a generic collection designed for inheritance.
/// <inheritdoc/>
public virtual async Task<List<string>> GetKeysByValueAsync(string value) => await Task.FromResult(GetKeysByValue(value));

Check warning

Code scanning / Sonarscharp (reported by Codacy)

Add '.ConfigureAwait(false)' to this call to allow execution to continue in any thread. Warning

Add '.ConfigureAwait(false)' to this call to allow execution to continue in any thread.
/// <inheritdoc/>
public abstract bool RemoveByKey(string key);
/// <inheritdoc/>
public virtual async Task<bool> RemoveByKeyAsync(string key) => await Task.FromResult(RemoveByKey(key));

Check warning

Code scanning / Sonarscharp (reported by Codacy)

Add '.ConfigureAwait(false)' to this call to allow execution to continue in any thread. Warning

Add '.ConfigureAwait(false)' to this call to allow execution to continue in any thread.
/// <inheritdoc/>
public abstract bool Set(string key, string value);
/// <inheritdoc/>
public virtual async Task<bool> SetAsync(string key, string value) => await Task.FromResult(Set(key, value));

Check warning

Code scanning / Sonarscharp (reported by Codacy)

Add '.ConfigureAwait(false)' to this call to allow execution to continue in any thread. Warning

Add '.ConfigureAwait(false)' to this call to allow execution to continue in any thread.
}
}
Loading
Loading