Skip to content

Commit

Permalink
1. Fixed cache (FIFO & LRU) not removed after maximum size.
Browse files Browse the repository at this point in the history
2. Optimizing lock related functions.
  • Loading branch information
Ahoo-Wang committed Sep 13, 2017
1 parent 4fdb831 commit ae21953
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 37 deletions.
17 changes: 9 additions & 8 deletions SmartSql.Tests/DataAccess/DataAccess_Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
using System.Text;
using SmartSql.DataAccess;
using Xunit;
using SmartSql.Abstractions;

namespace SmartSql.Tests.DataAccess
{

public class DataAccess_Test : IDisposable
public class DataAccess_Test : TestBase
{
private static TestDataAccess dao = new TestDataAccess();
private TestDataAccess dao;
public DataAccess_Test()
{
dao = new TestDataAccess(SqlMapper);
}

[Fact]
public void Insert()
{
Expand Down Expand Up @@ -59,16 +65,11 @@ public void GetEntity()
var entity = dao.GetEntity<long>(240162);
// Assert.NotNull(entity);
}

public void Dispose()
{
MapperContainer.Instance.Dispose();
}
}

public class TestDataAccess : DataAccessGeneric<T_Test>
{
public TestDataAccess()
public TestDataAccess(ISmartSqlMapper smartSqlMapper) : base(smartSqlMapper)
{

}
Expand Down
11 changes: 8 additions & 3 deletions SmartSql.Tests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@

namespace SmartSql.Tests
{
public abstract class TestBase: IDisposable
public abstract class TestBase : IDisposable
{
protected ISmartSqlMapper SqlMapper = MapperContainer.Instance.GetSqlMapper();
protected ISmartSqlMapper SqlMapper;
public TestBase()
{
SqlMapper = new SmartSqlMapper();
}


public void Dispose()
{
MapperContainer.Instance.Dispose();
SqlMapper.Dispose();
}
}

Expand Down
2 changes: 1 addition & 1 deletion SmartSql/Common/FileWatcherLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void Clear()
{
for (int i = 0; i < _fileWatchers.Count; i++)
{
FileSystemWatcher fileWatcher = (FileSystemWatcher)_fileWatchers[i];
FileSystemWatcher fileWatcher = _fileWatchers[i];
fileWatcher.EnableRaisingEvents = false;
fileWatcher.Dispose();
}
Expand Down
12 changes: 3 additions & 9 deletions SmartSql/DbSession/DbConnectionSessionStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
namespace SmartSql.DbSession
{
/// <summary>
/// For
/// DbConnection Session Store
/// </summary>
public class DbConnectionSessionStore : IDbConnectionSessionStore
{
private readonly ILogger _logger;
const string KEY = "SmartSql-Local-DbSesstion-";
protected string sessionName = string.Empty;
private AsyncLocal<IDictionary<string, IDbConnectionSession>> staticSessions
private static AsyncLocal<IDictionary<string, IDbConnectionSession>> staticSessions
= new AsyncLocal<IDictionary<string, IDbConnectionSession>>();
public IDbConnectionSession LocalSession
{
Expand Down Expand Up @@ -47,13 +47,7 @@ public void Store(IDbConnectionSession session)
{
if (staticSessions.Value == null)
{
lock (this)
{
if (staticSessions.Value == null)
{
staticSessions.Value = new Dictionary<String, IDbConnectionSession>();
}
}
staticSessions.Value = new Dictionary<String, IDbConnectionSession>();
}
staticSessions.Value[sessionName] = session;
}
Expand Down
30 changes: 18 additions & 12 deletions SmartSql/LocalFileConfigLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,12 @@ private void WatchConfig(ISmartSqlMapper smartSqlMapper)
var cofigFileInfo = FileLoader.GetInfo(config.Path);
FileWatcherLoader.Instance.Watch(cofigFileInfo, () =>
{
_logger.LogDebug($"LocalFileConfigLoader Changed ReloadConfig: {config.Path} Starting");
var newConfig = Load(config.Path, smartSqlMapper);
_logger.LogDebug($"LocalFileConfigLoader Changed ReloadConfig: {config.Path} End");
lock (this)
{
_logger.LogDebug($"LocalFileConfigLoader Changed ReloadConfig: {config.Path} Starting");
var newConfig = Load(config.Path, smartSqlMapper);
_logger.LogDebug($"LocalFileConfigLoader Changed ReloadConfig: {config.Path} End");
}
});
#endregion
#region SmartSqlMaps File Watch
Expand All @@ -112,15 +115,18 @@ private void WatchConfig(ISmartSqlMapper smartSqlMapper)
var sqlMapFileInfo = FileLoader.GetInfo(sqlmap.Path);
FileWatcherLoader.Instance.Watch(sqlMapFileInfo, () =>
{
_logger.LogDebug($"LocalFileConfigLoader Changed Reload SmartSqlMap: {sqlmap.Path} Starting");
var sqlmapStream = LoadConfigStream(sqlmap.Path);
var newSqlmap = LoadSmartSqlMap(sqlmapStream, config);
sqlmap.Scope = newSqlmap.Scope;
sqlmap.Statements = newSqlmap.Statements;
sqlmap.Caches = newSqlmap.Caches;
config.ResetMappedStatements();
smartSqlMapper.CacheManager.ResetMappedCaches();
_logger.LogDebug($"LocalFileConfigLoader Changed Reload SmartSqlMap: {sqlmap.Path} End");
lock (this)
{
_logger.LogDebug($"LocalFileConfigLoader Changed Reload SmartSqlMap: {sqlmap.Path} Starting");
var sqlmapStream = LoadConfigStream(sqlmap.Path);
var newSqlmap = LoadSmartSqlMap(sqlmapStream, config);
sqlmap.Scope = newSqlmap.Scope;
sqlmap.Statements = newSqlmap.Statements;
sqlmap.Caches = newSqlmap.Caches;
config.ResetMappedStatements();
smartSqlMapper.CacheManager.ResetMappedCaches();
_logger.LogDebug($"LocalFileConfigLoader Changed Reload SmartSqlMap: {sqlmap.Path} End");
}
});
#endregion
}
Expand Down
9 changes: 5 additions & 4 deletions SmartSql/SmartSql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
<PackageProjectUrl>https://github.com/Ahoo-Wang/SmartSql</PackageProjectUrl>
<RepositoryUrl>https://github.com/Ahoo-Wang/SmartSql</RepositoryUrl>
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
<Version>2.0.7</Version>
<AssemblyVersion>2.0.7.0</AssemblyVersion>
<FileVersion>2.0.7.0</FileVersion>
<Version>2.0.8</Version>
<AssemblyVersion>2.0.8.0</AssemblyVersion>
<FileVersion>2.0.8.0</FileVersion>
<PackageTags>SmartSql Dapper MyBatis Cache(Memory | Redis) ZooKeeper R/W Splitting</PackageTags>
<PackageReleaseNotes>Fixed cache (FIFO &amp; LRU) not removed after maximum size.</PackageReleaseNotes>
<PackageReleaseNotes>1. Fixed cache (FIFO &amp; LRU) not removed after maximum size.
2. Optimizing lock related functions.</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit ae21953

Please sign in to comment.