Just.CacheIt
2.1.4
dotnet add package Just.CacheIt --version 2.1.4
NuGet\Install-Package Just.CacheIt -Version 2.1.4
<PackageReference Include="Just.CacheIt" Version="2.1.4" />
paket add Just.CacheIt --version 2.1.4
#r "nuget: Just.CacheIt, 2.1.4"
// Install Just.CacheIt as a Cake Addin #addin nuget:?package=Just.CacheIt&version=2.1.4 // Install Just.CacheIt as a Cake Tool #tool nuget:?package=Just.CacheIt&version=2.1.4
Just.CacheIt
Framework for Direct and Simple Update of Cache or Reloadable Components in .NET
Why Should I Use It?
If you want to use and refresh the cache in your application, it's a good choice to use it.
Since you only need to inherit our interface and call the dependency injection extension.
Usage
- Choose a component of your system that you want to have some type of information cached for quick access to data.
namespace MySuperApp.Services
{
public class SuperRequestedService
{
...
}
}
- Inherit from the
ICacheable
interface and call your class functions that load or update your data as needed.
namespace MySuperApp.Services
{
public class SuperRequestedService: ICacheable
{
...
public async Task Load()
{
await FillMySuperData();
}
public async Task Refresh()
{
await RefreshMySuperData();
}
}
}
The Load Method is called once when your app starts. And the Refresh Method from time to time.
By default, the refresh time is 1 minute. See the Custom Configuration topic for more details.
- Call
AddCacheIt
Service Collection Extension.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
...
services.AddSingleton<SuperRequestedService>();
...
services.AddCacheIt();
}
If you want to make custom configuration for a cacheable, call the overloaded AddCacheIt
Service Collection Extension method. This method receives a Iconfiguration
object to setup the custom refresh.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
...
services.AddSingleton<SuperRequestedService>();
...
services.AddCacheIt(_configuration);
}
See Custom Configuration Section for more details.
- Start your app and Voilá!
For simplicity the example class does not inherits from another interface but if your component/class does, there is no problem.
How It Works
The HostedService Handler
, registered via AddCacheIt
Service Collection Extension, retrieves by reflection any Type
that Inherits from ICacheable
.
It then uses the ServiceProvider to retrieve the registered services and then calls the Load method if it is an Application Start, and over the life of the application and the defined refresh interval it calls the Refresh method.
Custom Configuration
Whether your update needs to be more frequent or not, you can use a custom setting in the appsettings.json
of your project.
See the example below, that we want to refresh at each 10 minutes.
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Debug"
}
},
"AllowedHosts": "*",
...
"CacheIt":{
"RefreshInterval":"00:10:00"
}
...
}
If you want your cacheable component to be refreshed with a custom interval, you can use the follwing example.
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Debug"
}
},
"AllowedHosts": "*",
...
"CacheIt":{
"RefreshInterval":"00:10:00",
"CustomRefresh":{
"RefreshTimesByCacheableName":{
"MyCacheableComponentNameInjectedAtServices": "00:00:25",
"IMySuperCacheableComponentNameInjectedAtServices": "00:05:00"
}
}
}
...
}
Please if you are using versions lower than 2.1.3-preview, use the following example
before 2.1.3-preview the intervals were expressed in minutes.
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Debug"
}
},
"AllowedHosts": "*",
...
"CacheIt":{
"RefreshIntervalMinutes":10,
"CustomRefresh":{
"RefreshTimesByCacheableName":{
"MyCacheableComponentNameInjectedAtServices": 0.1,
"IMySuperCacheableComponentNameInjectedAtServices": 5
}
}
}
...
}
IMPORTANT : the name must match with the class name injected at IServiceCollection!
And at your services configuration
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
...
services.AddSingleton<MyCacheableComponentNameInjectedAtServices>();
services.AddSingleton<IMySuperCacheableComponentNameInjectedAtServices,MySuperCacheableComponentNameInjectedAtServices>();
...
services.AddCacheIt(_configuration);
}
_configuration refers to the IConfiguration dependency.
Examples
Changelog
Please go to this guy
Contributing
Open a branch, code and open a PR to main and request review for any of the Contributors. For now this is the main flow.
License
Licensed under the MIT License.
Product | Versions 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 was computed. 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 was computed. 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. |
-
net5.0
- Microsoft.Extensions.Configuration (>= 6.0.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 6.0.0)
- Microsoft.Extensions.DependencyInjection (>= 6.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 6.0.0)
- System.Configuration.ConfigurationManager (>= 6.0.1)
-
net6.0
- Microsoft.Extensions.Configuration (>= 6.0.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 6.0.0)
- Microsoft.Extensions.DependencyInjection (>= 6.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 6.0.0)
- System.Configuration.ConfigurationManager (>= 6.0.1)
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 |
---|---|---|
2.1.4 | 1,606 | 2/21/2023 |
2.1.4-preview | 153 | 12/27/2022 |
2.1.3-preview | 156 | 11/6/2022 |
1.1.3 | 1,337 | 7/18/2022 |
1.1.3-preview | 224 | 6/19/2022 |
1.0.3 | 1,128 | 5/22/2022 |
1.0.3-preview | 317 | 5/13/2022 |
1.0.2 | 399 | 12/6/2021 |
1.0.1 | 967 | 12/5/2021 |