Skip to content

Commit 3d7e1b9

Browse files
authored
Merge pull request #11 from kevbite/cachebug
Fixing caching bug on multiple sources
2 parents c43bfd2 + cc8b099 commit 3d7e1b9

13 files changed

+100
-23
lines changed

appveyor.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
only:
44
- master
55
skip_tags: true
6-
version: 4.0.0
6+
version: 4.0.1
77

88
assembly_info:
99
assembly_version: '{version}'
@@ -59,7 +59,7 @@
5959

6060
-
6161
skip_tags: true
62-
version: 4.0.0-{branch}{build}
62+
version: 4.0.1-{branch}{build}
6363

6464
assembly_info:
6565
assembly_version: '{version}'

src/HumbleConfig.Caching.Tests/CachingConfigurationSourceDecoratorTests/CachingConfigurationSourceDecoratorForNoneExistingAppSettingKey.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ protected override IConfigurationSource CreateConfigurationSource()
1717
innerSource.Setup(x => x.GetAppSettingAsync<TValue>(It.IsAny<string>(), It.IsAny<CancellationToken>()))
1818
.ReturnsAsync(ConfigurationSourceResult<TValue>.FailedResult());
1919

20-
var source = new CachingConfigurationSourceDecorator(innerSource.Object, MemoryCache.Default, () => new CacheItemPolicy());
20+
var source = new CachingConfigurationSourceDecorator(innerSource.Object, new CacheKeyCreator(Guid.NewGuid().ToString()), MemoryCache.Default, () => new CacheItemPolicy());
2121

2222
return source;
2323
}

src/HumbleConfig.Caching.Tests/CachingConfigurationSourceDecoratorTests/CachingConfigurationSourceDecoratorForNoneExistingAppSettingKeyWithValueCached.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected override IConfigurationSource CreateConfigurationSource()
2121
innerSource.Setup(x => x.GetAppSettingAsync<TValue>(It.IsAny<string>(), It.IsAny<CancellationToken>()))
2222
.Throws(new Exception("Should never get here"));
2323

24-
var source = new CachingConfigurationSourceDecorator(innerSource.Object, objectCache.Object, () => new CacheItemPolicy());
24+
var source = new CachingConfigurationSourceDecorator(innerSource.Object, new CacheKeyCreator(Guid.NewGuid().ToString()), objectCache.Object, () => new CacheItemPolicy());
2525

2626
return source;
2727
}

src/HumbleConfig.Caching.Tests/CachingConfigurationSourceDecoratorTests/CachingConfigurationSourceDecoratorTestsForExistingKey.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected override IConfigurationSource GivenConfigurationSourceWithExistingRKey
1818
innerSource.Setup(x => x.GetAppSettingAsync<TValue>(key, cancellationToken))
1919
.ReturnsAsync(ConfigurationSourceResult<TValue>.SuccessResult(expectedValue));
2020

21-
var source = new CachingConfigurationSourceDecorator(innerSource.Object, MemoryCache.Default, () => new CacheItemPolicy());
21+
var source = new CachingConfigurationSourceDecorator(innerSource.Object, new CacheKeyCreator(Guid.NewGuid().ToString()), MemoryCache.Default, () => new CacheItemPolicy());
2222

2323
return source;
2424
}

src/HumbleConfig.Caching.Tests/CachingConfigurationSourceDecoratorTests/CachingConfigurationSourceDecoratorTestsForExistingKeyWithValueCached.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected override IConfigurationSource GivenConfigurationSourceWithExistingRKey
1919
.ReturnsAsync(ConfigurationSourceResult<TValue>.SuccessResult(expectedValue))
2020
.Throws(new Exception("Should never get here"));
2121

22-
var source = new CachingConfigurationSourceDecorator(innerSource.Object, MemoryCache.Default, () => new CacheItemPolicy());
22+
var source = new CachingConfigurationSourceDecorator(innerSource.Object, new CacheKeyCreator(Guid.NewGuid().ToString()), MemoryCache.Default, () => new CacheItemPolicy());
2323
source.GetAppSettingAsync<TValue>(key, cancellationToken).Wait();
2424

2525
return source;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace HumbleConfig.Caching
2+
{
3+
public class CacheKeyCreator : ICacheKeyCreator
4+
{
5+
private readonly string _keyPrefix;
6+
7+
public CacheKeyCreator(string keyPrefix)
8+
{
9+
_keyPrefix = keyPrefix;
10+
}
11+
12+
public string CreateCacheKey(string key)
13+
{
14+
return $"{_keyPrefix}-{key}";
15+
}
16+
}
17+
}

