Skip to content

Commit 91c6d20

Browse files
Merge pull request #34 from johelvisguzman/hotfix/issue#33
Closes #33 - The in-memory file based system needs to reload only when the file changes
2 parents e8b9d25 + 15c3e1d commit 91c6d20

File tree

3 files changed

+70
-11
lines changed

3 files changed

+70
-11
lines changed

src/DotNetToolkit.Repository.InMemory/InMemoryFileBasedRepositoryBase.cs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,34 @@ private void OnInitialize(string filePath)
9292
// Otherwise, try to get the data from the file
9393
else
9494
{
95-
// Adds the data from the file into memory
96-
using (var stream = new FileStream(DatabaseName, FileMode.Open, FileAccess.Read))
97-
using (var reader = new StreamReader(stream))
95+
// Checks to see when was the last time that the file was updated. If the TimeStamp and the lastWriteTime
96+
// are the same, then it means that the file has only been updated by this repository; otherwise,
97+
// something else updated it (maybe a manual edit). In which case, we need to re-upload the entities into memory
98+
var lastWriteTime = File.GetLastWriteTime(DatabaseName);
99+
var timetamp = GetTimeStamp();
100+
if (!lastWriteTime.Equals(timetamp))
98101
{
99-
var entities = OnLoaded(reader);
102+
LoadChanges();
103+
}
104+
}
105+
}
100106

101-
EnsureDeleted();
107+
private void LoadChanges()
108+
{
109+
// Adds the data from the file into memory
110+
using (var stream = new FileStream(DatabaseName, FileMode.Open, FileAccess.Read))
111+
using (var reader = new StreamReader(stream))
112+
{
113+
var entities = OnLoaded(reader);
102114

103-
foreach (var entity in entities)
104-
{
105-
AddItem(entity);
106-
}
115+
EnsureDeleted();
107116

108-
base.SaveChanges();
117+
foreach (var entity in entities)
118+
{
119+
AddItem(entity);
109120
}
121+
122+
base.SaveChanges();
110123
}
111124
}
112125

@@ -131,6 +144,8 @@ protected override void SaveChanges()
131144

132145
OnSaved(writer, entities);
133146
}
147+
148+
SetTimeStamp(File.GetLastWriteTime(DatabaseName));
134149
}
135150
}
136151

src/DotNetToolkit.Repository.InMemory/InMemoryRepositoryBase.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,24 @@ internal void EnsureDeleted()
7979
InMemoryCache<TEntity, TKey>.Instance.GetContext(DatabaseName).Clear();
8080
}
8181

82+
/// <summary>
83+
/// Sets the time stamp.
84+
/// </summary>
85+
/// <param name="time">The time.</param>
86+
internal void SetTimeStamp(DateTime time)
87+
{
88+
InMemoryCache<TEntity, TKey>.Instance.SetTimeStamp(DatabaseName, time);
89+
}
90+
91+
/// <summary>
92+
/// Gets the time stamp.
93+
/// </summary>
94+
/// <returns>The time stamp.</returns>
95+
internal DateTime GetTimeStamp()
96+
{
97+
return InMemoryCache<TEntity, TKey>.Instance.GetTimeStamp(DatabaseName);
98+
}
99+
82100
#endregion
83101

84102
#region Private Methods
@@ -230,6 +248,8 @@ protected override void SaveChanges()
230248
}
231249
}
232250

251+
SetTimeStamp(DateTime.Now);
252+
233253
_context.Clear();
234254
}
235255
}
@@ -333,6 +353,7 @@ private class InMemoryCache<TEntity, TKey> where TEntity : class
333353
private static volatile InMemoryCache<TEntity, TKey> _instance;
334354
private static readonly object _syncRoot = new object();
335355
private readonly ConcurrentDictionary<string, SortedDictionary<TKey, TEntity>> _storage;
356+
private readonly ConcurrentDictionary<string, DateTime> _timestamp;
336357

337358
#endregion
338359

@@ -344,6 +365,7 @@ private class InMemoryCache<TEntity, TKey> where TEntity : class
344365
private InMemoryCache()
345366
{
346367
_storage = new ConcurrentDictionary<string, SortedDictionary<TKey, TEntity>>();
368+
_timestamp = new ConcurrentDictionary<string, DateTime>();
347369
}
348370

349371
#endregion
@@ -389,6 +411,28 @@ public SortedDictionary<TKey, TEntity> GetContext(string name)
389411
return _storage[name];
390412
}
391413

414+
/// <summary>
415+
/// Sets the time stamp.
416+
/// </summary>
417+
/// <param name="name">The database name.</param>
418+
/// <param name="time">The time.</param>
419+
public void SetTimeStamp(string name, DateTime time)
420+
{
421+
_timestamp[name] = time;
422+
}
423+
424+
/// <summary>
425+
/// Gets the time stamp.
426+
/// </summary>
427+
/// <param name="name">The database name.</param>
428+
/// <returns>The time stamp.</returns>
429+
public DateTime GetTimeStamp(string name)
430+
{
431+
_timestamp.TryGetValue(name, out DateTime time);
432+
433+
return time;
434+
}
435+
392436
#endregion
393437
}
394438

version.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<VersionPrefix>2.1.0</VersionPrefix>
3+
<VersionPrefix>2.1.1</VersionPrefix>
44
<VersionSuffix></VersionSuffix>
55
</PropertyGroup>
66
</Project>

0 commit comments

Comments
 (0)