using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection;
using Symend.Caching.Testing;
public static class CachingServiceCollectionExtensions
public static void AddCaching(this IServiceCollection services, Action<CacheConfigurationBuilder> configure = null)
var builder = new CacheConfigurationBuilder();
configure?.Invoke(builder);
builder.Configure(services);
public class CacheConfigurationBuilder
private ICacheConfigurator _configurator;
public CacheConfigurationBuilder()
_configurator = new RedisCacheConfigurator();
public CacheConfigurationBuilder UseRedis(Action<RedisCacheConfigurator> configure = null)
var conf = new RedisCacheConfigurator();
return UseConfigurator(conf);
public CacheConfigurationBuilder UseConfigurator(ICacheConfigurator configurator)
_configurator = configurator;
public void Configure(IServiceCollection services)
_configurator.Configure(services);
public interface ICacheConfigurator
void Configure(IServiceCollection services);
public class RedisCacheConfigurator : ICacheConfigurator
public string ConnectionString { get; set; } = "redis";
public string InstanceName { get; set; }
public void Configure(IServiceCollection services)
Console.Out.WriteLine("Configuring StackExchangeRedisCache, ConnectionString = {0}, InstanceName = {1}", ConnectionString, InstanceName);
services.AddStackExchangeRedisCache(options => {
options.Configuration = ConnectionString;
options.InstanceName = InstanceName;
namespace Symend.Caching.Testing
public static class CacheConfigurationBuilderTestExtensions
public static CacheConfigurationBuilder UseInMemory(this CacheConfigurationBuilder builder, Action<InMemoryCacheConfigurator> configure = null)
var conf = new InMemoryCacheConfigurator();
return builder.UseConfigurator(conf);
public class InMemoryCacheConfigurator : ICacheConfigurator
public void Configure(IServiceCollection services)
Console.Out.WriteLine("Configuring InMemoryDistributedCache");
services.AddScoped<IDistributedCache, InMemoryDistributedCache>();
public class InMemoryDistributedCache : IDistributedCache
public byte[] Get(string key)
throw new NotImplementedException();
public Task<byte[]> GetAsync(string key, CancellationToken token = new CancellationToken())
throw new NotImplementedException();
public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
throw new NotImplementedException();
public Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = new CancellationToken())
throw new NotImplementedException();
public void Refresh(string key)
throw new NotImplementedException();
public Task RefreshAsync(string key, CancellationToken token = new CancellationToken())
throw new NotImplementedException();
public void Remove(string key)
throw new NotImplementedException();
public Task RemoveAsync(string key, CancellationToken token = new CancellationToken())
throw new NotImplementedException();
public static void Main()
var services = new ServiceCollection();
services.AddCaching(options => {
var provider = services.BuildServiceProvider(false);
var cache = provider.GetService<IDistributedCache>();
Console.Out.WriteLine("Resolved service = {0}", cache.GetType());