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!
Platform | Build Server | Status |
---|---|---|
AppVeyor | Windows | |
Travis | Linux/OSX |
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
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();
}
}
[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");
}
}
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 ?
Install-Package EasyCaching.Serialization.MessagePack
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();
}
}
coming soon !
public interface IDateTimeService
{
string GetCurrentUtcTime();
}
public class DateTimeService : IDateTimeService , IEasyCaching
{
[EasyCachingInterceptor(Expiration = 10)]
public string GetCurrentUtcTime()
{
return System.DateTime.UtcNow.ToString();
}
}
public class Startup
{
//...
public IServiceProvider ConfigureServices(IServiceCollection services)
{
//..
//service
services.AddTransient<IDateTimeService,DateTimeService>();
//caching
services.AddDefaultInMemoryCache();
//CastleInterceptor
return services.ConfigureCastleInterceptor();
}
}
[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();
}
}
Hybrid Caching is mainly building for combine local caching and distributed caching. It is called 2-tiers caching.
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;
});
}
[Route("api/[controller]")]
public class ValuesController : Controller
{
private readonly IHybridCachingProvider _provider;
public ValuesController(IHybridCachingProvider provider)
{
this._provider = provider;
}
[HttpGet]
public string Get()
{
_provider.xxx();
}
}
coming soon !
See sample
- Memory
- Redis
- SQLite
- Memcached
- Hybrid(Combine local caching and distributed caching)
- Get/GetAsync(with data retriever)
- Get/GetAsync(without data retriever)
- Set/SetAsync
- Remove/RemoveAsync
- Refresh/RefreshAsync
- BinaryFormatter
- MessagePack
- AspectCore
- Castle
- Configuration
- Bus
- Caching Region
- ...