添加项目文件。
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user