Giter Site home page Giter Site logo

mjebrahimi / smartcachemanager Goto Github PK

View Code? Open in Web Editor NEW
12.0 3.0 0.0 794 KB

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

License: GNU General Public License v3.0

C# 97.58% HTML 1.80% CSS 0.53% JavaScript 0.09%
cache caching flights-api iata iata-codes

smartcachemanager's Introduction

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.

smartcachemanager's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar mjebrahimi avatar moientajik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.