using System.Text.Json; using System.Text.Json.Serialization; using CacheManager.Core; using Microsoft.Extensions.DependencyInjection; using XF.Domain.Cache; namespace Hotline.CacheManager { public static class StartupExtensions { public static IServiceCollection AddCache(this IServiceCollection services, Action action) { var options = new CacheOptions(); action(options); services.AddCacheManagerConfiguration(cfg => { cfg .WithUpdateMode(CacheUpdateMode.Up) .WithMicrosoftMemoryCacheHandle() .And .WithRedisConfiguration("redis", options.ConnectionString, 1) // .WithRedisConfiguration("redis", d => //{ // d.WithDatabase(0).WithEndpoint("redis.fengwo.com", 6380); //}) .WithSerializer(typeof(SystemTextJsonSerializer), new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, DictionaryKeyPolicy = JsonNamingPolicy.CamelCase, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }) .WithRedisBackplane("redis") .WithRedisCacheHandle("redis", true); //cfg.AddCacheEvents(services); }) .AddCacheManager() .Configure(action) .AddSingleton(typeof(ITypedCache<>), typeof(DefaultTypedCache<>)) ; return services; } } public class CacheOptions { public string ConnectionString { get; set; } public int Port { get; set; } public string Prefix { get; set; } public bool EnableKeyspaceNotifications { get; set; } } }