Jack.RedisLib
1.0.0.4
dotnet add package Jack.RedisLib --version 1.0.0.4
NuGet\Install-Package Jack.RedisLib -Version 1.0.0.4
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Jack.RedisLib" Version="1.0.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Jack.RedisLib" Version="1.0.0.4" />
<PackageReference Include="Jack.RedisLib" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Jack.RedisLib --version 1.0.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Jack.RedisLib, 1.0.0.4"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Jack.RedisLib@1.0.0.4
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Jack.RedisLib&version=1.0.0.4
#tool nuget:?package=Jack.RedisLib&version=1.0.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
单实例示例
IServiceCollection services = new ServiceCollection();
services.AddLogging();
services.AddRedisClient(config =>
{
config.ConnectionString = "127.0.0.1:6379";
config.Database = 0;
config.Debug = true;
config.InstanceName = "ProjectName:";
});
IServiceProvider serviceProvider = services.BuildServiceProvider();
IRedisClient redisClient = serviceProvider.GetRequiredService<IRedisClient>();
多实例示例
IServiceCollection services = new ServiceCollection();
services.AddLogging();
Dictionary<string, Action<RedisOptions>> configOptions = new Dictionary<string, Action<RedisOptions>>
{
["Project:AAA"] = (config) =>
{
config.ConnectionString = "127.0.0.1:6379";
config.Database = 0;
config.Debug = true;
config.InstanceName = "Project:AAA:";
},
["Project:BBB"] = (config) =>
{
config.ConnectionString = "127.0.0.1:6379";
config.Database = 0;
config.Debug = true;
config.InstanceName = "Project:BBB:";
},
};
services.AddRedisClientFactory(configOptions);
IServiceProvider serviceProvider = services.BuildServiceProvider();
IRedisClientFactory redisClientFactory = serviceProvider.GetRequiredService<IRedisClientFactory>();
var clientAAA = redisClientFactory.Create("Project:AAA");
var clientBBB = redisClientFactory.Create("Project:BBB");
简单使用
string key = "CurrentTime";
DateTime value = DateTime.Now;
await redisClient.StringSet(key, value, TimeSpan.FromMinutes(5));
DateTime? cacheValue = await redisClient.StringGetForValueType<DateTime>(key);
cacheValue = await redisClient.StringGetDeleteForValueType<DateTime>(key);
string key = "StringValue";
string value = "Redis";
await redisClient.StringSet(key, value, TimeSpan.FromMinutes(5));
string cacheValue = await redisClient.StringGet<string>(key);
string key = "StringDecrement";
long? value = await redisClient.StringDecrement(key);
value = await redisClient.StringDecrement(key, -1);
value = await redisClient.StringGetDeleteForValueType<long>(key);
string key = "StringDecrement";
double? value = await redisClient.StringDecrement(key, 0.1);
value = await redisClient.StringDecrement(key, -0.1);
value = await redisClient.StringGetDeleteForValueType<double>(key);
string lockName = "lockName";
string lockToken = "lockToken";
TimeSpan expiry = TimeSpan.FromSeconds(2);
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
Console.CancelKeyPress += (o, e) =>
{
cancellationTokenSource.Cancel();
};
CancellationToken cancellationToken = cancellationTokenSource.Token;
bool executeResult = await redisClient.LockWithExtend(lockName, expiry, async () =>
{
Console.WriteLine($"开始:{lockToken}\t{DateTime.Now}");
await Task.Delay(3000, cancellationToken);
Console.WriteLine($"结束:{lockToken}\t{DateTime.Now}");
}, lockToken);
Console.WriteLine($"{lockToken}:{(executeResult ? "执行成功" : "执行失败")}");
List<Task<bool>> tasksForBool = new();
List<Task<long>> tasksForLong = new();
await redisClient.BatchExecute(async client =>
{
string key = redisClient.GetKeyForInstance("BatchKey");
tasksForBool.Add(client.StringSetAsync(key, 1));
tasksForBool.Add(client.StringSetAsync(key, 3));
tasksForBool.Add(client.StringSetAsync(key, -1, TimeSpan.FromSeconds(60)));
tasksForLong.Add(client.StringIncrementAsync(key, -1));
tasksForLong.Add(client.StringIncrementAsync(key, 2));
await Task.CompletedTask;
});
await Task.WhenAll(tasksForBool);
await Task.WhenAll(tasksForLong);
foreach (var taskForBool in tasksForBool)
{
Console.WriteLine(taskForBool.Result);
}
foreach (var taskForLong in tasksForLong)
{
Console.WriteLine(taskForLong.Result);
}
IRedisClient clientAAA = redisClientFactory.Create("Project1");
IRedisClient clientBBB = redisClientFactory.Create("Project2");
await clientAAA.StringSet("TransactionExecute_Name", "A");
await clientAAA.StringSet("TransactionExecute_Age", "18");
List<Task> tasks = new List<Task>
{
ExecuteAAA(clientAAA),
ExecuteBBB(clientBBB),
};
await Task.WhenAll(tasks);
// -----------------------------------------------------------------------------------------------------
async Task ExecuteAAA(IRedisClient clientAAA)
{
string TransactionExecute_Name = await clientAAA.StringGet<string>("TransactionExecute_Name");
string TransactionExecute_Age = await clientAAA.StringGet<string>("TransactionExecute_Age");
List<Task<bool>> tasks = new List<Task<bool>>();
var result = await clientAAA.TransactionExecute(async (tran) =>
{
var result = tran.AddCondition(Condition.StringEqual(clientAAA.GetKeyForInstance("TransactionExecute_Name"), TransactionExecute_Name));
tasks.Add(tran.StringSetAsync(clientAAA.GetKeyForInstance("TransactionExecute_Name"), "C"));
tasks.Add(tran.StringSetAsync(clientAAA.GetKeyForInstance("TransactionExecute_Age"), 12));
await Task.Delay(500);
});
Console.WriteLine($"{result.ExecuteResult}\t{result.Exception}");
if (result.ExecuteResult)
{
await Task.WhenAll(tasks);
foreach (var task in tasks)
{
Console.WriteLine(task.Result);
}
}
}
// -----------------------------------------------------------------------------------------------------
async Task ExecuteBBB(IRedisClient clientBBB)
{
await clientBBB.StringSet("TransactionExecute_Name", "B");
}
多重锁
await using IMultiRedisLock multiRedisLock = serviceProvider.GetRequiredService<IMultiRedisLock>();
for (int i = 0; i < 5; i++)
{
await multiRedisLock.AddLockAsync(Guid.NewGuid().ToString("n"), TimeSpan.FromSeconds(10));
}
if (multiRedisLock.IsAcquired == false)
{
_logger.LogWarning("获取锁失败");
return;
}
var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(60)); // 60秒后主动取消
CancellationToken cancellationToken = cancellationTokenSource.Token;
do
{
await Task.Delay(1000);
_logger.LogWarning("获取锁成功,任务执行中");
} while (cancellationToken.IsCancellationRequested == false);
cancellationTokenSource.Dispose();
_logger.LogWarning("任务执行完毕");
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Options (>= 8.0.2)
- Newtonsoft.Json (>= 13.0.3)
- RedLock.net (>= 2.3.2)
- StackExchange.Redis (>= 2.8.22)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Jack.RedisLib:
Package | Downloads |
---|---|
SmartCloud.Common.Domain
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.