Yo.StackExchange.Redis.Extensions 1.0.4

dotnet add package Yo.StackExchange.Redis.Extensions --version 1.0.4
                    
NuGet\Install-Package Yo.StackExchange.Redis.Extensions -Version 1.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="Yo.StackExchange.Redis.Extensions" Version="1.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Yo.StackExchange.Redis.Extensions" Version="1.0.4" />
                    
Directory.Packages.props
<PackageReference Include="Yo.StackExchange.Redis.Extensions" />
                    
Project file
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 Yo.StackExchange.Redis.Extensions --version 1.0.4
                    
#r "nuget: Yo.StackExchange.Redis.Extensions, 1.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 Yo.StackExchange.Redis.Extensions@1.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=Yo.StackExchange.Redis.Extensions&version=1.0.4
                    
Install as a Cake Addin
#tool nuget:?package=Yo.StackExchange.Redis.Extensions&version=1.0.4
                    
Install as a Cake Tool

Yo.StackExchange.Redis.Extensions

Yo.StackExchange.Redis.Extensions is a library that extends StackExchange.Redis allowing you a set of functionality needed by common applications. The library is signed and completely compatible with the .netcoreapp3.1, .NET Standard 2.1, .NET 5.0, .NET 6.0, .NET 7.0, .NET 8.0

Latest release is available on NuGet.

Channel Status
Nuget(Yo.Redis.Connection) NuGet Status
Nuget(Yo.Redis.DistributedCache) NuGet Status
Nuget(Yo.StackExchange.Redis.Extensions) NuGet Status

🚀 Quick start

string redisConnectionString = "127.0.0.1:6379,password={password},defaultDatabase=0,ssl=false,writeBuffer=10240,abortConnect=false";
IServiceCollection serviceCollection = new ServiceCollection();
ServiceProvider serviceProvider = _serviceCollection.AddRedisClient(option =>
{
    option.Configuration = redisConnectionString;
}).BuildServiceProvider();

IRedisClient redisClient = _serviceProvider.GetService<IRedisClient>()!;

//get
var value = await redisClient.GetAsync("key");
string value = await redisClient.GetTAsync<string>("key");
var value = await _redisClient.MGetAsync("key1", "key2");

//set
await redisClient.SetAsync("key", "value");
await redisClient.SetNxAsync("key", "value");
await redisClient.MSetAsync(new KeyValuePair<RedisKey, RedisValue>("key1", "value1"), new KeyValuePair<RedisKey, RedisValue>("key2", "value2"));

//del
await _redisClient.DelAsync("key1", "key2");

//hash
var value = await _redisClient.HGetAsync("key1", "field1");
var value = await _redisClient.HMGetAsync("key", "field1", "field2");
var value = await _redisClient.HGetAllAsync("key");

await _redisClient.HSetAsync("key", "field", "value");
await _redisClient.HSetNxAsync("key", "field", "value");
await _redisClient.HMSetAsync("key", new KeyValuePair<RedisValue, RedisValue>("field1", "value1"), new KeyValuePair<RedisValue, RedisValue>("field2", "value2");

await _redisClient.HDelAsync("key", "field1", "field2");

//list
await _redisClient.LPushAsync("key", "value1", "value2");
await _redisClient.LPushXAsync("key", "value");

await _redisClient.RPushAsync("key", "value1", "value2");
await _redisClient.RPushXAsync("key", "value");

await _redisClient.LPopAsync("key");
await _redisClient.RPopAsync("key");

await _redisClient.RPopLPushAsync("sourceKey", "destinationKey");

//set
var value = await _redisClient.SCardAsync("key");
var value = await _redisClient.SIsMemberAsync("key", "value");
var value = await _redisClient.SMembersAsync("key");
var value = await _redisClient.SRandMemberAsync("key", 1);

await _redisClient.SAddAsync("key", "value1", "value2");
var value = await _redisClient.SPopAsync("key");

//zset
var value = await _redisClient.ZCardAsync("key");
var value = await _redisClient.ZRangeAsync("key", 0, 1);
var value = await _redisClient.ZRangeByScoreAsync("key", 0, 1);
var value = await _redisClient.ZScoreAsync("key", "member");

var value = await _redisClient.ZAddAsync("key", new SortedSetEntry("value1", 1), new SortedSetEntry("value2", 2)); 
var value = await _redisClient.ZIncrbyAsync("key", "member", 1);
var value = await _redisClient.ZRemAsync("key", "value1", "value2");
var value = await _redisClient.ZRemRangeByRankAsync("key", 0, 1);

//lock
var result = await redisClient.LockAsync<string>(key, TimeSpan.FromMinutes(5), async () =>
{
    await Task.Delay(2 * 1000);
    return await Task.FromResult("true");
});

var result = await _redisClient.LockAsync<string>(key, TimeSpan.FromMinutes(5), 2, TimeSpan.FromSeconds(2), async () =>
{
    await Task.Delay(2 * 1000);
    return await Task.FromResult("true");
});

//Lua script
private const string SetScript = (@"
    redis.call('HSET', KEYS[1], 'absexp', ARGV[1], 'sldexp', ARGV[2], 'data', ARGV[4])
    if ARGV[3] ~= '-1' then
      redis.call('EXPIRE', KEYS[1], ARGV[3])
    end
    return 1");
await redisClient.ScriptEvaluateAsync(SetScript, new RedisKey[] { "key1" },
    new RedisValue[]
    {
            "value1",
            "value2",
            value3,
            "value4"
    });

//other
await _redisClient.IncrAsync("key");
await _redisClient.DelAsync("key1", "key2");
await _redisClient.ExpireAsync("key1", TimeSpan.FromSeconds(5));

💻 RedisConnection

IRedisConnection redisConnection = _serviceProvider.GetService<IRedisConnection>()!;

//StackExchange.Redis native functions
await _redisConnection.ExecuteAsync(redisClient => redisClient.StringIncrementAsync("key1", 1));

📰 DistributedCache

IDistributedCache distributedCache = _serviceProvider.GetService<IDistributedCache>()!;

await distributedCache.SetAsync("key", Encoding.UTF8.GetBytes("value"));
var value = await distributedCache.GetAsync(key);

await distributedCache.SetAsync("key", Encoding.UTF8.GetBytes("value"), new DistributedCacheEntryOptions().SetAbsoluteExpiration(DateTimeOffset.UtcNow.AddMinutes(10)));
var value = await distributedCache.GetAsync(key);

await distributedCache.SetAsync("key", Encoding.UTF8.GetBytes("value"), new DistributedCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromSeconds(5)));
var value = await distributedCache.GetAsync(key);

await distributedCache.SetAsync("key", Encoding.UTF8.GetBytes("value"), new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(5)));
var value = await distributedCache.GetAsync(key);

await _distributedCache.RemoveAsync("key");

Q&A

For questions or issues do not hesitate to raise an issue.

Contributing

Thanks to all the people who already contributed!

<a href="https://github.com/Kyle-Y-Li/Yo.StackExchange.Redis.Extensions/graphs/contributors"> <img src="https://contributors-img.web.app/image?repo=Kyle-Y-Li/Yo.StackExchange.Redis.Extensions" /> </a>

License

StackExchange.Redis is Copyright © StackExchange and other contributors under the MIT license.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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 is compatible.  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 netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.4 104 7/24/2024
1.0.3 130 7/22/2024
1.0.2 111 7/17/2024
1.0.1 110 7/13/2024
1.0.0 106 7/13/2024