Chd.Library.Caching
9.0.3
See the version list below for details.
dotnet add package Chd.Library.Caching --version 9.0.3
NuGet\Install-Package Chd.Library.Caching -Version 9.0.3
<PackageReference Include="Chd.Library.Caching" Version="9.0.3" />
paket add Chd.Library.Caching --version 9.0.3
#r "nuget: Chd.Library.Caching, 9.0.3"
// Install Chd.Library.Caching as a Cake Addin #addin nuget:?package=Chd.Library.Caching&version=9.0.3 // Install Chd.Library.Caching as a Cake Tool #tool nuget:?package=Chd.Library.Caching&version=9.0.3
Caching library for .Net Core
Chd (cleverly handle difficulty) library helps you cleverly handle difficulty, writing code fastly and do your application stable.
📝 Table of Contents
- About
- Getting Started
- Prerequisites
- Injecting caching into the project
- Apsettings Configurations
- Usage
- TODO
- Authors
- Acknowledgments
🧐 About
Data is stored in redis with the key created by combining all the input parameters of the method.
Using it for methods that retrieve data from the database where similar parameters are entered frequently will reduce database usage.
Compile-time aspects are faster than run-time aspects because they are woven into the code during the build process,reducing the overhead during execution. For these reasons, compile time aspect was used.
You must add redis server configurations in appsettings.json file. You must call UseRedis method in program.cs file.
You must use Cache attribute on method which you want to cache return value of method. Cache attribute takes a parameter that cache time in seconds.
🏁 Getting Started
Caching is an important feature that reduces database usage. This package is very simple to use with aspect oriented programming. If method is called with same parameters, method will not run. It will get data from redis database which is very fast because of getting data key value approach. If method is called with different parameters, method will run. It will store data in memory for sixty seconds. If method is called with same parameters in sixty seconds, method will not run. It will get data from memory. This approach is very useful for select methods. But we must be careful for update or insert methods. We must clear cache after update or insert operation. We should not use this approach on update or insert methods. We should use this approach on select methods which have not crud operations after select operation.
🛠️ Prerequisites
- You must use .net core 9.0 or higher
- You must execute "NuGet\Install-Package MethodBoundaryAspect.Fody -Version 2.0.150" command on Package Manager Console
- This package use redis server for caching, therefore you must install redis server on your server.
💉 Injecting caching into the project
Firstly you must call UseRedis method ("app.UseRedis();") in the program.cs file. You must use Cache attribute on method which you want to cache return value of method. Cache attribute takes a parameter that cache time in seconds.
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
....
....
app.UseRedis();
⚙️ Apsettings Configurations
You must add code below in appsettings.json
"Redis":
{
"Url": "x.x.x.x:pppp"//redis server ip and port
}
🎈 Usage
The "Cache" attribute that we wrote on a simple addition method stores the value of the method in memory for sixty seconds. This method runs after sixty seconds if the parameters in this method have not changed while it is called.
If this method get data from database, it will not get data from database in sixty seconds. It will get data from memory. In this approach we reduce database usage. Also this approach increase performance of application. But if we use this approach on update or insert methods, we must be careful. Because we can get old data from memory. We must clear cache after update or insert operation. we should not use this approach on update or insert methods. We should use this approach on select methods which have not crud operations after select operation.
It takes the key and return value formed by combining all the parameters into the redis memory. If we call the method with the same parameters, we see that it does not run again within six seconds because the method attribute is [Cache(6)].
public class CachingTestController : ControllerBase
{
public CachingTestController()
{
}
[Cache(6)] //Data is stored in redis with the key created by combining all the input parameters of the method for 6 second.
[Route("Sum")]
[HttpGet]
public int? Sum(int a, int b) //For testing on swagger or etc..
{
return a + b;
}
}
📑 Test Result
Add break point in to Sum method. You will see only one times broke down into method. Other call is brought from redis server.
⛏️ Built Using
- Redis - Redis server
✍️ Authors
- Mehmet Yoldaş - Linkedin
See also the list of contributors who participated in this project.
🎉 Acknowledgements
Thank you for using my library.
Product | Versions 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. |
-
net9.0
- Chd.Library.Common (>= 9.0.8)
- MethodBoundaryAspect.Fody (>= 2.0.150)
- Serilog.Settings.Configuration (>= 8.0.0)
- StackExchange.Redis (>= 2.7.17)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.