Skip to content

Commit ae21953

Browse files
committed
1. Fixed cache (FIFO & LRU) not removed after maximum size.
2. Optimizing lock related functions.
1 parent 4fdb831 commit ae21953

File tree

6 files changed

+44
-37
lines changed

6 files changed

+44
-37
lines changed

SmartSql.Tests/DataAccess/DataAccess_Test.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33
using System.Text;
44
using SmartSql.DataAccess;
55
using Xunit;
6+
using SmartSql.Abstractions;
67

78
namespace SmartSql.Tests.DataAccess
89
{
910

10-
public class DataAccess_Test : IDisposable
11+
public class DataAccess_Test : TestBase
1112
{
12-
private static TestDataAccess dao = new TestDataAccess();
13+
private TestDataAccess dao;
14+
public DataAccess_Test()
15+
{
16+
dao = new TestDataAccess(SqlMapper);
17+
}
18+
1319
[Fact]
1420
public void Insert()
1521
{
@@ -59,16 +65,11 @@ public void GetEntity()
5965
var entity = dao.GetEntity<long>(240162);
6066
// Assert.NotNull(entity);
6167
}
62-
63-
public void Dispose()
64-
{
65-
MapperContainer.Instance.Dispose();
66-
}
6768
}
6869

6970
public class TestDataAccess : DataAccessGeneric<T_Test>
7071
{
71-
public TestDataAccess()
72+
public TestDataAccess(ISmartSqlMapper smartSqlMapper) : base(smartSqlMapper)
7273
{
7374

7475
}

SmartSql.Tests/TestBase.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66

77
namespace SmartSql.Tests
88
{
9-
public abstract class TestBase: IDisposable
9+
public abstract class TestBase : IDisposable
1010
{
11-
protected ISmartSqlMapper SqlMapper = MapperContainer.Instance.GetSqlMapper();
11+
protected ISmartSqlMapper SqlMapper;
12+
public TestBase()
13+
{
14+
SqlMapper = new SmartSqlMapper();
15+
}
16+
1217

1318
public void Dispose()
1419
{
15-
MapperContainer.Instance.Dispose();
20+
SqlMapper.Dispose();
1621
}
1722
}
1823

SmartSql/Common/FileWatcherLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void Clear()
4646
{
4747
for (int i = 0; i < _fileWatchers.Count; i++)
4848
{
49-
FileSystemWatcher fileWatcher = (FileSystemWatcher)_fileWatchers[i];
49+
FileSystemWatcher fileWatcher = _fileWatchers[i];
5050
fileWatcher.EnableRaisingEvents = false;
5151
fileWatcher.Dispose();
5252
}

SmartSql/DbSession/DbConnectionSessionStore.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
namespace SmartSql.DbSession
1010
{
1111
/// <summary>
12-
/// For
12+
/// DbConnection Session Store
1313
/// </summary>
1414
public class DbConnectionSessionStore : IDbConnectionSessionStore
1515
{
1616
private readonly ILogger _logger;
1717
const string KEY = "SmartSql-Local-DbSesstion-";
1818
protected string sessionName = string.Empty;
19-
private AsyncLocal<IDictionary<string, IDbConnectionSession>> staticSessions
19+
private static AsyncLocal<IDictionary<string, IDbConnectionSession>> staticSessions
2020
= new AsyncLocal<IDictionary<string, IDbConnectionSession>>();
2121
public IDbConnectionSession LocalSession
2222
{
@@ -47,13 +47,7 @@ public void Store(IDbConnectionSession session)
4747
{
4848
if (staticSessions.Value == null)
4949
{
50-
lock (this)
51-
{
52-
if (staticSessions.Value == null)
53-
{
54-
staticSessions.Value = new Dictionary<String, IDbConnectionSession>();
55-
}
56-
}
50+
staticSessions.Value = new Dictionary<String, IDbConnectionSession>();
5751
}
5852
staticSessions.Value[sessionName] = session;
5953
}

SmartSql/LocalFileConfigLoader.cs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,12 @@ private void WatchConfig(ISmartSqlMapper smartSqlMapper)
9999
var cofigFileInfo = FileLoader.GetInfo(config.Path);
100100
FileWatcherLoader.Instance.Watch(cofigFileInfo, () =>
101101
{
102-
_logger.LogDebug($"LocalFileConfigLoader Changed ReloadConfig: {config.Path} Starting");
103-
var newConfig = Load(config.Path, smartSqlMapper);
104-
_logger.LogDebug($"LocalFileConfigLoader Changed ReloadConfig: {config.Path} End");
102+
lock (this)
103+
{
104+
_logger.LogDebug($"LocalFileConfigLoader Changed ReloadConfig: {config.Path} Starting");
105+
var newConfig = Load(config.Path, smartSqlMapper);
106+
_logger.LogDebug($"LocalFileConfigLoader Changed ReloadConfig: {config.Path} End");
107+
}
105108
});
106109
#endregion
107110
#region SmartSqlMaps File Watch
@@ -112,15 +115,18 @@ private void WatchConfig(ISmartSqlMapper smartSqlMapper)
112115
var sqlMapFileInfo = FileLoader.GetInfo(sqlmap.Path);
113116
FileWatcherLoader.Instance.Watch(sqlMapFileInfo, () =>
114117
{
115-
_logger.LogDebug($"LocalFileConfigLoader Changed Reload SmartSqlMap: {sqlmap.Path} Starting");
116-
var sqlmapStream = LoadConfigStream(sqlmap.Path);
117-
var newSqlmap = LoadSmartSqlMap(sqlmapStream, config);
118-
sqlmap.Scope = newSqlmap.Scope;
119-
sqlmap.Statements = newSqlmap.Statements;
120-
sqlmap.Caches = newSqlmap.Caches;
121-
config.ResetMappedStatements();
122-
smartSqlMapper.CacheManager.ResetMappedCaches();
123-
_logger.LogDebug($"LocalFileConfigLoader Changed Reload SmartSqlMap: {sqlmap.Path} End");
118+
lock (this)
119+
{
120+
_logger.LogDebug($"LocalFileConfigLoader Changed Reload SmartSqlMap: {sqlmap.Path} Starting");
121+
var sqlmapStream = LoadConfigStream(sqlmap.Path);
122+
var newSqlmap = LoadSmartSqlMap(sqlmapStream, config);
123+
sqlmap.Scope = newSqlmap.Scope;
124+
sqlmap.Statements = newSqlmap.Statements;
125+
sqlmap.Caches = newSqlmap.Caches;
126+
config.ResetMappedStatements();
127+
smartSqlMapper.CacheManager.ResetMappedCaches();
128+
_logger.LogDebug($"LocalFileConfigLoader Changed Reload SmartSqlMap: {sqlmap.Path} End");
129+
}
124130
});
125131
#endregion
126132
}

SmartSql/SmartSql.csproj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
<PackageProjectUrl>https://github.com/Ahoo-Wang/SmartSql</PackageProjectUrl>
1111
<RepositoryUrl>https://github.com/Ahoo-Wang/SmartSql</RepositoryUrl>
1212
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
13-
<Version>2.0.7</Version>
14-
<AssemblyVersion>2.0.7.0</AssemblyVersion>
15-
<FileVersion>2.0.7.0</FileVersion>
13+
<Version>2.0.8</Version>
14+
<AssemblyVersion>2.0.8.0</AssemblyVersion>
15+
<FileVersion>2.0.8.0</FileVersion>
1616
<PackageTags>SmartSql Dapper MyBatis Cache(Memory | Redis) ZooKeeper R/W Splitting</PackageTags>
17-
<PackageReleaseNotes>Fixed cache (FIFO &amp; LRU) not removed after maximum size.</PackageReleaseNotes>
17+
<PackageReleaseNotes>1. Fixed cache (FIFO &amp; LRU) not removed after maximum size.
18+
2. Optimizing lock related functions.</PackageReleaseNotes>
1819
</PropertyGroup>
1920

2021
<ItemGroup>

0 commit comments

Comments
 (0)