diff --git a/Lager.Tests/DummySettingsStorage.cs b/Lager.Tests/DummySettingsStorage.cs index 54bee81..0c5011c 100644 --- a/Lager.Tests/DummySettingsStorage.cs +++ b/Lager.Tests/DummySettingsStorage.cs @@ -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 diff --git a/Lager.Tests/SettingsStorageTest.cs b/Lager.Tests/SettingsStorageTest.cs index 71d7a3a..9088dfe 100644 --- a/Lager.Tests/SettingsStorageTest.cs +++ b/Lager.Tests/SettingsStorageTest.cs @@ -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(); cache.Setup(x => x.GetAsync(It.IsAny())).Returns(testCache.GetAsync); - var settings = new DummySettingsStorage(cache.Object); + var settings = new DummySettingsStorage("Storage", cache.Object); await settings.InitializeAsync(); diff --git a/Lager/SettingsStorage.cs b/Lager/SettingsStorage.cs index 4e3ecc9..a921343 100644 --- a/Lager/SettingsStorage.cs +++ b/Lager/SettingsStorage.cs @@ -89,7 +89,10 @@ protected T GetOrCreate(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; } /// @@ -104,16 +107,21 @@ protected void SetOrCreate(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)