dotnetcore / EasyCaching

:boom: EasyCaching is an open source caching library that contains basic usages and some advanced usages of caching which can help us to handle caching more easier!
MIT License
1.95k stars 322 forks source link

Multiple Hybrid Caching Providers #447

Closed c5racing closed 1 year ago

c5racing commented 1 year ago

Description

Is it possible to use multiple hybrid caching providers? I have try adding two; but when resolving IHybridProviderFactory, I get an error resolving this service as there's more than one registered

Related code

services.AddEasyCaching(options =>
            {
                options.UseRedis(config =>
                {
                    config.DBConfig.AllowAdmin = true;
                    config.DBConfig.Endpoints.Add(new EasyCaching.Core.Configurations.ServerEndPoint(redisSettings.Host, redisSettings.Port));
                    config.SerializerName = "newtonSoft";
                    config.DBConfig.ConnectionTimeout = 10000;
                    config.EnableLogging = false;

                    if (!string.IsNullOrWhiteSpace(redisSettings.Password))
                    {
                        config.DBConfig.Password = redisSettings.Password;
                        config.DBConfig.IsSsl = true;
                    }
                }, "redis-global").WithJson("newtonSoft").WithCompressor("newtonSoft", "lz4")

                     .UseInMemory(config =>
                     {
                         config.DBConfig = new InMemoryCachingOptions
                         {
                             ExpirationScanFrequency = 60,
                             SizeLimit = 20000,
                             EnableReadDeepClone = true,
                             EnableWriteDeepClone = true,
                         };
                         config.MaxRdSecond = 120;
                         config.EnableLogging = false;
                         config.LockMs = 5000;
                         config.SleepMs = 300;
                     }, "memory-global")

                    .UseHybrid(config =>
                    {
                        config.TopicName = "topic-global";
                        config.EnableLogging = false;
                        config.LocalCacheProviderName = "memory-global";
                        config.DistributedCacheProviderName = "redis-global";
                    }, "hybrid-global")

                    .WithRedisBus(busConf =>
                    {
                        busConf.AllowAdmin = true;
                        busConf.Endpoints.Add(new ServerEndPoint(redisSettings.Host, redisSettings.Port));
                        busConf.SerializerName = "newtonSoft";
                        if (!string.IsNullOrWhiteSpace(redisSettings.Password))
                        {
                            busConf.Password = redisSettings.Password;
                            busConf.IsSsl = true;
                        }
                    });

                options.UseRedis(config =>
                {
                    config.DBConfig.AllowAdmin = true;
                    config.DBConfig.Endpoints.Add(new EasyCaching.Core.Configurations.ServerEndPoint(redisSettings.Host, redisSettings.Port));
                    config.SerializerName = "newtonSoft";
                    config.DBConfig.ConnectionTimeout = 10000;
                    config.EnableLogging = false;

                    if (!string.IsNullOrWhiteSpace(redisSettings.Password))
                    {
                        config.DBConfig.Password = redisSettings.Password;
                        config.DBConfig.IsSsl = true;
                    }
                }, $"redis-{serviceName}").WithJson("newtonSoft").WithCompressor("newtonSoft", "lz4")

                     .UseInMemory(config =>
                     {
                         config.DBConfig = new InMemoryCachingOptions
                         {
                             ExpirationScanFrequency = 60,
                             SizeLimit = 20000,
                             EnableReadDeepClone = true,
                             EnableWriteDeepClone = true,
                         };
                         config.MaxRdSecond = 120;
                         config.EnableLogging = false;
                         config.LockMs = 5000;
                         config.SleepMs = 300;
                     }, $"memory-{serviceName}")

                    .UseHybrid(config =>
                    {
                        config.TopicName = $"topic-{serviceName}";
                        config.EnableLogging = false;
                        config.LocalCacheProviderName = $"memory-{serviceName}";
                        config.DistributedCacheProviderName = $"redis-{serviceName}";
                    }, $"hybrid-{serviceName}")

                    .WithRedisBus(busConf =>
                    {
                        busConf.AllowAdmin = true;
                        busConf.Endpoints.Add(new ServerEndPoint(redisSettings.Host, redisSettings.Port));
                        busConf.SerializerName = "newtonSoft";
                        if (!string.IsNullOrWhiteSpace(redisSettings.Password))
                        {
                            busConf.Password = redisSettings.Password;
                            busConf.IsSsl = true;
                        }
                    });
            });

Expected behavior: [What you expected to happen]

I can resolve the service and get the provider.

Actual behavior: [What actually happened]

When trying to get IHybridProviderFactory, I get "InvalidOperationException: Sequence contains more than one matching element"

Autofac.Core.DependencyResolutionException: 'An exception was thrown while activating EasyCaching.Core.DefaultHybridProviderFactory -> λ:EasyCaching.Core.IHybridCachingProvider[] -> λ:EasyCaching.Core.IHybridCachingProvider -> λ:EasyCaching.Core.Bus.IEasyCachingBus.'

var hybridFactory = serviceProvider.GetRequiredService<IHybridProviderFactory>(); hybridFactory..GetHybridCachingProvider("hybrid-global");

c5racing commented 1 year ago

This is resolved. I didn't provide a unique name for the RedisBus Configuration.

```
        .WithRedisBus(busConf =>
            {
                busConf.AllowAdmin = true;
                busConf.Endpoints.Add(new ServerEndPoint(redisSettings.Host, redisSettings.Port));
                busConf.SerializerName = "newtonSoft";
                if (!string.IsNullOrWhiteSpace(redisSettings.Password))
                {
                    busConf.Password = redisSettings.Password;
                    busConf.IsSsl = true;
                }
            }, $"bus-{serviceName}")