SmartCacheManager
SmartCacheManager is a smart caching module to cache objects with resilient and variable expiration timing that is useful for caching result of web services.
Install / Use
/learn @mjebrahimi/SmartCacheManagerREADME
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:
- 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
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.
Related Skills
node-connect
347.9kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.7kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.9kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.9kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
