@@ -11,7 +11,6 @@ namespace SmartSql.Cache
1111
1212 public class CacheManager : ICacheManager
1313 {
14- private readonly ConcurrentDictionary < Guid , ConcurrentQueue < ExecutionContext > > _sessionMappedExecutionQueue ;
1514 private ConcurrentDictionary < String , IList < Configuration . Cache > > _statementMappedFlushCache ;
1615 private ConcurrentDictionary < Configuration . Cache , DateTime > _cacheMappedLastFlushTime ;
1716 private readonly Timer _timer ;
@@ -23,8 +22,11 @@ public CacheManager(SmartSqlConfig smartSqlConfig)
2322 {
2423 _smartSqlConfig = smartSqlConfig ;
2524 _logger = _smartSqlConfig . LoggerFactory . CreateLogger < CacheManager > ( ) ;
25+ smartSqlConfig . InvokeSucceedListener . InvokeSucceed += ( sender , args ) =>
26+ {
27+ HandleCache ( args . ExecutionContext ) ;
28+ } ;
2629 InitCacheMapped ( ) ;
27- _sessionMappedExecutionQueue = new ConcurrentDictionary < Guid , ConcurrentQueue < ExecutionContext > > ( ) ;
2830 _timer = new Timer ( FlushOnInterval , null , _defaultDueTime , _defaultPeriodTime ) ;
2931 }
3032 private void InitCacheMapped ( )
@@ -54,31 +56,6 @@ private void InitCacheMapped()
5456 }
5557 }
5658 }
57- public void ExecuteRequest ( ExecutionContext executionContext )
58- {
59- if ( _logger . IsEnabled ( LogLevel . Debug ) )
60- {
61- _logger . LogDebug ( "ExecuteRequest Start" ) ;
62- }
63- if ( executionContext . DbSession . Transaction == null )
64- {
65- HandleCache ( executionContext ) ;
66- }
67- else
68- {
69- if ( ! _sessionMappedExecutionQueue . TryGetValue ( executionContext . DbSession . Id , out var executionQueue ) )
70- {
71- executionQueue = new ConcurrentQueue < ExecutionContext > ( ) ;
72- _sessionMappedExecutionQueue . TryAdd ( executionContext . DbSession . Id , executionQueue ) ;
73- }
74- executionQueue . Enqueue ( executionContext ) ;
75- }
76- if ( _logger . IsEnabled ( LogLevel . Debug ) )
77- {
78- _logger . LogDebug ( "ExecuteRequest End" ) ;
79- }
80- }
81-
8259 private void FlushOnExecuted ( ExecutionContext executionContext )
8360 {
8461 if ( _logger . IsEnabled ( LogLevel . Debug ) )
@@ -135,47 +112,8 @@ private void FlushOnInterval(object state)
135112 _logger . LogDebug ( $ "FlushOnInterval End") ;
136113 }
137114 }
138- public void BindSessionEventHandler ( IDbSession dbSession )
139- {
140- dbSession . Committed += DbSession_Committed ;
141- dbSession . Disposed += DbSession_Disposed ;
142- dbSession . Rollbacked += DbSession_Rollbacked ;
143- }
144-
145- private void DbSession_Rollbacked ( object sender , DbSessionEventArgs eventArgs )
146- {
147- var dbSession = sender as IDbSession ;
148- _sessionMappedExecutionQueue . TryRemove ( dbSession . Id , out var exeQueue ) ;
149- }
150-
151- private void DbSession_Disposed ( object sender , DbSessionEventArgs eventArgs )
152- {
153- var dbSession = sender as IDbSession ;
154- if ( _sessionMappedExecutionQueue . TryGetValue ( dbSession . Id , out var executionQueue ) )
155- {
156- _sessionMappedExecutionQueue . TryRemove ( dbSession . Id , out var exeQueue ) ;
157- }
158- }
159-
160- private void DbSession_Committed ( object sender , DbSessionEventArgs eventArgs )
161- {
162- var dbSession = sender as IDbSession ;
163- if ( _sessionMappedExecutionQueue . TryGetValue ( dbSession . Id , out var executionQueue ) )
164- {
165- HandleCacheQueue ( executionQueue ) ;
166- _sessionMappedExecutionQueue . TryRemove ( dbSession . Id , out var exeQueue ) ;
167- }
168- }
169-
170- private void HandleCacheQueue ( ConcurrentQueue < ExecutionContext > executionQueue )
171- {
172- while ( executionQueue . TryDequeue ( out var executionContext ) )
173- {
174- HandleCache ( executionContext ) ;
175- }
176- }
177115
178- private void HandleCache ( ExecutionContext executionContext )
116+ public void HandleCache ( ExecutionContext executionContext )
179117 {
180118 FlushOnExecuted ( executionContext ) ;
181119 var cache = executionContext . Request . Cache ;
0 commit comments