-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServiceCollectionExtensions.cs
36 lines (33 loc) · 1.57 KB
/
ServiceCollectionExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using StackExchange.Redis;
using Newtonsoft.Json;
using CacheManager.Core;
using Jazea.Caching;
namespace Microsoft.Extensions.DependencyInjection
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddCaching(this IServiceCollection services, RedisConnection redisConnection, TimeSpan timeout)
{
var configurationOptions = ConfigurationOptions.Parse(redisConnection.ToString());
var cacheManagerConfiguration = new ConfigurationBuilder()
.WithJsonSerializer(new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
}, new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
})
//.WithUpdateMode(CacheUpdateMode.Up)
.WithRedisConfiguration("Redis", ConnectionMultiplexer.Connect(configurationOptions))
.WithRedisCacheHandle("Redis")
.WithExpiration(ExpirationMode.None, timeout)
.Build();
services.AddTransient(typeof(ICacheManagerConfiguration), x => cacheManagerConfiguration);
services.AddSingleton(typeof(ICacheManager<>), typeof(BaseCacheManager<>));
return services;
}
}
}