src/HumbleConfig.Caching/CachingConfigurationSourceDecorator.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@ namespace HumbleConfig.Caching
88
public class CachingConfigurationSourceDecorator : IConfigurationSource
99
{
1010
private readonly IConfigurationSource _innerSource;
11+
private readonly ICacheKeyCreator _cacheKeyCreator;
1112
private readonly ObjectCache _objectCache;
1213
private readonly Func<CacheItemPolicy> _cacheItemPolicyFactory;
1314

14-
public CachingConfigurationSourceDecorator(IConfigurationSource innerSource, ObjectCache objectCache, Func<CacheItemPolicy> cacheItemPolicyFactoryFactory)
15+
public CachingConfigurationSourceDecorator(IConfigurationSource innerSource, ICacheKeyCreator cacheKeyCreator, ObjectCache objectCache, Func<CacheItemPolicy> cacheItemPolicyFactoryFactory)
1516
{
1617
_innerSource = innerSource;
18+
_cacheKeyCreator = cacheKeyCreator;
1719
_objectCache = objectCache;
1820
_cacheItemPolicyFactory = cacheItemPolicyFactoryFactory;
1921
}
2022

2123
public async Task<ConfigurationSourceResult<TValue>> GetAppSettingAsync<TValue>(string key, CancellationToken cancellationToken = default(CancellationToken))
2224
{
23-
var appsetting = (ConfigurationSourceResult<TValue>)_objectCache.Get(CreateCacheKey(key));
25+
var appsetting = (ConfigurationSourceResult<TValue>)_objectCache.Get(_cacheKeyCreator.CreateCacheKey(key));
2426

2527
if (appsetting == null)
2628
{
@@ -32,15 +34,11 @@ public CachingConfigurationSourceDecorator(IConfigurationSource innerSource, Obj
3234

3335
return appsetting;
3436
}
35-
36-
private static string CreateCacheKey(string key)
37-
{
38-
return $"HumbleConfig-{key}";
39-
}
37+
4038

4139
private CacheItem CreateCacheItem<TValue>(string key, ConfigurationSourceResult<TValue> value)
4240
{
43-
return new CacheItem(CreateCacheKey(key), value);
41+
return new CacheItem(_cacheKeyCreator.CreateCacheKey(key), value);
4442
}
4543
}
4644
}

src/HumbleConfig.Caching/CachingExtentions.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,29 @@ namespace HumbleConfig.Caching
66
{
77
public static class CachingExtentions
88
{
9-
public static IConfigurationSourceConfigurator WithCache(this IConfigurationSourceConfigurator a, ObjectCache objectCache, Func<CacheItemPolicy> cacheItemPolicyFactory)
9+
public static IConfigurationSourceConfigurator WithCache(this IConfigurationSourceConfigurator configurationSourceConfigurator, ICacheKeyCreator cacheKeyCreator, ObjectCache objectCache, Func<CacheItemPolicy> cacheItemPolicyFactory)
1010
{
11-
return a.WrapSource(x => new CachingConfigurationSourceDecorator(x, objectCache, cacheItemPolicyFactory));
11+
return configurationSourceConfigurator.WrapSource(x => new CachingConfigurationSourceDecorator(x, cacheKeyCreator, objectCache, cacheItemPolicyFactory));
1212
}
1313

14-
public static IConfigurationSourceConfigurator WithDefaultMemoryCache(this IConfigurationSourceConfigurator a, Func<CacheItemPolicy> cacheItemPolicyFactory)
14+
public static IConfigurationSourceConfigurator WithCache(this IConfigurationSourceConfigurator configurationSourceConfigurator, ObjectCache objectCache, Func<CacheItemPolicy> cacheItemPolicyFactory)
1515
{
16-
return a.WrapSource(x => new CachingConfigurationSourceDecorator(x, MemoryCache.Default, cacheItemPolicyFactory));
16+
var cacheKeyCreator = new CacheKeyCreator(Guid.NewGuid().ToString());
17+
18+
return WithCache(configurationSourceConfigurator, cacheKeyCreator, objectCache, cacheItemPolicyFactory);
19+
}
20+
21+
public static IConfigurationSourceConfigurator WithDefaultMemoryCache(this IConfigurationSourceConfigurator configurationSourceConfigurator, Func<CacheItemPolicy> cacheItemPolicyFactory)
22+
{
23+
return WithCache(configurationSourceConfigurator, MemoryCache.Default, cacheItemPolicyFactory);
1724
}
1825

19-
public static IConfigurationSourceConfigurator WithDefaultMemoryCache(this IConfigurationSourceConfigurator a, TimeSpan expiresAfter)
26+
public static IConfigurationSourceConfigurator WithDefaultMemoryCache(this IConfigurationSourceConfigurator configurationSourceConfigurator, TimeSpan expiresAfter)
2027
{
21-
return a.WrapSource(x => new CachingConfigurationSourceDecorator(x, MemoryCache.Default, () => new CacheItemPolicy()
28+
return WithDefaultMemoryCache(configurationSourceConfigurator, () => new CacheItemPolicy()
2229
{
2330
AbsoluteExpiration = DateTimeOffset.UtcNow.Add(expiresAfter)
24-
}));
31+
});
2532
}
2633
}
2734
}

src/HumbleConfig.Caching/HumbleConfig.Caching.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@
4141
<Reference Include="System.Xml" />
4242
</ItemGroup>
4343
<ItemGroup>
44+
<Compile Include="CacheKeyCreator.cs" />
4445
<Compile Include="CachingExtentions.cs" />
4546
<Compile Include="CachingConfigurationSourceDecorator.cs" />
47+
<Compile Include="ICacheKeyCreator.cs" />
4648
<Compile Include="Properties\AssemblyInfo.cs" />
4749
</ItemGroup>
4850
<ItemGroup>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace HumbleConfig.Caching
2+
{
3+
public interface ICacheKeyCreator
4+
{
5+
string CreateCacheKey(string key);
6+
}
7+
}

0 commit comments

Comments
 (0)