Moongazing.GrpcAutoCache 1.0.0

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

Features

  • Automatic caching for gRPC calls
  • Supports Redis & MemoryCache
  • Works as an Interceptor – no need to modify existing gRPC service logic
  • Configurable expiration & cache invalidation
  • Boosts gRPC API performance by reducing redundant calls

Installation

-Install via NuGet dotnet add package Moongazing.GrpcAutoCache

-Install from Source Code git clone https://github.com/Moongazing/Moongazing.GrpcAutoCache/Moongazing.GrpcAutoCache.git cd Grpc.CacheInterceptor dotnet build

-Usage

-Register the Interceptor in Startup.cs

public void ConfigureServices(IServiceCollection services) { services.AddGrpc(); services.AddSingleton<ICacheProvider, RedisCacheProvider>(); // Use Redis for caching services.AddSingleton<IGrpcCacheManager, GrpcCacheManager>(); }

public void Configure(IApplicationBuilder app) { app.UseRouting(); app.UseEndpoints(endpoints ⇒ { endpoints.MapGrpcService<MyGrpcService>().AddInterceptors<CachingInterceptor>(); }); }

  • Use in a gRPC Service

using Grpc.Core; using System.Threading.Tasks;

public class MyGrpcService : MyGrpcServiceBase { public override Task<MyResponse> GetData(MyRequest request, ServerCallContext context) { return Task.FromResult(new MyResponse { Message = "Cached gRPC Response" }); } }

-Performance Benchmark

Our tests show up to 40% performance improvement with 100,000+ gRPC requests.

Test Scenario Cache Disabled Cache Enabled 10K Requests 220ms 50ms 100K Requests 2.1s 600ms

Configuration

Set Cache Expiration Time

Inside CachingInterceptor.cs, modify the _cacheDuration value:

private readonly TimeSpan _cacheDuration = TimeSpan.FromMinutes(5);

Manually Invalidate Cached Data

To remove cached responses for a specific request, call: await _cacheManager.InvalidateCacheAsync("method_key");

Development & Contribution

Feel free to contribute by forking the repository and opening a pull request:

git clone https://github.com/Moongazing/Moongazing.GrpcAutoCache/Moongazing.GrpcAutoCache.git
cd Moongazing.GrpcAutoCache
dotnet build
Develop & open a Pull Request! 

Frequently Asked Questions

❓ How does gRPC method caching work?

The interceptor hashes method parameters and responses to generate a unique cache key. The cached response is stored in Redis or MemoryCache, and subsequent requests fetch the cached data instead of processing the request again. ❓ How do I configure caching behavior?

Modify the cache expiration time in CachingInterceptor.cs. ❓ Does this work with all gRPC methods?

Yes! The interceptor works with Unary RPC methods. Support for Streaming RPCs is under development.

License

This project is licensed under the MIT License. © 2025 - Tunahan Ali Ozturk

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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. 
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.0 240 3/6/2025