Skip to content

Commit b5a9b39

Browse files
author
Michael Conrad
authored
Some minor config changes (#399)
1 parent 24507f8 commit b5a9b39

8 files changed

Lines changed: 155 additions & 68 deletions

File tree

benchmarks/CacheManager.Config.Tests/Program.cs

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Garnet.server;
88
using Microsoft.Extensions.DependencyInjection;
99
using Microsoft.Extensions.Logging;
10+
using Microsoft.Extensions.Configuration;
1011

1112
namespace CacheManager.Config.Tests;
1213

@@ -34,8 +35,16 @@ public static async Task Main(string[] args)
3435
var iterations = 100;
3536
try
3637
{
38+
var configuration = new ConfigurationBuilder()
39+
.AddJsonFile("cache.json")
40+
.Build();
41+
42+
3743
var services = new ServiceCollection();
3844

45+
services.AddCacheManager();
46+
services.AddCacheManagerConfiguration(configuration, c => c.WithJsonSerializer());
47+
3948
services.AddLogging(c =>
4049
{
4150
c.AddSystemdConsole();
@@ -47,49 +56,51 @@ public static async Task Main(string[] args)
4756

4857
using var server = StartServer(loggerFactory);
4958

50-
var builder = new Core.CacheConfigurationBuilder("myCache");
59+
var cacheA = provider.GetRequiredService<ICacheManager<string>>();
5160

52-
builder
53-
.WithRetryTimeout(500)
54-
.WithMaxRetries(3);
61+
//var builder = new Core.CacheConfigurationBuilder("myCache");
5562

56-
builder
57-
.WithDictionaryHandle()
58-
.WithExpiration(ExpirationMode.Absolute, TimeSpan.FromSeconds(20))
59-
.DisableStatistics();
63+
//builder
64+
// .WithRetryTimeout(500)
65+
// .WithMaxRetries(3);
6066

61-
builder
62-
.WithRedisCacheHandle("redis", true)
63-
.WithExpiration(ExpirationMode.Absolute, TimeSpan.FromMinutes(60))
64-
.DisableStatistics();
67+
//builder
68+
// .WithDictionaryHandle()
69+
// .WithExpiration(ExpirationMode.Absolute, TimeSpan.FromSeconds(20))
70+
// .DisableStatistics();
6571

66-
builder.WithRedisBackplane("redis");
72+
//builder
73+
// .WithRedisCacheHandle("redis", true)
74+
// .WithExpiration(ExpirationMode.Absolute, TimeSpan.FromMinutes(60))
75+
// .DisableStatistics();
6776

68-
builder.WithRedisConfiguration("redis", config =>
69-
{
70-
config
71-
//.UseTwemproxy()
72-
//.UseCompatibilityMode("2.4")
73-
.WithAllowAdmin()
74-
.WithDatabase(0)
75-
.WithConnectionTimeout(5000)
76-
.EnableKeyspaceEvents()
77-
.WithEndpoint("127.0.0.1", 6379);
78-
});
77+
//builder.WithRedisBackplane("redis");
78+
79+
//builder.WithRedisConfiguration("redis", config =>
80+
//{
81+
// config
82+
// //.UseTwemproxy()
83+
// //.UseCompatibilityMode("2.4")
84+
// .WithAllowAdmin()
85+
// .WithDatabase(0)
86+
// .WithConnectionTimeout(5000)
87+
// .EnableKeyspaceEvents()
88+
// .WithEndpoint("127.0.0.1", 6379);
89+
//});
7990

8091
//builder.WithRedisConfiguration("redis", "localhost:22121");
8192

82-
builder.WithBondCompactBinarySerializer();
93+
//builder.WithBondCompactBinarySerializer();
8394

84-
var cacheA = new BaseCacheManager<string>(builder.Build());
85-
var cacheB = new BaseCacheManager<string>(builder.Build());
95+
//var cacheA = new BaseCacheManager<string>(builder.Build());
96+
//var cacheB = new BaseCacheManager<string>(builder.Build());
8697
cacheA.Clear();
8798

8899
cacheA.Add("key", "value", "region");
89100

90101
var val = cacheA.Get("key", "region");
91102

92-
var val2 = cacheB.AddOrUpdate("key", "region", "added?", (v) => v + "updated");
103+
//var val2 = cacheB.AddOrUpdate("key", "region", "added?", (v) => v + "updated");
93104

94105
await Task.Delay(100);
95106

benchmarks/CacheManager.Config.Tests/Tests.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static void CacheThreadTest(ICacheManager<string> cache, int seed = 42)
7676
public static async Task PumpData(ICacheManager<string> cache, int runForSeconds = 300)
7777
{
7878
var source = new CancellationTokenSource(TimeSpan.FromSeconds(runForSeconds));
79-
var maxItems = 500000;
79+
var maxItems = 1000;
8080
var numReaders = 10;
8181
var puts = 0;
8282
var hits = 0;
@@ -92,14 +92,19 @@ public static async Task PumpData(ICacheManager<string> cache, int runForSeconds
9292
try
9393
{
9494
count++;
95-
cache.Put("key" + count, Guid.NewGuid().ToString());
95+
cache.Put("key" + count, Guid.NewGuid().ToString(), "_region");
9696

9797
Interlocked.Increment(ref puts);
9898

99+
if (count % 100 == 0)
100+
{
101+
cache.Remove("key" + count, "_region");
102+
}
103+
99104
if (count == maxItems)
100105
{
101106
count = 0;
102-
cache.Clear();
107+
// cache.ClearRegion("_region");
103108
Interlocked.Increment(ref putIterations);
104109
}
105110
}
@@ -142,7 +147,7 @@ async Task Read()
142147
{
143148
try
144149
{
145-
var value = cache.Get("key" + rnd.Next(0, maxItems));
150+
var value = cache.Get("key" + rnd.Next(0, maxItems), "_region");
146151
if (value == null)
147152
{
148153
Interlocked.Increment(ref misses);

benchmarks/CacheManager.Config.Tests/cache.json

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,40 @@
22
"$schema": "http://cachemanager.michaco.net/schemas/cachemanager.json#",
33
"redis": [
44
{
5-
"key": "redisConnection",
6-
"endpoints": [
7-
{
8-
"host": "localhost",
9-
"port": 6379
10-
}
11-
],
12-
"allowAdmin": true,
13-
"database": 11
5+
"key": "redisConnection",
6+
"connectionString": "localhost:6379,defaultDatabase=0,allowAdmin=true,abortConnect=false,connectRetry=5,connectTimeout=15000"
147
}
158
],
169
"cacheManagers": [
1710
{
1811
"maxRetries": 1000,
19-
"name": "cachename",
12+
"name": "default",
2013
"retryTimeout": 100,
2114
"updateMode": "Up",
2215
"backplane": {
2316
"key": "redisConnection",
2417
"knownType": "Redis",
25-
"channelName": "test"
26-
},
27-
"loggerFactory": {
28-
"knownType": "Microsoft"
29-
},
30-
"serializer": {
31-
"knownType": "Json"
18+
"channelName": "default"
3219
},
3320
"handles": [
34-
//{
35-
// "knownType": "Dictionary",
36-
// "enablePerformanceCounters": true,
37-
// "enableStatistics": true,
38-
// "expirationMode": "Absolute",
39-
// "expirationTimeout": "0:0:23",
40-
// "isBackplaneSource": false,
41-
// "name": "sys cache"
42-
//},
21+
{
22+
"knownType": "Dictionary",
23+
"enablePerformanceCounters": false,
24+
"enableStatistics": false,
25+
"expirationMode": "Absolute",
26+
// 1 minute
27+
"expirationTimeout": "0:1:00",
28+
"isBackplaneSource": false,
29+
"name": "memory"
30+
},
4331
{
4432
"knownType": "Redis",
4533
"key": "redisConnection",
4634
"isBackplaneSource": true,
4735
"expirationMode": "Sliding",
48-
"expirationTimeout": "00:10:00"
36+
// 10 minutes
37+
"expirationTimeout": "0.00:10:00",
38+
"name": "redis"
4939
}
5040
]
5141
}

src/CacheManager.Core/CacheManagerConfiguration.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43

54
namespace CacheManager.Core
65
{
@@ -42,10 +41,10 @@ public CacheManagerConfiguration()
4241

4342
/// <summary>
4443
/// Gets or sets the limit of the number of retry operations per action.
45-
/// <para>Default is 50.</para>
44+
/// <para>Default is 10.</para>
4645
/// </summary>
4746
/// <value>The maximum retries.</value>
48-
public int MaxRetries { get; set; } = 50;
47+
public int MaxRetries { get; set; } = 10;
4948

5049
/// <summary>
5150
/// Gets or sets the number of milliseconds the cache should wait before it will retry an action.
@@ -127,4 +126,4 @@ public override string ToString()
127126
return $"{Name}: {string.Join(", ", CacheHandleConfigurations)}";
128127
}
129128
}
130-
}
129+
}

src/CacheManager.Core/Configuration/CacheConfigurationBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ internal static CacheManagerConfiguration LoadFromSection(CacheManagerSection se
260260
var cfg = new CacheManagerConfiguration()
261261
{
262262
UpdateMode = managerCfg.UpdateMode,
263-
MaxRetries = maxRetries ?? 50,
263+
MaxRetries = maxRetries ?? 10,
264264
RetryTimeout = retryTimeout ?? 100
265265
};
266266

@@ -651,7 +651,7 @@ public ConfigurationBuilderCacheHandlePart WithHandle(Type cacheHandleBaseType)
651651

652652
/// <summary>
653653
/// Sets the maximum number of retries per action.
654-
/// <para>Default is 50.</para>
654+
/// <para>Default is 10.</para>
655655
/// <para>
656656
/// Not every cache handle implements this, usually only distributed caches will use it.
657657
/// </para>

src/CacheManager.Core/ICacheManagerConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public interface IReadOnlyCacheManagerConfiguration
7777

7878
/// <summary>
7979
/// Gets the limit of the number of retry operations per action.
80-
/// <para>Default is 50.</para>
80+
/// <para>Default is 10.</para>
8181
/// </summary>
8282
/// <value>The maximum retries.</value>
8383
int MaxRetries { get; }

test/CacheManager.Tests/CacheManagerAdvancedUpdateTests.cs renamed to test/CacheManager.Tests/SyntheticCacheHandleTests.cs

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Diagnostics.CodeAnalysis;
33
using System.Linq;
4+
using System.Threading;
45
using CacheManager.Core;
56
using CacheManager.Core.Internal;
67

@@ -11,8 +12,89 @@
1112
namespace CacheManager.Tests
1213
{
1314
[ExcludeFromCodeCoverage]
14-
public class CacheManagerAdvancedUpdateTests : IClassFixture<RedisTestFixture>
15+
public class SyntheticCacheHandleTests : IClassFixture<RedisTestFixture>
1516
{
17+
[Fact]
18+
public void TryGetOrAdd_Retry_Test()
19+
{
20+
// test that configuring x retries does exactly that many retries.
21+
var countCalls = 0;
22+
var cache = CacheFactory.Build<string>(settings =>
23+
{
24+
settings.WithHandle(typeof(MockCacheHandle<>));
25+
settings.WithMaxRetries(5);
26+
});
27+
28+
var handle = cache.CacheHandles.ElementAt(0) as MockCacheHandle<string>;
29+
30+
handle.GetCallValue = null; // always fail to trigger retry;
31+
32+
handle.AddCall = () =>
33+
{
34+
Interlocked.Increment(ref countCalls);
35+
return false; // always fail to trigger retry;
36+
};
37+
38+
cache.TryGetOrAdd("key", old => old + "new", out string newValue);
39+
40+
// Expecting one normal exec + retries
41+
Assert.Equal(6, countCalls);
42+
}
43+
44+
[Fact]
45+
public void GetOrAdd_Retry_Test()
46+
{
47+
// test that configuring x retries does exactly that many retries.
48+
var countCalls = 0;
49+
var cache = CacheFactory.Build<string>(settings =>
50+
{
51+
settings.WithHandle(typeof(MockCacheHandle<>));
52+
settings.WithMaxRetries(5);
53+
});
54+
55+
var handle = cache.CacheHandles.ElementAt(0) as MockCacheHandle<string>;
56+
57+
handle.GetCallValue = null; // always fail to trigger retry;
58+
59+
handle.AddCall = () =>
60+
{
61+
Interlocked.Increment(ref countCalls);
62+
return false; // always fail to trigger retry;
63+
};
64+
65+
Assert.Throws<InvalidOperationException>(() => cache.GetOrAdd("key", old => old + "new"));
66+
67+
// Expecting one normal exec + retries
68+
Assert.Equal(6, countCalls);
69+
}
70+
71+
[Fact]
72+
public void AddOrUpdate_Retry_Test()
73+
{
74+
// test that configuring x retries does exactly that many retries.
75+
var countCalls = 0;
76+
var cache = CacheFactory.Build<string>(settings =>
77+
{
78+
settings.WithHandle(typeof(MockCacheHandle<>));
79+
settings.WithMaxRetries(5);
80+
});
81+
82+
var handle = cache.CacheHandles.ElementAt(0) as MockCacheHandle<string>;
83+
84+
handle.GetCallValue = null; // always fail to trigger retry;
85+
handle.UpdateValue = UpdateItemResult.ForFactoryReturnedNull<string>();
86+
handle.AddCall = () =>
87+
{
88+
Interlocked.Increment(ref countCalls);
89+
return false; // always fail to trigger retry;
90+
};
91+
92+
Assert.Throws<InvalidOperationException>(() => cache.AddOrUpdate("key", "addValue", old => old + "new"));
93+
94+
// Expecting one normal exec + retries
95+
Assert.Equal(6, countCalls);
96+
}
97+
1698
[Theory]
1799
[ClassData(typeof(TestCacheManagers))]
18100
public void Update_ThrowsIf_FactoryReturnsNull(ICacheManager<object> cache)

test/CacheManager.Tests/ValidConfigurationValidationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public void Cfg_Valid_CfgFile_AllDefaults()
148148
cache.Configuration.LoggerFactoryType.Should().BeNull();
149149
cache.Configuration.BackplaneType.Should().BeNull();
150150
cache.Configuration.RetryTimeout.Should().Be(100);
151-
cache.Configuration.MaxRetries.Should().Be(50);
151+
cache.Configuration.MaxRetries.Should().Be(10);
152152
cache.CacheHandles.Count().Should().Be(1);
153153
AssertCacheHandleConfig(cache.CacheHandles.ElementAt(0), "defaultsHandle", ExpirationMode.None, TimeSpan.Zero);
154154
}

0 commit comments

Comments
 (0)