Skip to content
/ cache-it Public

Framework for Direct and Simple Update of Cache Components

License

Notifications You must be signed in to change notification settings

duccl/cache-it

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Just.CacheIt_Logo.png

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

  1. 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
    {
        ...
    }
}
  1. 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.

  1. 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.

  1. 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

  1. .NET Web Api With Just.CacheIt

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.

About

Framework for Direct and Simple Update of Cache Components

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages