-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathLazyCacheModule.cs
36 lines (31 loc) · 1.26 KB
/
LazyCacheModule.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 LazyCache.Providers;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using Ninject;
using Ninject.Modules;
namespace LazyCache
{
public class LazyCacheModule : NinjectModule
{
private readonly Func<IAppCache> implementationFactory;
public LazyCacheModule()
{
}
public LazyCacheModule(Func<IAppCache> implementationFactory)
{
this.implementationFactory = implementationFactory;
}
// See also https://github.com/aspnet/Caching/blob/dev/src/Microsoft.Extensions.Caching.Memory/MemoryCacheServiceCollectionExtensions.cs
public override void Load()
{
Bind<IOptions<MemoryCacheOptions>>().ToConstant(Options.Create(new MemoryCacheOptions()));
Bind<ICacheProvider>().To<MemoryCacheProvider>().InSingletonScope()
.WithConstructorArgument<Func<IMemoryCache>>(context => () => new MemoryCache(context.Kernel.Get<IOptions<MemoryCacheOptions>>()));
if (implementationFactory == null)
Bind<IAppCache>().To<CachingService>().InSingletonScope();
else
Bind<IAppCache>().ToMethod(context => implementationFactory()).InSingletonScope();
}
}
}