/
TwoLayerCacheContainerModule.cs
74 lines (59 loc) · 2.84 KB
/
TwoLayerCacheContainerModule.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using InfinniPlatform.Cache.Clusterization;
using InfinniPlatform.IoC;
using InfinniPlatform.MessageQueue;
namespace InfinniPlatform.Cache.IoC
{
/// <summary>
/// Dependency registration module for <see cref="InfinniPlatform.Cache" />.
/// </summary>
public class TwoLayerCacheContainerModule : IContainerModule
{
/// <summary>
/// Creates new instance of <see cref="TwoLayerCacheContainerModule"/>.
/// </summary>
public TwoLayerCacheContainerModule(TwoLayerCacheOptions options)
{
_options = options;
}
private readonly TwoLayerCacheOptions _options;
/// <inheritdoc />
public void Load(IContainerBuilder builder)
{
builder.RegisterInstance(_options).AsSelf().SingleInstance();
builder.RegisterType<InMemoryCacheFactory>().AsSelf().SingleInstance();
builder.RegisterFactory(CreateInMemoryCacheFactory).As<IInMemoryCacheFactory>().SingleInstance();
builder.RegisterType<SharedCacheFactory>().AsSelf().SingleInstance();
builder.RegisterFactory(CreateSharedCacheFactory).As<ISharedCacheFactory>().SingleInstance();
builder.RegisterType<TwoLayerCacheStateObserver>().AsSelf().SingleInstance();
builder.RegisterType<TwoLayerCacheStateObserverStub>().AsSelf().SingleInstance();
builder.RegisterFactory(CreateTwoLayerCacheStateObserver).As<ITwoLayerCacheStateObserver>().SingleInstance();
builder.RegisterType<TwoLayerCache>().As<ITwoLayerCache>().SingleInstance();
// Clusterization
builder.RegisterType<TwoLayerCacheResetKeyConsumer>().As<IConsumer>().SingleInstance();
}
private IInMemoryCacheFactory CreateInMemoryCacheFactory(IContainerResolver resolver)
{
return _options.InMemoryCacheFactory?.Invoke(resolver) ?? resolver.Resolve<InMemoryCacheFactory>();
}
private ISharedCacheFactory CreateSharedCacheFactory(IContainerResolver resolver)
{
return _options.SharedCacheFactory?.Invoke(resolver) ?? resolver.Resolve<SharedCacheFactory>();
}
private ITwoLayerCacheStateObserver CreateTwoLayerCacheStateObserver(IContainerResolver resolver)
{
var twoLayerCacheStateObserver = _options.TwoLayerCacheStateObserver?.Invoke(resolver);
if (twoLayerCacheStateObserver == null)
{
if (TwoLayerCacheStateObserver.CanBeCreated(resolver))
{
twoLayerCacheStateObserver = resolver.Resolve<TwoLayerCacheStateObserver>();
}
else
{
twoLayerCacheStateObserver = resolver.Resolve<TwoLayerCacheStateObserverStub>();
}
}
return twoLayerCacheStateObserver;
}
}
}