50 lines
1.7 KiB
C#
50 lines
1.7 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace RedisHelper
|
|
{
|
|
public static class RedisServiceExtension
|
|
{
|
|
public static IServiceCollection AddRedisCache(this IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
services.AddSingleton<RedisUrl>(opts =>
|
|
{
|
|
var setting = new RedisConfig();
|
|
configuration.Bind("RedisConfig", setting);
|
|
return new RedisUrl { url = setting.ToString() };
|
|
});
|
|
services.AddScoped<ICache, RedisCache>();
|
|
services.AddScoped<IDistributedLock, DistributedLock>();
|
|
return services;
|
|
}
|
|
|
|
|
|
public static IServiceCollection AddRedisCacheSingleton(this IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
services.AddSingleton<RedisUrl>(opts =>
|
|
{
|
|
var setting = new RedisConfig();
|
|
configuration.Bind("RedisConfig", setting);
|
|
return new RedisUrl { url = setting.ToString() };
|
|
});
|
|
services.AddSingleton<ICache, RedisCache>();
|
|
services.AddSingleton<IDistributedLock, DistributedLock>();
|
|
return services;
|
|
}
|
|
|
|
|
|
public static IServiceCollection AddRedisCache(this IServiceCollection services, string url)
|
|
{
|
|
services.AddSingleton<RedisUrl>(options => new RedisUrl { url = url });
|
|
services.AddSingleton<ICache, RedisCache>();
|
|
services.AddSingleton<IDistributedLock, DistributedLock>();
|
|
return services;
|
|
}
|
|
}
|
|
}
|