SharpRedis 0.0.7.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package SharpRedis --version 0.0.7.2                
NuGet\Install-Package SharpRedis -Version 0.0.7.2                
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="SharpRedis" Version="0.0.7.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SharpRedis --version 0.0.7.2                
#r "nuget: SharpRedis, 0.0.7.2"                
#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.
// Install SharpRedis as a Cake Addin
#addin nuget:?package=SharpRedis&version=0.0.7.2

// Install SharpRedis as a Cake Tool
#tool nuget:?package=SharpRedis&version=0.0.7.2                

<h1 align="center">⚔️ SharpRedis</h1> <p align="center"> <span>English</span> |
<a href="./README_zh_cn.md">中文</a> </p> <p align="center"> <b>SharpRedis</b> is a high-performance Redis driver implemented in C#, designed to solve common developer pain points—<b>No more Timeout issues</b>, <b>No more data packet misalignment</b>, and no limitations on older .NET versions. If you're facing these problems, try SharpRedis today! </p>


✨ Why Choose SharpRedis?

SharpRedis offers the following exceptional features to help you handle complex Redis needs with ease:

  • 🚀 Asynchronous Driver: Fully based on an asynchronous programming model, taking full advantage of modern .NET capabilities to deliver high performance and responsiveness.

  • 🛠️ Wide Framework Support: From .NET Framework 3.0 to .NET 8.0, SharpRedis covers almost all .NET versions, making it suitable for a wide range of project requirements.

  • ⚡ Fully Asynchronous Event-Driven: Ensures all operations are handled via asynchronous event processing, designed for high concurrency scenarios, maintaining top-tier performance even under heavy traffic and high throughput.

  • ✔️ Perfect Asynchronous Support: Since .NET Framework 4.0+, all SharpRedis methods support asynchronous operations, making it easy to handle complex non-blocking tasks.

  • 🏎️ Redis Local Caching Support: Designed for efficient read operations, fully compatible with the local caching feature introduced in Redis 6.x, significantly improving data access speed.

  • 📦 Supports Latest Redis Commands: SharpRedis stays up to date with Redis version updates, fully supporting up to Redis 7.4.0, including the latest commands like Hash expiry, ensuring you're always working with the latest features.

  • 📡 Pipeline Command Support: Supports Redis's pipeline command feature, allowing you to execute multiple commands in batches, reducing network latency and improving overall performance.

  • 🛡️ Command Isolation: Commands are not mixed up; each command type is isolated. For example, use redis.String.Get... to operate on strings, and redis.Hash.HSet... to work with hashes.

  • 🐦‍🔥 Flexible Cancellation or Timeout: All methods support the passing of a CancellationToken, allowing for graceful operation cancellations.

  • 🧮 Span<char> Support: Span is a high-performance feature of .NET. If you've transformed a string into Span<char> for calculation or slicing, it can be directly stored in Redis without generating additional strings.


💼 Who Should Use SharpRedis?

SharpRedis is designed for all .NET developers as a Redis client, providing a powerful, flexible, and efficient solution, particularly suitable for scenarios that require asynchronous programming and high concurrency. Whether you're handling large-scale data storage or building an efficient distributed cache, SharpRedis offers an ideal solution.

🚀 Quick Start

using SharpRedis;

var redis = Redis.UseStandalone("host=127.0.0.1,port=6379");
redis.String.Set("key1", "key1");

var get = redis.String.Get("key1");

// redis.Hash operates on Hashes
// redis.PubSub operates on Publish/Subscribe
// redis.Connection operates on connection (currently limited, more features will be added)
// redis.List operates on Lists
// redis.Bitmap operates on Bitmaps (though essentially strings, they are distinguished)
// redis.Set operates on Sets
// redis.SortedSet operates on Sorted Sets
// redis.Stream operates on Streams
// redis.HyperLogLog operates on HyperLogLogs
// redis.Geospatial operates on GEO types
// redis.Script operates on LUA scripts and functions
// redis.Key operates on Redis keys
// redis.Server operates on Redis server (currently limited, more features will be added)

🚀 Using with Microsoft.Extensions.DependencyInjection

// Install dependency
// Install-Package SharpRedis.DependencyInjection

using SharpRedis.DependencyInjection;

// Register SharpRedis
services.AddSharpRedisStandalone("host=127.0.0.1,port=6379");
// Register SharpRedis with local cache support
services.AddSharpRedisStandalone<LocalCache>("host=127.0.0.1,port=6379");

// Named registration
// Requires Microsoft.Extensions.DependencyInjection.Abstractions 8.0.0 or higher
// The required NuGet package is SharpRedis.DependencyInjectionKeyedService
// Install-Package SharpRedis.DependencyInjectionKeyedService
services.AddSharpRedisStandalone("host=127.0.0.1,port=6379", serviceName: "named");

🚀 Using with Autofac

// Install dependency
// Install-Package SharpRedis.Autofac

using SharpRedis.Autofac;

// Register SharpRedis
containerBuilder.AddSharpRedisStandalone("host=127.0.0.1,port=6379");
// Register SharpRedis with local cache support
containerBuilder.AddSharpRedisStandalone<LocalCache>("host=127.0.0.1,port=6379");

// Named registration
containerBuilder.AddSharpRedisStandalone("host=127.0.0.1,port=6379", serviceName: "named");

🚀 Using with Unity

// Install dependency
// Install-Package SharpRedis.Unity

using SharpRedis.Unity;

// Register SharpRedis
unityContainer.AddSharpRedisStandalone("host=127.0.0.1,port=6379");
// Register SharpRedis with local cache support
unityContainer.AddSharpRedisStandalone<LocalCache>("host=127.0.0.1,port=6379");

// Named registration
unityContainer.AddSharpRedisStandalone("host=127.0.0.1,port=6379", serviceName: "named");

⚙️ Connection String Configuration

Option Default Value Description
host 127.0.01 Redis server host address
port 6379 Redis server port number
password null Redis password (optional if not set)
user null Redis user (optional if not set)
encoding utf-8 Data encoding protocol
connectname null Connection name prefix
prefix null Key prefix, applied to all key operations if set
defaultdatabase 0 Default database connection
maxpoolsize 100 Maximum pool size; it's not recommended to exceed 300
minpoolsize 3 Minimum pool size; it's not recommended to set this too high, as it will maintain many connections
commandtimeout 60000 Global execution timeout in milliseconds; flexible control via method's CancellationToken is recommended instead of adjusting this
idletimeout 30000 Idle connection reclaim time in milliseconds
subconcurrency 5 Maximum number of subscriptions per connection; additional subscriptions will create new connections
resp 2 RESP protocol version (2 or 3). Version 3 requires Redis 6.x or higher
buffer 4096 (4kb) Buffer size, not recommended to adjust unless your Redis data exceeds 4kb per read

Complete example: host=127.0.0.1,port=6379,password=123456,user=redis,encoding=utf-8,connectname=abc,prefix=myprefix,defaultdatabase=0,maxpoolsize=100,minpoolsize=3,commandtimeout=60000,idletimeout=30000,subconcurrency=5,resp=3,buffer=4096

Options can be in any order

If you don't want to use a connection string, you can configure it via code

var redis = Redis.UseStandalone(option =>
{
	option.Host = "127.0.01";
	option.Port = 6379;
});

🔗 Nuget

Package NuGet Downloads
SharpRedis nuget stats
SharpRedis.Autofac nuget stats
SharpRedis.Unity nuget stats
SharpRedis.DependencyInjection nuget stats
SharpRedis.DependencyInjectionKeyedService nuget stats

📡 Using Command Pipelining

using var pipe = redis.BeginPipelining();
_ = pipe.String.Set("key", "value");
_ = pipe.String.Get("key");
_ = pipe.Hash.HGet("hash", "field");
var result = pipe.ExecutePipelining();

📡 Temporary Database Switching

using var db = redis.SwitchDatabase(1);
db.String.Get("key");
// After switching databases, you can continue to use pipelining
using var pipe = db.BeginPipelining();
...
var result = pipe.ExecutePipelining();

⏳ Using Transactions

using var tran = redis.UseTransaction();
tran.String.Set("key", "value");
...
var result = tran.Exec(); // Execute the transaction

🏎️ Enabling Local Cache Support

var redis = Redis.UseStandalone(option =>
{
    option.Host = "127.0.0.1";
    option.Port = 6379;
    option.Password = "123456";
    option.SetClientSideCaching(new LocalCache());
});

// Custom local cache implementation
public class LocalCache : ClientSideCachingStandard
{
    private readonly MemoryCache _cache;

    public LocalCache()
    {
        this._cache = new MemoryCache(new MemoryCacheOptions { });
    }

    public override ClientSideCachingMode Mode => ClientSideCachingMode.Default;

    public override string[]? KeyPatterns => ["localcache_test*"];

    public override string[]? WithoutKeyPatterns => ["nocache*"];

    public override string[]? KeyPrefixes => ["localcache_test:"];

    protected override bool Clear()
    {
        this._cache.Clear();
        return true;
    }

    protected override bool Delete(in ClientSideCacheKey key)
    {
        this._cache.Remove(key);
        return true;
    }

    protected override bool Set(in ClientSideCacheKey key, object value)
    {
        this._cache.Set(key, value);
        return true;
    }

    protected override bool TryGet(in ClientSideCacheKey key, [NotNullWhen(true)] out object? value)
    {
        return this._cache.TryGetValue(key, out value);
    }
}

❤ If this project has been helpful to you, feel free to donate. Your donation is the greatest support I can receive. Thanks to all the donors ❤

Alipay

<img src="./alipay.png" alt="Alipay" width="200" height="200">

WeChat

<img src="./wechat.png" alt="WeChat" width="200" height="200">

🗄 License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  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. 
.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 is compatible. 
.NET Framework net30 is compatible.  net35 is compatible.  net40 is compatible.  net403 was computed.  net45 is compatible.  net451 was computed.  net452 was computed.  net46 is compatible.  net461 was computed.  net462 was computed.  net463 was computed.  net47 is compatible.  net471 was computed.  net472 was computed.  net48 is compatible.  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.
  • .NETFramework 3.0

    • No dependencies.
  • .NETFramework 3.5

    • No dependencies.
  • .NETFramework 4.0

  • .NETFramework 4.5

    • No dependencies.
  • .NETFramework 4.6

    • No dependencies.
  • .NETFramework 4.7

    • No dependencies.
  • .NETFramework 4.8

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net5.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on SharpRedis:

Package Downloads
SharpRedis.Autofac

A pure asynchronous driver of high performance high throughput Redis driver for C#

SharpRedis.Unity

A pure asynchronous driver of high performance high throughput Redis driver for C#

SharpRedis.DependencyInjection

A pure asynchronous driver of high performance high throughput Redis driver for C#

SharpRedis.DependencyInjectionKeyedService

A pure asynchronous driver of high performance high throughput Redis driver for C#

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.8.2 82 11/15/2024
0.0.8.1 72 11/14/2024
0.0.8 76 11/14/2024
0.0.7.3 104 10/16/2024
0.0.7.2 92 10/14/2024
0.0.7.1 97 10/12/2024
0.0.7 296 9/11/2024
0.0.6.3 230 6/29/2024
0.0.6.2 104 6/29/2024
0.0.6.1 114 6/28/2024
0.0.6 104 6/26/2024
0.0.5.4 122 6/4/2024
0.0.5.3 124 5/30/2024
0.0.5.2 98 5/30/2024
0.0.5.1 123 5/24/2024
0.0.5 112 5/22/2024
0.0.4 105 5/21/2024
0.0.3 88 5/21/2024
0.0.2 110 5/21/2024
0.0.1 107 5/20/2024 0.0.1 is deprecated because it has critical bugs.

Fixed the occasional timeout BUG