1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- using CSRedis;
- using Microsoft.Extensions.Caching.Distributed;
- using Microsoft.Extensions.Caching.Redis;
- using Microsoft.Extensions.DependencyInjection;
- using System.Collections.Generic;
- using System.Linq;
- namespace Fuel.Infrastructure.Extension
- {
-
-
-
- public static class RedisServiceCollectionExtension
- {
- public static IServiceCollection AddRedisClients(this IServiceCollection services, RedisOptions options)
- {
- var redisConfig = options;
- CSRedisClient redisClient = null;
- List<string> panams = new List<string>();
- if (redisConfig != null)
- {
-
-
- if (redisConfig.Sentinels != null && redisConfig.Sentinels.Any())
- {
-
- redisClient =
- new CSRedisClient(
- $"{redisConfig.ConnectionString},defaultDatabase={redisConfig.DataBase},prefix={redisConfig.PrefixKey}",
- redisConfig.Sentinels);
- }
- else
- {
-
- redisClient =
- new CSRedisClient(
- $"{redisConfig.ConnectionString},password={redisConfig.Password},defaultDatabase={redisConfig.DataBase},prefix={redisConfig.PrefixKey}");
- }
- }
- CoreRedisHelper.AddClient(options.KeyName, redisClient);
- if (CoreRedisHelper.redisHelpers.Any() && CoreRedisHelper.redisHelpers.Count <= 1)
- {
- RedisHelper.Initialization(redisClient);
- services.AddSingleton<IDistributedCache>(new CSRedisCache(RedisHelper.Instance));
- }
- return services;
- }
-
-
-
-
-
- public static IServiceCollection UseRedisClient(this IServiceCollection services, RedisOptions redisOptions )
- {
-
-
- services.AddRedisClients(redisOptions);
- return services;
- }
-
-
-
-
-
- public static IServiceCollection UseRedisClients(this IServiceCollection services,List<RedisOptions> redisClients)
- {
- if (redisClients.Any())
- {
- redisClients = redisClients.OrderByDescending(p => p.Default).ToList();
- foreach (var client in redisClients)
- {
- if (!string.IsNullOrWhiteSpace(client.KeyName))
- {
- services.AddRedisClients(client);
- }
- }
- }
- return services;
- }
- }
- }
|