Skip to content

Commit 658ad5c

Browse files
committed
add InvokeSucceedListener
add support ListenInvokeSucceed
1 parent af1d338 commit 658ad5c

File tree

12 files changed

+185
-107
lines changed

12 files changed

+185
-107
lines changed

build/version.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<VersionMajor>4</VersionMajor>
44
<VersionMinor>0</VersionMinor>
5-
<VersionPatch>46</VersionPatch>
5+
<VersionPatch>48</VersionPatch>
66
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
77
</PropertyGroup>
88
</Project>

src/SmartSql/Cache/CacheManager.cs

Lines changed: 5 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

src/SmartSql/Cache/ICacheManager.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ namespace SmartSql.Cache
99
public interface ICacheManager : IDisposable
1010
{
1111
bool TryGetValue(ExecutionContext executionContext, out object cacheItem);
12-
void ExecuteRequest(ExecutionContext executionContext);
13-
void BindSessionEventHandler(IDbSession dbSession);
12+
void HandleCache(ExecutionContext executionContext);
1413
}
15-
}
14+
}

src/SmartSql/Cache/NoneCacheManager.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,23 @@ namespace SmartSql.Cache
77
{
88
public class NoneCacheManager : ICacheManager
99
{
10-
public void BindSessionEventHandler(IDbSession dbSession)
11-
{
12-
13-
}
14-
10+
1511
public void Dispose()
1612
{
1713

1814
}
1915

20-
public void ExecuteRequest(ExecutionContext executionContext)
21-
{
22-
23-
}
16+
2417

2518
public bool TryGetValue(ExecutionContext executionContext, out object cacheItem)
2619
{
2720
cacheItem = null;
2821
return false;
2922
}
23+
24+
public void HandleCache(ExecutionContext executionContext)
25+
{
26+
27+
}
3028
}
3129
}

src/SmartSql/Configuration/SmartSqlConfig.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,16 @@ public class SmartSqlConfig
3434
public IDbSessionStore SessionStore { get; set; }
3535
public IDbSessionFactory DbSessionFactory { get; set; }
3636
public ICacheManager CacheManager { get; set; }
37+
public InvokeSucceedListener InvokeSucceedListener { get; set; }
3738
public IDictionary<String, IIdGenerator> IdGenerators { get; set; }
39+
3840
public SqlMap GetSqlMap(string scope)
3941
{
4042
if (!SqlMaps.TryGetValue(scope, out var sqlMap))
4143
{
4244
throw new SmartSqlException($"Can not find SqlMap.Scope:{scope}");
4345
}
46+
4447
return sqlMap;
4548
}
4649

@@ -56,23 +59,30 @@ public SmartSqlConfig()
5659
Properties = new Properties();
5760
IdGenerators = new Dictionary<string, IIdGenerator>
5861
{
59-
{ nameof(SnowflakeId.Default), SnowflakeId.Default }
62+
{nameof(SnowflakeId.Default), SnowflakeId.Default}
6063
};
6164
DbSessionFactory = new DbSessionFactory(this);
6265
SessionStore = new DbSessionStore(DbSessionFactory);
6366
StatementAnalyzer = new StatementAnalyzer();
67+
InvokeSucceedListener = new InvokeSucceedListener();
68+
DbSessionFactory.Opened += (sender, args) =>
69+
{
70+
InvokeSucceedListener.BindDbSessionEvent(args.DbSession);
71+
};
6472
}
6573
}
74+
6675
public class Settings
6776
{
68-
public static Settings Default = new Settings
77+
public static readonly Settings Default = new Settings
6978
{
7079
IgnoreParameterCase = false,
7180
IsCacheEnabled = false,
7281
ParameterPrefix = "$"
7382
};
83+
7484
public bool IgnoreParameterCase { get; set; }
7585
public bool IsCacheEnabled { get; set; }
7686
public string ParameterPrefix { get; set; }
7787
}
78-
}
88+
}

src/SmartSql/DbSession/DbSessionFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace SmartSql.DbSession
1010
{
1111
public class DbSessionFactory : IDbSessionFactory
1212
{
13+
public event DbSessionFactoryOpenedEventHandler Opened;
1314
public SmartSqlConfig SmartSqlConfig { get; }
1415
public DbSessionFactory(SmartSqlConfig smartSqlConfig)
1516
{
@@ -19,7 +20,7 @@ public DbSessionFactory(SmartSqlConfig smartSqlConfig)
1920
public IDbSession Open()
2021
{
2122
var dbSession = new DefaultDbSession(SmartSqlConfig);
22-
SmartSqlConfig.CacheManager.BindSessionEventHandler(dbSession);
23+
Opened?.Invoke(this, new DbSessionFactoryOpenedEventArgs { DbSession = dbSession});
2324
return dbSession;
2425
}
2526

src/SmartSql/DbSession/DefaultDbSession.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class DefaultDbSession : IDbSession
2222
public event DbSessionEventHandler Committed;
2323
public event DbSessionEventHandler Rollbacked;
2424
public event DbSessionEventHandler Disposed;
25+
public event DbSessionInvokedEventHandler Invoked;
2526

2627
public Guid Id { get; }
2728
public AbstractDataSource DataSource { get; private set; }
@@ -296,6 +297,7 @@ public ExecutionContext Invoke<TResult>(AbstractRequestContext requestContext)
296297

297298
requestContext.ExecutionContext = executionContext;
298299
Pipeline.Invoke<TResult>(executionContext);
300+
Invoked?.Invoke(this,new DbSessionInvokedEventArgs{ ExecutionContext = executionContext});
299301
#endregion
300302

301303
_diagnosticListener.WriteDbSessionInvokeAfter(operationId, executionContext);
@@ -352,6 +354,7 @@ public async Task<ExecutionContext> InvokeAsync<TResult>(AbstractRequestContext
352354
}
353355
requestContext.ExecutionContext = executionContext;
354356
await Pipeline.InvokeAsync<TResult>(executionContext);
357+
Invoked?.Invoke(this,new DbSessionInvokedEventArgs{ ExecutionContext = executionContext});
355358
#endregion
356359
_diagnosticListener.WriteDbSessionInvokeAfter(operationId, executionContext);
357360
return executionContext;

src/SmartSql/DbSession/IDbSession.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ public class DbSessionEventArgs : EventArgs
1515
public static readonly DbSessionEventArgs None = new DbSessionEventArgs();
1616
}
1717
public delegate void DbSessionEventHandler(object sender, DbSessionEventArgs eventArgs);
18+
public class DbSessionInvokedEventArgs : EventArgs
19+
{
20+
public ExecutionContext ExecutionContext { get; set; }
21+
}
22+
public delegate void DbSessionInvokedEventHandler(object sender, DbSessionInvokedEventArgs eventArgs);
1823

1924
public interface IDbSession : ITransaction, IDisposable
2025
{
@@ -23,6 +28,8 @@ public interface IDbSession : ITransaction, IDisposable
2328
event DbSessionEventHandler Committed;
2429
event DbSessionEventHandler Rollbacked;
2530
event DbSessionEventHandler Disposed;
31+
event DbSessionInvokedEventHandler Invoked;
32+
2633
Guid Id { get; }
2734
DbTransaction Transaction { get; }
2835
DbConnection Connection { get; }

src/SmartSql/DbSession/IDbSessionFactory.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@
88

99
namespace SmartSql.DbSession
1010
{
11+
public class DbSessionFactoryOpenedEventArgs : EventArgs
12+
{
13+
public IDbSession DbSession { get; set; }
14+
}
15+
public delegate void DbSessionFactoryOpenedEventHandler(object sender, DbSessionFactoryOpenedEventArgs eventArgs);
16+
1117
public interface IDbSessionFactory
1218
{
19+
event DbSessionFactoryOpenedEventHandler Opened;
1320
SmartSqlConfig SmartSqlConfig { get; }
1421
IDbSession Open();
1522
IDbSession Open(String connectionString);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using Microsoft.Extensions.Logging;
4+
5+
namespace SmartSql.DbSession
6+
{
7+
public class InvokeSucceedEventArgs : EventArgs
8+
{
9+
public ExecutionContext ExecutionContext { get; set; }
10+
}
11+
12+
public delegate void InvokeSucceedEventHandler(object sender, InvokeSucceedEventArgs eventArgs);
13+
14+
public class InvokeSucceedListener
15+
{
16+
private readonly ConcurrentDictionary<Guid, ConcurrentQueue<ExecutionContext>> _sessionMappedExecutionQueue;
17+
public event InvokeSucceedEventHandler InvokeSucceed;
18+
19+
public InvokeSucceedListener()
20+
{
21+
_sessionMappedExecutionQueue = new ConcurrentDictionary<Guid, ConcurrentQueue<ExecutionContext>>();
22+
}
23+
24+
public void BindDbSessionEvent(IDbSession dbSession)
25+
{
26+
dbSession.Rollbacked += (sender, args) => { OnDbSessionRollbacked(sender as IDbSession); };
27+
dbSession.Disposed += (sender, args) => { OnDbSessionDisposed(sender as IDbSession); };
28+
dbSession.Committed += (sender, args) => { OnDbSessionCommitted(sender as IDbSession); };
29+
dbSession.Invoked += (sender, args) => { OnDbSessionInvoked(args.ExecutionContext); };
30+
}
31+
32+
private void OnDbSessionInvoked(ExecutionContext executionContext)
33+
{
34+
if (executionContext.DbSession.Transaction == null)
35+
{
36+
InvokeSucceed?.Invoke(this, new InvokeSucceedEventArgs {ExecutionContext = executionContext});
37+
}
38+
else
39+
{
40+
if (!_sessionMappedExecutionQueue.TryGetValue(executionContext.DbSession.Id, out var executionQueue))
41+
{
42+
executionQueue = new ConcurrentQueue<ExecutionContext>();
43+
_sessionMappedExecutionQueue.TryAdd(executionContext.DbSession.Id, executionQueue);
44+
}
45+
46+
executionQueue.Enqueue(executionContext);
47+
}
48+
}
49+
50+
private void OnDbSessionRollbacked(IDbSession dbSession)
51+
{
52+
_sessionMappedExecutionQueue.TryRemove(dbSession.Id, out _);
53+
}
54+
55+
private void OnDbSessionDisposed(IDbSession dbSession)
56+
{
57+
if (_sessionMappedExecutionQueue.TryGetValue(dbSession.Id, out _))
58+
{
59+
_sessionMappedExecutionQueue.TryRemove(dbSession.Id, out _);
60+
}
61+
}
62+
63+
private void OnDbSessionCommitted(IDbSession dbSession)
64+
{
65+
if (!_sessionMappedExecutionQueue.TryGetValue(dbSession.Id, out var executionQueue)) return;
66+
while (executionQueue.TryDequeue(out var executionContext))
67+
{
68+
InvokeSucceed?.Invoke(this, new InvokeSucceedEventArgs {ExecutionContext = executionContext});
69+
}
70+
71+
_sessionMappedExecutionQueue.TryRemove(dbSession.Id, out _);
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)