Skip to content

Commit

Permalink
Fix all the things
Browse files Browse the repository at this point in the history
GetOrCreate now automatically adds the default value or the retrieved
value into the internal cache.
Also I made a dumb mistake in the InitializeAsync unit test
  • Loading branch information
flagbug committed Dec 14, 2013
1 parent 3a41a3a commit a98a4a3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Lager.Tests/DummySettingsStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace Lager.Tests
{
public class DummySettingsStorage : SettingsStorage
{
public DummySettingsStorage(IBlobCache cache)
: base("__DUMMYSTORAGE__", cache)
public DummySettingsStorage(string keyPrefix, IBlobCache cache)
: base(keyPrefix, cache)
{ }

public int DummyNumber
Expand Down
6 changes: 3 additions & 3 deletions Lager.Tests/SettingsStorageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ public void GetOrCreateWithNullKeyThrowsArgumentNullException()
public async Task InitializeAsyncLoadsValuesIntoCache()
{
var testCache = new TestBlobCache();
testCache.InsertObject("DummyNumber", 16);
testCache.InsertObject("DummyText", "Random");
testCache.InsertObject("Storage:DummyNumber", 16);
testCache.InsertObject("Storage:DummyText", "Random");

var cache = new Mock<IBlobCache>();
cache.Setup(x => x.GetAsync(It.IsAny<string>())).Returns<string>(testCache.GetAsync);
var settings = new DummySettingsStorage(cache.Object);
var settings = new DummySettingsStorage("Storage", cache.Object);

await settings.InitializeAsync();

Expand Down
18 changes: 13 additions & 5 deletions Lager/SettingsStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ protected T GetOrCreate<T>(T defaultValue, [CallerMemberName] string key = null)
this.cacheLock.ExitReadLock();
}

return this.blobCache.GetOrCreateObject(string.Format("{0}:{1}", this.keyPrefix, key), () => defaultValue).Wait();
T returnValue = this.blobCache.GetOrCreateObject(string.Format("{0}:{1}", this.keyPrefix, key), () => defaultValue)
.Do(x => this.AddToInternalCache(key, x)).Wait();

return returnValue;
}

/// <summary>
Expand All @@ -104,16 +107,21 @@ protected void SetOrCreate<T>(T value, [CallerMemberName] string key = null)
if (key == null)
throw new ArgumentNullException("key");

this.AddToInternalCache(key, value);

this.blobCache.InsertObject(string.Format("{0}:{1}", this.keyPrefix, key), value);

this.OnPropertyChanged(key);
}

private void AddToInternalCache(string key, object value)
{
this.cacheLock.EnterWriteLock();

this.cache.Remove(key);
this.cache.Add(key, value);

this.cacheLock.ExitWriteLock();

this.blobCache.InsertObject(string.Format("{0}:{1}", this.keyPrefix, key), value);

this.OnPropertyChanged(key);
}

private void OnPropertyChanged(string propertyName = null)
Expand Down

0 comments on commit a98a4a3

Please sign in to comment.