Skip to content

mjebrahimi/SmartCacheManager

Repository files navigation

NuGet License: MIT Build Status

SmartCacheManager

SmartCacheManager is a response caching module which cache objects with resilient and variable expiration time that is useful for caching the result of web services and other calculations.

Features

  • • Caching objects using EasyCaching
    • Supports various caching providers such as In-Memory, Redis, Memcached, Disk, Sqlite, ...
  • • Serializing data
    • Supports various caching serializers such as BinaryFormatter, MessagePack, Json, Protobuf, ...
  • • Settings for cache expiration time by specifying the minimum and maximum duration
  • • Increasing and decreasing cache expiration time automatically based on the date of search and RPM
  • • Calculates the RPM (request per minute) of a search
  • • Ability to limit searches based on maximum limit count in a specified time range
  • • Ability to configuring cache-key dynamically based on your needs
  • Async support with cancellation tokens
  • Thread-safety support
  • • Flexible, lightweight and highly customizable
  • • Logs Sensitive Data using Serilog
    • Logs all errors occures in services
    • Logs all steps of the search and caching process
    • Logs all incomming requests to this module
    • Logs all outgoing request from this module to external services
    • Logs the execution time of service methods using MethodTimer.Fody
    • Logs to the Sql-Server (by default) and supports Console, Debug, File, EventViwer, Seq, ElasticSearch and ...

Getting Started

1. Install Package

For the .NET Core 2.2 use v1.0.0 and for the .NET Core 3.1 use v2.0.0 of SmartCacheManager:

PM> Install-Package SmartCacheManager

2. Implement your own cache-manager service

For example: A simple implementation of a FlightCacheManager could be like this:

public class FlightCacheManager : SmartCacheManager<FlightSearchModel>, IFlightCacheManager
{
    public FlightCacheManager(ICacheManager cacheManager, ILoggerFactory loggerFactory, ISystemClock systemClock,
        ICacheSettingService<CacheSetting> cacheSettingService, ISearchHistoryService<CacheSetting, LimitSetting> searchHistoryService)
        : base(cacheManager, loggerFactory, systemClock, cacheSettingService, searchHistoryService)
    {
    }
    
    protected override string GenerateSearchHistoryKey(FlightSearchModel searchModel)
    {
        return GenerateSearchResultKey(searchModel);
    }
    
    protected override string GenerateSearchResultKey(FlightSearchModel searchModel)
    {
        return $"{searchModel.Origin}-{searchModel.Destination}-{searchModel.SearchDate.ToString("yyyy-MM-dd")}";
    }
    
    protected override DateTime GetSearchDate(FlightSearchModel searchModel)
    {
        return searchModel.SearchDate;
    }
}

public class FlightSearchModel
{
    public string Origin { get; set; }
    public string Destination { get; set; }
    public DateTime SearchDate { get; set; }
}

3. Register services

Register SmartCacheManager in your DI container by calling services.AddSmartCacheManager() and also, register your own cache-manager implementation:

public static void ConfigureServices(IServiceCollection services)
{
    services.AddSmartCacheManager(opt => opt.UseSqlServer("Data Source=.;Initial Catalog=CacheManageDb;Integrated Security=true");
    services.AddScoped<FlightCacheManager>();
    //...
}

4. Initialize it

Initialize SmartCacheManager using app.InitialSmartCacheManager() in the Configure method of Startup.cs:

public static void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.InitialSmartCacheManager();
    //...

5. Use it

Inject your own implementation of cache-manager to your services and use it by calling SearchFromCacheAsync() method:

var supplier = FlightSupplierts.Amadeus; // Supplier could be any type: string, enum or ...
var searchModel = new FlightSearchModel
{
    Origin = "AMS",
    Destination = "HAM",
    SearchDate = DateTime.Now.AddDays(10)
};

var result = await _flightCacheManager.SearchFromCacheAsync(searchModel, supplier, () =>
{
    return _amadeusFlightService.SearchAsync(searchModel);
});

Contributing

Create an issue if you find a bug or have a suggestion or question. If you want to develop this project:

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request

Give a Star! ⭐️

If you find this repository useful, please give it a star. Thanks!

License/Copyright

SmartCacheManager is Copyright © 2020 Mohammd Javad Ebrahimi under the GNU GPLv3 License.

About

SmartCacheManager is a smart caching module to cache objects with resilient and variable expiration timing that is useful for caching result of web services.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages