Skip to content

💥 EasyCaching is a open source caching library that contains basic usages and some advanced usages of caching which can help us to handle caching more easier!

License

Notifications You must be signed in to change notification settings

wjw1-Evan/EasyCaching

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

89 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EasyCaching is a open source caching library that contains basic usages and some advanced usages of caching which can help us to handle caching more easier!

Coverage Status

GitHub license

CI Build Status

Platform Build Server Status
AppVeyor Windows Build status
Travis Linux/OSX Build Status

Nuget Packages

Package Name Version Downloads
EasyCaching.Core
EasyCaching.InMemory
EasyCaching.Redis
EasyCaching.Memcached
EasyCaching.SQLite
EasyCaching.Serialization.MessagePack
EasyCaching.Interceptor.Castle
EasyCaching.Interceptor.AspectCore

Basci Usages

#1 Caching APIs usage

Step 1 : Install the package

Choose one kinds of caching type that you needs and install it via Nuget.

Install-Package EasyCaching.InMemory
Install-Package EasyCaching.Redis
Install-Package EasyCaching.SQLite
Install-Package EasyCaching.Memcached

Step 2 : Config in your Startup class

Different types of caching hvae their own way to config.

Here are samples show you how to config.

public class Startup
{
    //...
    
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        //1. In-Memory Cache
        services.AddDefaultInMemoryCache();
        
        //2. Redis Cache
        //services.AddDefaultRedisCache(option=>
        //{                
        //    option.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6379));
        //    option.Password = "";
        //});
        
        //3. Memcached Cache
        //services.AddDefaultMemcached(option=>
        //{                
        //    option.AddServer("127.0.0.1",11211);
        //    //specify the Transcoder use messagepack .
        //    option.Transcoder = new MessagePackFormatterTranscoder(new DefaultMessagePackSerializer()) ;
        //});
        
        //4. SQLite Cache
        //services.AddSQLiteCache(option=>{});
    }
    
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        //3. Memcache Cache
        //app.UseDefaultMemcached();
    
        //4. SQLite Cache
        //app.UseSQLiteCache();
    }
}

Step 3 : Write code in you controller

[Route("api/[controller]")]
public class ValuesController : Controller
{
    private readonly IEasyCachingProvider _provider;

    public ValuesController(IEasyCachingProvider provider)
    {
        this._provider = provider;
    }

    [HttpGet]
    public string Get()
    {
        //Remove
        _provider.Remove("demo");
        
        //Set
        _provider.Set("demo", "123", TimeSpan.FromMinutes(1));
            
        //Get
        var res = _provider.Get("demo", () => "456", TimeSpan.FromMinutes(1));
        
        //Get without data retriever
        var res = _provider.Get<string>("demo");
        
        //Remove Async
        await _provider.RemoveAsync("demo");
           
        //Set Async
        await _provider.SetAsync("demo", "123", TimeSpan.FromMinutes(1));   
            
        //Get Async    
        var res = await _provider.GetAsync("demo",async () => await Task.FromResult("456"), TimeSpan.FromMinutes(1));   
        
        //Get without data retriever Async
        var res = await _provider.GetAsync<string>("demo");
    }
}

#2 Caching Serializer

Serializer is mainly building for distributed caching .

Redis Caching has implemented a default serializer that uses System.Runtime.Serialization.Formatters.Binary to handle serialization and deserialization .

Memcahced Caching is based on EnyimMemcachedCore , it also has a default serializer named BinaryFormatterTranscoder .

EasyCaching.Serialization.MessagePack is an extension package providing MessagePack serialization for distributed caches

How to use ?

Step 1 : Install packages via Nuget

Install-Package EasyCaching.Serialization.MessagePack

Step 2 : Config in Startup class

public class Startup
{
    //...
    
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        //1. For Redis
        services.AddDefaultRedisCache(option=>
        {                
            option.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6379));
            option.Password = "";       
        });
        services.AddDefaultMessagePackSerializer();
        //2. For Memcached
        //services.AddDefaultMemcached(op=>
        //{                
        //    op.AddServer("127.0.0.1",11211);
        //
        //    op.Transcoder = new MessagePackFormatterTranscoder(new DefaultMessagePackSerializer()) ;
        //});
        //services.AddDefaultMessagePackSerializer();
    }
}

#3 Others

coming soon !

Advanced Usages

#1 Caching Interceptor

Step 1 : Define you service class

public interface IDateTimeService 
{        
    string GetCurrentUtcTime();
}

public class DateTimeService : IDateTimeService ,  IEasyCaching
{        
    [EasyCachingInterceptor(Expiration = 10)]
    public string GetCurrentUtcTime()
    {
        return System.DateTime.UtcNow.ToString();
    }
}

Step 2 : Config in Startup class

public class Startup
{
    //...

    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        //..

        //service 
        services.AddTransient<IDateTimeService,DateTimeService>();

        //caching
        services.AddDefaultInMemoryCache();

        //CastleInterceptor
        return services.ConfigureCastleInterceptor();
    }
}

Step 3 : Write code in you controller

[Route("api/[controller]")]
public class ValuesController : Controller
{
    private readonly IDateTimeService _service;

    public ValuesController(IDateTimeService service)
    {
        this._service = service;
    }

    [HttpGet]
    public string Get()
    {
        return _service.GetCurrentUtcTime();
    }
}

#3 Hybrid Caching

Hybrid Caching is mainly building for combine local caching and distributed caching. It is called 2-tiers caching.

Step 1 : Config

public void ConfigureServices(IServiceCollection services)
{
    //...
    //1. Add local caching
    services.AddDefaultInMemoryCacheForHybrid();
    //2. Add distributed caching
    services.AddDefaultRedisCacheForHybrid(option =>
    {
        option.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6379));
        option.Password = "";
    });
    //3. Add hybrid caching
    services.AddDefaultHybridCache();
    //4. Important step for different impls of only one interface .
    services.AddSingleton(factory =>
    {
        Func<string, IEasyCachingProvider> accesor = key =>
        {
            if(key.Equals(HybridCachingKeyType.LocalKey))
            {
                return factory.GetService<InMemoryCachingProvider>();
            }
            else if(key.Equals(HybridCachingKeyType.DistributedKey))
            {
                return factory.GetService<DefaultRedisCachingProvider>();
            }
            else
            {
                throw new KeyNotFoundException();
            }
        };
        return accesor;
    });
}

Step 2: Use

[Route("api/[controller]")]
public class ValuesController : Controller
{
    private readonly IHybridCachingProvider _provider;

    public ValuesController(IHybridCachingProvider provider)
    {
        this._provider = provider;
    }
    
    [HttpGet]    
    public string Get()
    {
        _provider.xxx();
    }
}

#4 Others

coming soon !

Examples

See sample

Todo List

Caching Providers

  • Memory
  • Redis
  • SQLite
  • Memcached
  • Hybrid(Combine local caching and distributed caching)

Basic Caching API

  • Get/GetAsync(with data retriever)
  • Get/GetAsync(without data retriever)
  • Set/SetAsync
  • Remove/RemoveAsync
  • Refresh/RefreshAsync

Serializer Extensions

  • BinaryFormatter
  • MessagePack

Caching Interceptor

  • AspectCore
  • Castle

Others

  • Configuration
  • Bus
  • Caching Region
  • ...

About

💥 EasyCaching is a open source caching library that contains basic usages and some advanced usages of caching which can help us to handle caching more easier!

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%