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.
- • 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 ...
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
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; }
}
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>();
//...
}
Initialize SmartCacheManager using app.InitialSmartCacheManager()
in the Configure
method of Startup.cs
:
public static void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.InitialSmartCacheManager();
//...
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);
});
Create an issue if you find a bug or have a suggestion or question. If you want to develop this project:
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request
If you find this repository useful, please give it a star. Thanks!
SmartCacheManager is Copyright © 2020 Mohammd Javad Ebrahimi under the GNU GPLv3 License.