How q7164518/sharpredis works
Project type and purpose
• SharpRedis is a high-performance, fully asynchronous C# Redis driver/library, supporting .NET 3.0 through .NET 8. It is not a web app or microservice—it’s a client‐side SDK that applications reference to communicate with a Redis server.
File structure
• Top-level directories:
• src/SharpRedis: the core library (Commands, Provider, Network, Models, Extensions, Values)
• src/SharpRedis.* (Autofac, DependencyInjection, DependencyInjectionKeyedService, Unity): DI/IoC integration projects
• examples/: sample console applications demonstrating usage on multiple .NET versions
• test/: automated tests against the provider and data types
• Core patterns:
• Asynchronous, event-driven API
• Command modules separated by Redis data type (String, Hash, List, etc.)
• Provider layer (Calls → Redis operations)
• Network layer (Connection, ConnectionPool, Master-Slave pool)
• Extensions for older .NET versions and extra behaviors (e.g., client-side caching)
• Dependency Injection registration extensions
README for additional insights
• Lists key features: asynchronous driver, local caching, pipeline/transaction support, DI/IoC integration, broad .NET coverage
• Shows usage snippets for standalone, pipeline, transaction, switching DB, local cache, DI libraries
Main components
a. Frontend/Consumer
• Example Applications (examples/*.csproj)
• Consumer .NET application code referencing SharpRedis
b. DI/IoC Integration Layer
• SharpRedis.DependencyInjection
• SharpRedis.DependencyInjectionKeyedService
• SharpRedis.Autofac
• SharpRedis.Unity
• These projects expose RegistrationExtensions to inject SharpRedis into IServiceCollection, Autofac ContainerBuilder, or UnityContainer.
c. SharpRedis Core API Layer
• Redis (static entry point, e.g., Redis.UseStandalone, .UseCluster, .UseSentinel)
• Redis.String, Redis.Hash, Redis.List, Redis.Set, Redis.SortedSet, Redis.Stream, Redis.PubSub, Redis.Transaction, Redis.Pipeline, etc.
d. Command Modules
• src/SharpRedis/Commands/*.cs: Command-specific classes implementing each data type’s commands
e. Provider Layer
• src/SharpRedis/Provider:
• Calls (DefaultCall, PipeliningCall, TransactionCall, MasterSlaveCall, etc.)
• RedisPipelining, RedisTransaction, RedisSwitchDatabase
• Standard BaseCall, BaseRedis, and feature-specific Redis classes
• Types: RedisString, RedisHash, RedisList, RedisSet, … and their Async/Span variants
f. Network Layer
• src/SharpRedis/Network:
• DefaultConnection, SubConnection
• ConnectionPool implementations: DefaultPool, MasterSlavePool
• Interfaces: IConnection, IConnectionPool
g. Models & Values
• CommandPacket, ConnectionOptions, OnReceiveModel, standard model interfaces
• Value wrappers: BooleanValue, NumberValue, StreamValue, XInfo*Value, etc.
h. Extensions
• Client-side caching, convert extensions, data packet extensions, and legacy .NET support under Extensions folder
i. External Systems
• Redis Server(s): standalone, cluster, sentinel
Relationships and interactions (arrowed flows)
1. Consumer App → (via DI or direct call) SharpRedis.Core.Redis
2. SharpRedis.Core.Redis → Command Module (e.g., Redis.String.Set)
3. Command Module → Provider.Call (DefaultCall or specialized call)
4. Provider.Call → Network Layer (IConnectionPool.Request → IConnection.WriteAsync)
5. Network Layer → TCP connection → Redis Server
6. Redis Server → TCP response → Network Layer → Provider (parses into Value types) → returns to Consumer
7. Pipeline/Transaction: commands buffered in Provider (PipeliningCall, TransactionCall) then ExecutePipelining or Exec triggers a batch → Network
8. Local Cache Extensions intercept calls and responses if enabled (in Extensions → ClientSideCachingExtensions)
Architectural patterns & design principles
• Layered architecture: API → Commands → Provider → Network → External
• Dependency Inversion: interfaces for IConnection, IConnectionPool, BaseCall, BaseRedis
• Strategy/Factory for different call types (default, pipeline, transaction, master-slave)
• Separation of concerns: each Redis data type in its own module
• Asynchronous I/O everywhere using async/await and CancellationToken
• DI/IoC registration extensions for pluggability in .NET DI, Autofac, Unity
• Extension points for local caching, custom models, and older .NET support
Final advice
• Async patterns: annotate where async operations occur and where CancellationToken is passed.
• Pluggable parts: DI modules, custom local cache, RESP protocol version.
• Use layering and proper grouping so a reader immediately grasps the flow from application to Redis server.