Skip to content

Commit b41d66d

Browse files
committed
Fixing ms memory cache ctor to work with ms configuration things in CacheManager. Added more tests for MS Configuraiton
1 parent 9d2c065 commit b41d66d

5 files changed

Lines changed: 91 additions & 31 deletions

File tree

src/CacheManager.Microsoft.Extensions.Caching.Memory/MemoryCacheHandle`1.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ public class MemoryCacheHandle<TCacheValue> : BaseCacheHandle<TCacheValue>
2020

2121
private volatile MemoryCache _cache = null;
2222

23+
/// <summary>
24+
/// Initializes a new instance of the <see cref="MemoryCacheHandle{TCacheValue}"/> class.
25+
/// </summary>
26+
/// <param name="managerConfiguration">The manager configuration.</param>
27+
/// <param name="configuration">The cache handle configuration.</param>
28+
/// <param name="loggerFactory">The logger factory.</param>
29+
[CLSCompliant(false)]
30+
public MemoryCacheHandle(ICacheManagerConfiguration managerConfiguration, CacheHandleConfiguration configuration, ILoggerFactory loggerFactory)
31+
: this(managerConfiguration, configuration, loggerFactory, null)
32+
{
33+
}
34+
2335
/// <summary>
2436
/// Initializes a new instance of the <see cref="MemoryCacheHandle{TCacheValue}"/> class.
2537
/// </summary>
@@ -28,7 +40,7 @@ public class MemoryCacheHandle<TCacheValue> : BaseCacheHandle<TCacheValue>
2840
/// <param name="loggerFactory">The logger factory.</param>
2941
/// <param name="memoryCacheOptions">The vendor specific options.</param>
3042
[CLSCompliant(false)]
31-
public MemoryCacheHandle(ICacheManagerConfiguration managerConfiguration, CacheHandleConfiguration configuration, ILoggerFactory loggerFactory, MemoryCacheOptions memoryCacheOptions = null)
43+
public MemoryCacheHandle(ICacheManagerConfiguration managerConfiguration, CacheHandleConfiguration configuration, ILoggerFactory loggerFactory, MemoryCacheOptions memoryCacheOptions)
3244
: base(managerConfiguration, configuration)
3345
{
3446
NotNull(configuration, nameof(configuration));

src/CacheManager.StackExchange.Redis/RedisCacheHandle.cs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@
1010

1111
namespace CacheManager.Redis
1212
{
13-
internal enum ScriptType
14-
{
15-
Put,
16-
Add,
17-
Update,
18-
Get
19-
}
20-
2113
/// <summary>
2214
/// Cache handle implementation for Redis.
2315
/// </summary>
@@ -660,17 +652,17 @@ protected override bool RemoveInternal(string key, string region)
660652
private void SubscribeKeyspaceNotifications()
661653
{
662654
_connection.Subscriber.Subscribe(
663-
$"__keyevent@{_redisConfiguration.Database}__:expired",
664-
(channel, key) =>
665-
{
666-
var tupple = ParseKey(key);
667-
if (Logger.IsEnabled(LogLevel.Debug))
668-
{
669-
Logger.LogDebug("Got expired event for key '{0}:{1}'", tupple.Item2, tupple.Item1);
670-
}
671-
672-
TriggerCacheSpecificRemove(tupple.Item1, tupple.Item2, CacheItemRemovedReason.Expired);
673-
});
655+
$"__keyevent@{_redisConfiguration.Database}__:expired",
656+
(channel, key) =>
657+
{
658+
var tupple = ParseKey(key);
659+
if (Logger.IsEnabled(LogLevel.Debug))
660+
{
661+
Logger.LogDebug("Got expired event for key '{0}:{1}'", tupple.Item2, tupple.Item1);
662+
}
663+
664+
TriggerCacheSpecificRemove(tupple.Item1, tupple.Item2, CacheItemRemovedReason.Expired);
665+
});
674666

675667
_connection.Subscriber.Subscribe(
676668
$"__keyevent@{_redisConfiguration.Database}__:evicted",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace CacheManager.Redis
2+
{
3+
internal enum ScriptType
4+
{
5+
Put,
6+
Add,
7+
Update,
8+
Get
9+
}
10+
}

test/CacheManager.Tests/MicrosoftConfigurationTests.cs

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public void Configuration_CacheManager_ComplexSingleManager()
5151
config.CacheHandleConfigurations[1].Key.Should().Be(key + "2");
5252
config.CacheHandleConfigurations[0].Name.Should().Be("handleName");
5353
config.CacheHandleConfigurations[1].Name.Should().Be("handleName2");
54+
55+
var cache = new BaseCacheManager<string>(config);
56+
cache.Add("key", "value").Should().BeTrue();
5457
}
5558

5659
[Fact]
@@ -80,13 +83,16 @@ public void Configuration_CacheManager_ComplexManyManager()
8083
};
8184

8285
var configs = GetConfiguration(data).GetCacheConfigurations().ToArray();
83-
for(var i = 1; i <= configs.Count(); i++)
86+
for (var i = 1; i <= configs.Count(); i++)
8487
{
8588
var config = configs[i - 1];
86-
config.Name.Should().Be("myCacheName" +i);
89+
config.Name.Should().Be("myCacheName" + i);
8790
config.MaxRetries.Should().Be(i * 100);
8891
config.RetryTimeout.Should().Be(i * 100);
8992
config.CacheHandleConfigurations.Count.Should().Be(i);
93+
94+
var cache = new BaseCacheManager<string>(config);
95+
cache.Add("key", "value").Should().BeTrue();
9096
}
9197
}
9298

@@ -121,6 +127,9 @@ public void Configuration_CacheManager_ByNameFromMany()
121127
config.MaxRetries.Should().Be(200);
122128
config.RetryTimeout.Should().Be(200);
123129
config.CacheHandleConfigurations.Count.Should().Be(2);
130+
131+
var cache = new BaseCacheManager<string>(config);
132+
cache.Add("key", "value").Should().BeTrue();
124133
}
125134

126135
[Fact]
@@ -191,6 +200,8 @@ public void Configuration_CacheManager_AllProperties()
191200
config.MaxRetries.Should().Be(42);
192201
config.RetryTimeout.Should().Be(21);
193202
config.UpdateMode.Should().Be(CacheUpdateMode.Up);
203+
var cache = new BaseCacheManager<string>(config);
204+
cache.Add("key", "value").Should().BeTrue();
194205
}
195206

196207
[Fact]
@@ -296,6 +307,7 @@ public void Configuration_CacheHandle_MinimalValid()
296307
}
297308

298309
#if !NETCOREAPP
310+
299311
[Fact]
300312
public void Configuration_CacheHandle_KnownType_SystemRuntime()
301313
{
@@ -309,7 +321,10 @@ public void Configuration_CacheHandle_KnownType_SystemRuntime()
309321
config.Name.Should().Be("name");
310322
config.CacheHandleConfigurations.Count.Should().Be(1);
311323
config.CacheHandleConfigurations[0].HandleType.Should().Be(typeof(SystemRuntimeCaching.MemoryCacheHandle<>));
324+
var cache = new BaseCacheManager<string>(config);
325+
cache.Add("key", "value").Should().BeTrue();
312326
}
327+
313328
#endif
314329

315330
[Fact]
@@ -364,6 +379,7 @@ public void Configuration_CacheHandle_KnownType_RedisB()
364379
}
365380

366381
#if !NETCOREAPP
382+
367383
[Fact]
368384
public void Configuration_CacheHandle_KnownType_CouchbaseNoKey()
369385
{
@@ -429,10 +445,13 @@ public void Configuration_CacheHandle_KnownType_MemcachedNoKey()
429445
action.ShouldThrow<InvalidOperationException>().WithMessage("*'key' or 'name'*");
430446
}
431447

448+
#if MEMCACHEDENABLED
449+
432450
[Fact]
451+
[Trait("category", "memcached")]
433452
public void Configuration_CacheHandle_KnownType_Memcached()
434453
{
435-
var key = Guid.NewGuid().ToString();
454+
var key = "default";
436455
var data = new Dictionary<string, string>
437456
{
438457
{"cacheManagers:0:name", "name"},
@@ -446,8 +465,13 @@ public void Configuration_CacheHandle_KnownType_Memcached()
446465
config.CacheHandleConfigurations[0].HandleType.Should().Be(typeof(Memcached.MemcachedCacheHandle<>));
447466
config.CacheHandleConfigurations[0].Key.Should().Be(key);
448467
config.CacheHandleConfigurations[0].Name.Should().NotBeNullOrWhiteSpace(); // name is random in this case
468+
469+
var cache = new BaseCacheManager<string>(config);
470+
cache.Add(Guid.NewGuid().ToString(), "value").Should().BeTrue();
449471
}
450472

473+
#endif
474+
451475
[Fact]
452476
public void Configuration_CacheHandle_KnownType_MemcachedB()
453477
{
@@ -465,7 +489,7 @@ public void Configuration_CacheHandle_KnownType_MemcachedB()
465489
config.CacheHandleConfigurations[0].Name.Should().Be("name");
466490
config.CacheHandleConfigurations[0].Key.Should().Be("name"); // now key gets set to name
467491
}
468-
492+
469493
[Fact]
470494
public void Configuration_CacheHandle_Type_MemcachedB()
471495
{
@@ -498,6 +522,7 @@ public void Configuration_CacheHandle_KnownType_Web()
498522
config.CacheHandleConfigurations.Count.Should().Be(1);
499523
config.CacheHandleConfigurations[0].HandleType.Should().Be(typeof(Web.SystemWebCacheHandle<>));
500524
}
525+
501526
#endif
502527

503528
[Fact]
@@ -513,8 +538,10 @@ public void Configuration_CacheHandle_KnownType_Dictionary()
513538
config.Name.Should().Be("name");
514539
config.CacheHandleConfigurations.Count.Should().Be(1);
515540
config.CacheHandleConfigurations[0].HandleType.Should().Be(typeof(Core.Internal.DictionaryCacheHandle<>));
516-
}
517541

542+
var cache = new BaseCacheManager<string>(config);
543+
cache.Add("key", "value").Should().BeTrue();
544+
}
518545

519546
[Fact]
520547
public void Configuration_CacheHandle_KnownType_MsMemory()
@@ -529,6 +556,9 @@ public void Configuration_CacheHandle_KnownType_MsMemory()
529556
config.Name.Should().Be("name");
530557
config.CacheHandleConfigurations.Count.Should().Be(1);
531558
config.CacheHandleConfigurations[0].HandleType.Should().Be(typeof(CacheManager.MicrosoftCachingMemory.MemoryCacheHandle<>));
559+
560+
var cache = new BaseCacheManager<string>(config);
561+
cache.Add("key", "value").Should().BeTrue();
532562
}
533563

534564
[Fact]
@@ -557,6 +587,9 @@ public void Configuration_CacheHandle_AllProperties()
557587
config.CacheHandleConfigurations[0].IsBackplaneSource.Should().BeTrue();
558588
config.CacheHandleConfigurations[0].Name.Should().Be("handleName");
559589
config.CacheHandleConfigurations[0].Key.Should().Be(key);
590+
591+
var cache = new BaseCacheManager<string>(config);
592+
cache.Add("key", "value").Should().BeTrue();
560593
}
561594

562595
[Fact]
@@ -704,7 +737,7 @@ public void Configuration_Backplane_InvalidKnownTypeB()
704737
Action act = () => GetConfiguration(data).GetCacheConfiguration("name");
705738
act.ShouldThrow<InvalidOperationException>().WithMessage("*Known backplane type 'Something' is invalid*");
706739
}
707-
740+
708741
[Fact]
709742
public void Configuration_Backplane_Redis_MissingKey()
710743
{
@@ -719,7 +752,11 @@ public void Configuration_Backplane_Redis_MissingKey()
719752
act.ShouldThrow<InvalidOperationException>().WithMessage("*The key property is required*");
720753
}
721754

755+
#if REDISENABLED
756+
722757
[Fact]
758+
[Trait("category", "Redis")]
759+
[Trait("category", "Unreliable")]
723760
public void Configuration_Backplane_Redis_Valid()
724761
{
725762
var key = Guid.NewGuid().ToString();
@@ -742,8 +779,13 @@ public void Configuration_Backplane_Redis_Valid()
742779
config.BackplaneConfigurationKey.Should().Be(key);
743780
config.BackplaneType.Should().Be(typeof(Redis.RedisCacheBackplane));
744781
config.HasBackplane.Should().BeTrue();
782+
783+
var cache = new BaseCacheManager<string>(config);
784+
cache.Add(Guid.NewGuid().ToString(), "value").Should().BeTrue();
745785
}
746786

787+
#endif
788+
747789
[Fact]
748790
public void Configuration_Backplane_SomeType_Valid()
749791
{
@@ -834,6 +876,9 @@ public void Configuration_LoggerFactory_KnownType_Valid()
834876

835877
var config = GetConfiguration(data).GetCacheConfiguration("name");
836878
config.LoggerFactoryType.Should().Be(typeof(Logging.MicrosoftLoggerFactoryAdapter));
879+
880+
var cache = new BaseCacheManager<string>(config);
881+
cache.Add("key", "value").Should().BeTrue();
837882
}
838883

839884
[Fact]
@@ -901,6 +946,7 @@ public void Configuration_Serializer_SomeType_Valid()
901946
}
902947

903948
#if !NETCOREAPP
949+
904950
[Fact]
905951
public void Configuration_Serializer_KnownType_Binary()
906952
{
@@ -921,6 +967,7 @@ public void Configuration_Serializer_KnownType_Binary()
921967
config.SerializerType.Should().Be(typeof(Core.Internal.BinaryCacheSerializer));
922968
act.ShouldNotThrow();
923969
}
970+
924971
#endif
925972

926973
[Fact]
@@ -1024,7 +1071,7 @@ public void Configuration_Serializer_KnownType_BondCompact()
10241071
cache.Add("key", "value");
10251072
};
10261073

1027-
config.SerializerType.Should().Be(typeof(Serialization.Bond.BondCompactBinaryCacheSerializer));
1074+
config.SerializerType.Should().Be(typeof(Serialization.Bond.BondCompactBinaryCacheSerializer));
10281075
act.ShouldNotThrow();
10291076
}
10301077

@@ -1049,7 +1096,6 @@ public void Configuration_Serializer_KnownType_BondFast()
10491096
act.ShouldNotThrow();
10501097
}
10511098

1052-
10531099
[Fact]
10541100
public void Configuration_Serializer_KnownType_BondJosn()
10551101
{
@@ -1168,7 +1214,7 @@ public void Configuration_Redis_Invalid_IsSsl()
11681214
Action act = () => GetConfiguration(data).LoadRedisConfigurations();
11691215
act.ShouldThrow<InvalidOperationException>().WithMessage("*Failed to convert 'invalid'*");
11701216
}
1171-
1217+
11721218
[Fact]
11731219
public void Configuration_Redis_Properties()
11741220
{
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"parallelizeTestCollections": true,
2+
"parallelizeTestCollections": false,
33
"methodDisplay": "method",
44
"preEnumerateTheories": false,
5-
"diagnosticMessages": true,
5+
"diagnosticMessages": false,
66
"longRunningTestSeconds": 5,
77
"maxParallelThreads": 2
88
}

0 commit comments

Comments
 (0)