12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- using EasyCaching.Core.Configurations;
- using EasyCaching.Serialization.SystemTextJson.Configurations;
- using Microsoft.Extensions.DependencyInjection;
- using XF.Domain.Cache;
- using XF.Domain.Queues;
- namespace XF.EasyCaching
- {
- public static class StartupExtensions
- {
- public static IServiceCollection AddCache(this IServiceCollection services, Action<CacheOptions> action)
- {
- var options = new CacheOptions();
- action(options);
- services.AddEasyCaching(d =>
- {
- d.WithSystemTextJson("xjson");
- // local
- d.UseInMemory("m1");
- d.UseRedis(config =>
- {
- config.DBConfig.Endpoints.Add(new ServerEndPoint(options.Host, options.Port));
- config.DBConfig.Database = options.Database;
- config.DBConfig.Password = options.Password;
- config.SerializerName = "xjson";
- }, "r1");
- // combine local and distributed
- d.UseHybrid(config =>
- {
- config.TopicName = options.TopicName;
- config.EnableLogging = false;
- // specify the local cache provider name after v0.5.4
- config.LocalCacheProviderName = "m1";
- // specify the distributed cache provider name after v0.5.4
- config.DistributedCacheProviderName = "r1";
- }, "h1")
- // use redis bus
- .WithRedisBus(busConf =>
- {
- busConf.Endpoints.Add(new ServerEndPoint(options.Host, options.Port));
- busConf.Password = options.Password;
- // do not forget to set the SerializerName for the bus here !!
- busConf.SerializerName = "xjson";
- });
- })
- .Configure(action)
- .AddSingleton(typeof(ITypedCache<>), typeof(DefaultTypedCache<>))
- .AddSingleton(typeof(IQueue), typeof(DistributedQueue))
- ;
- return services;
- }
- }
- public class CacheOptions
- {
- public string Host { get; set; }
- public int Port { get; set; }
- public string Password { get; set; }
- public int Database { get; set; } = 1;
- public string Prefix { get; set; }
- public int MaxRdSecond { get; set; }
- public string TopicName { get; set; }
- }
- }
|