1
+ using System ;
2
+ using CacheManager . Core ;
3
+ using Microsoft . Extensions . Logging ;
4
+ using Microsoft . Extensions . Options ;
5
+ using Newtonsoft . Json ;
6
+ using StackExchange . Redis ;
7
+
8
+ namespace ArchitectNow . Caching
9
+ {
10
+ class CacheKeeper < T > : ICacheKeeper < T >
11
+ {
12
+ private readonly ILogger < CacheKeeper < T > > _log ;
13
+ private readonly ICacheManager < T > _distributed ;
14
+ private readonly ICacheManager < T > _inMemory ;
15
+ private bool _distributedEnabled = true ;
16
+ private readonly RedisOptions _redisOptions ;
17
+
18
+ public CacheKeeper ( ILogger < CacheKeeper < T > > log , IOptions < RedisOptions > redisOptions , IOptions < CachingOptions > cachingOptions )
19
+ {
20
+ _log = log ;
21
+ _redisOptions = redisOptions . Value ;
22
+
23
+ if ( ! cachingOptions . Value . Enabled )
24
+ {
25
+ _distributedEnabled = false ;
26
+ _inMemory = CacheFactory . Build < T > ( s => s . WithHandle ( typeof ( NoOpCacheHandler < > ) , Guid . NewGuid ( ) . ToString ( "N" ) ) ) ;
27
+ return ;
28
+ }
29
+
30
+ _inMemory = CacheFactory . Build < T > (
31
+ s => s
32
+ . WithDictionaryHandle ( )
33
+ . WithExpiration ( ExpirationMode . Sliding , TimeSpan . FromSeconds ( 5 ) ) ) ;
34
+
35
+ var multiplexer = Create ( ) ;
36
+
37
+ if ( multiplexer == null )
38
+ {
39
+ _distributedEnabled = false ;
40
+ return ;
41
+ }
42
+
43
+ multiplexer . ConnectionFailed += ( sender , args ) =>
44
+ {
45
+ _distributedEnabled = false ;
46
+
47
+ _log . LogDebug ( "Connection failed, disabling redis..." ) ;
48
+ } ;
49
+
50
+ multiplexer . ConnectionRestored += ( sender , args ) =>
51
+ {
52
+ _distributedEnabled = true ;
53
+
54
+ _log . LogDebug ( "Connection restored, redis is back..." ) ;
55
+ } ;
56
+
57
+ var jsonSerializerSettings = new JsonSerializerSettings
58
+ {
59
+ Formatting = Formatting . None
60
+ } ;
61
+
62
+ _distributed = CacheFactory . Build < T > (
63
+ s =>
64
+ {
65
+ s
66
+ . WithJsonSerializer ( jsonSerializerSettings , jsonSerializerSettings )
67
+ . WithDictionaryHandle ( )
68
+ . WithExpiration ( ExpirationMode . Absolute , TimeSpan . FromMinutes ( 30 ) )
69
+ . And
70
+ . WithRedisConfiguration ( "redis" , multiplexer )
71
+ . WithRedisCacheHandle ( "redis" ) ;
72
+ } ) ;
73
+ }
74
+
75
+ public ICacheManager < T > GetCacheManager ( )
76
+ {
77
+ if ( _distributedEnabled && _distributed != null )
78
+ {
79
+ return _distributed ;
80
+ }
81
+
82
+ return _inMemory ;
83
+ }
84
+
85
+ private IConnectionMultiplexer Create ( )
86
+ {
87
+ var isEnabled = _redisOptions . Enabled ;
88
+
89
+ if ( ! isEnabled )
90
+ {
91
+ return null ;
92
+ }
93
+
94
+ var connectionString = _redisOptions . ConnectionString ;
95
+
96
+ if ( string . IsNullOrEmpty ( connectionString ) )
97
+ {
98
+ throw new Exception ( "Missing redis connection string." ) ;
99
+ }
100
+
101
+ var configurationOptions = ConfigurationOptions . Parse ( connectionString ) ;
102
+
103
+ try
104
+ {
105
+ return ConnectionMultiplexer . Connect ( configurationOptions ) ;
106
+ }
107
+ catch ( Exception exception )
108
+ {
109
+ _log . LogError ( exception . Message ) ;
110
+ return null ;
111
+ }
112
+ }
113
+ }
114
+ }
0 commit comments