-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathStackExchangeRedisCacheTest.cs
378 lines (336 loc) · 14.6 KB
/
StackExchangeRedisCacheTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
namespace Masa.Contrib.Caching.Distributed.StackExchangeRedis.Tests;
#pragma warning disable CS0618
[TestClass]
public class StackExchangeRedisCacheTest : TestBase
{
[TestMethod]
public void TestAddStackExchangeRedisCache()
{
var services = new ServiceCollection();
services.AddDistributedCache(distributedCacheOptions => distributedCacheOptions.UseStackExchangeRedisCache(option =>
{
option.DefaultDatabase = 1;
option.Servers = new List<RedisServerOptions>()
{
new(REDIS_HOST)
};
option.GlobalCacheOptions = new CacheOptions()
{
CacheKeyType = CacheKeyType.None
};
}));
var serviceProvider = services.BuildServiceProvider();
var options = serviceProvider.GetService<IOptions<RedisConfigurationOptions>>();
Assert.IsNotNull(options);
Assert.AreEqual(1, options.Value.DefaultDatabase);
Assert.AreEqual(1, options.Value.Servers.Count);
Assert.AreEqual(REDIS_HOST, options.Value.Servers[0].Host);
Assert.AreEqual(6379, options.Value.Servers[0].Port);
var distributedCacheClient = serviceProvider.GetService<IDistributedCacheClient>();
Assert.IsNotNull(distributedCacheClient);
string key = "test_key";
distributedCacheClient.Set(key, "content");
Assert.IsTrue(distributedCacheClient.Exists(key));
distributedCacheClient.Remove(key);
}
[TestMethod]
public void TestAddMultiStackExchangeRedisCache()
{
var services = new ServiceCollection();
services.AddStackExchangeRedisCache("test", option =>
{
option.DefaultDatabase = 1;
option.Servers = new List<RedisServerOptions>()
{
new(REDIS_HOST)
};
option.GlobalCacheOptions = new CacheOptions()
{
CacheKeyType = CacheKeyType.None
};
});
services.AddStackExchangeRedisCache("test2", new RedisConfigurationOptions()
{
DefaultDatabase = 2,
Servers = new List<RedisServerOptions>()
{
new(REDIS_HOST)
},
GlobalCacheOptions = new CacheOptions()
{
CacheKeyType = CacheKeyType.None
}
});
var serviceProvider = services.BuildServiceProvider();
var factory = serviceProvider.GetRequiredService<IDistributedCacheClientFactory>();
var distributedCacheClient = factory.Create("test");
Assert.IsNotNull(distributedCacheClient);
string key = "test_key";
distributedCacheClient.Set(key, "content");
Assert.IsTrue(distributedCacheClient.Exists(key));
var distributedCacheClient2 = factory.Create("test2");
Assert.IsFalse(distributedCacheClient2.Exists(key));
distributedCacheClient2.Set(key + "2", "content_2");
Assert.AreEqual("content_2", distributedCacheClient2.Get<string>(key + "2"));
Assert.AreEqual(null, distributedCacheClient.Get<string>(key + "2"));
distributedCacheClient.Remove(key);
distributedCacheClient2.Remove(key + "2");
}
[TestMethod]
public void TestAddStackExchangeRedisCacheByAppsettings()
{
var builder = WebApplication.CreateBuilder();
var rootPath = builder.Environment.ContentRootPath;
var services = builder.Services;
services.AddStackExchangeRedisCache("test");
var serviceProvider = services.BuildServiceProvider();
var distributedCacheClient = serviceProvider.GetRequiredService<IDistributedCacheClient>();
string key = "test_1";
distributedCacheClient.Set(key, "test_content");
Assert.IsTrue(distributedCacheClient.Exists(key));
var oldContent = File.ReadAllText(Path.Combine(rootPath, "appsettings.json"));
File.WriteAllText(Path.Combine(rootPath, "appsettings.json"),
JsonSerializer.Serialize(new
{
RedisConfig = new RedisConfigurationOptions()
{
Servers = new List<RedisServerOptions>()
{
new(REDIS_HOST, 6379)
},
DefaultDatabase = 1
}
}));
Task.Delay(3000).ConfigureAwait(false).GetAwaiter().GetResult();
distributedCacheClient = serviceProvider.GetRequiredService<IDistributedCacheClientFactory>().Create();
var exist = distributedCacheClient.Exists(key);
Assert.IsFalse(exist);
File.WriteAllText(Path.Combine(Path.Combine(rootPath, "appsettings.json")), oldContent);
Task.Delay(3000).ConfigureAwait(false).GetAwaiter().GetResult();
distributedCacheClient.Remove(key);
}
[TestMethod]
public void TestAddStackExchangeRedisCacheRepeat()
{
var services = new ServiceCollection();
services.AddStackExchangeRedisCache(options =>
{
options.DefaultDatabase = 1;
options.Servers = new List<RedisServerOptions>()
{
new(REDIS_HOST)
};
options.GlobalCacheOptions = new CacheOptions()
{
CacheKeyType = CacheKeyType.None
};
});
services.AddStackExchangeRedisCache(new RedisConfigurationOptions()
{
DefaultDatabase = 2,
Servers = new List<RedisServerOptions>()
{
new(REDIS_HOST)
},
GlobalCacheOptions = new CacheOptions()
{
CacheKeyType = CacheKeyType.None
}
});
var serviceProvider = services.BuildServiceProvider();
var distributedCacheClient = serviceProvider.GetService<IDistributedCacheClient>();
Assert.IsNotNull(distributedCacheClient);
Assert.IsTrue(distributedCacheClient is RedisCacheClient redisClient);
var fieldInfo = typeof(RedisCacheClient).GetField("Db", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
Assert.IsNotNull(fieldInfo);
var value = fieldInfo.GetValue((RedisCacheClient)distributedCacheClient);
Assert.IsNotNull(value);
Assert.AreEqual(1, ((IDatabase)value).Database);
}
[TestMethod]
public void TestAddStackExchangeRedisCacheRepeatByConfiguration()
{
var builder = WebApplication.CreateBuilder();
builder.Services.AddStackExchangeRedisCache();
builder.Services.AddStackExchangeRedisCache(Options.DefaultName, "RedisConfig2");
builder.Services.AddStackExchangeRedisCache(Options.DefaultName, "RedisConfig3");
var serviceProvider = builder.Services.BuildServiceProvider();
var distributedCacheClient = serviceProvider.GetService<IDistributedCacheClient>();
Assert.IsNotNull(distributedCacheClient);
Assert.IsTrue(distributedCacheClient is RedisCacheClient redisClient);
var fieldInfo = typeof(RedisCacheClient).GetField("Db", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
Assert.IsNotNull(fieldInfo);
var value = fieldInfo.GetValue((RedisCacheClient)distributedCacheClient);
Assert.IsNotNull(value);
Assert.AreEqual(6, ((IDatabase)value).Database);
}
[TestMethod]
public void TestCachingBuilder()
{
var services = new ServiceCollection();
var cachingBuilder = services.AddStackExchangeRedisCache(options =>
{
options.Servers = new List<RedisServerOptions>()
{
new()
};
});
Assert.AreEqual(Options.DefaultName, cachingBuilder.Name);
Assert.AreEqual(services, cachingBuilder.Services);
cachingBuilder = services.AddStackExchangeRedisCache("test", options =>
{
options.Servers = new List<RedisServerOptions>()
{
new()
};
});
Assert.AreEqual("test", cachingBuilder.Name);
}
[TestMethod]
public void TestFormatCacheKey()
{
var services = new ServiceCollection();
services.AddDistributedCache("test", distributedCacheOptions => distributedCacheOptions.UseStackExchangeRedisCache(options =>
{
options.Servers = new List<RedisServerOptions>()
{
new()
};
options.GlobalCacheOptions = new CacheOptions()
{
CacheKeyType = CacheKeyType.TypeName
};
}));
services.AddDistributedCache("test2", distributedCacheOptions => distributedCacheOptions.UseStackExchangeRedisCache(options =>
{
options.Servers = new List<RedisServerOptions>()
{
new(),
};
options.DefaultDatabase = 0;
options.GlobalCacheOptions = new CacheOptions()
{
CacheKeyType = CacheKeyType.None
};
}));
var serviceProvider = services.BuildServiceProvider();
var distributedCacheClientFactory = serviceProvider.GetRequiredService<IDistributedCacheClientFactory>();
var key = "redisConfig";
var distributedCacheClient = distributedCacheClientFactory.Create("test");
Assert.IsNotNull(distributedCacheClient);
distributedCacheClient.Remove<string>(key);
distributedCacheClient.Set(key, "redis configuration json");
var value = distributedCacheClient.Get<string>(key);
Assert.AreEqual("redis configuration json", value);
var distributedCacheClient2 = distributedCacheClientFactory.Create("test2");
Assert.IsNotNull(distributedCacheClient2);
Assert.IsFalse(distributedCacheClient2.Exists(key));
Assert.IsFalse(distributedCacheClient2.Exists<string>(key));
distributedCacheClient2.Set(key, "redis configuration2 json");
var value2 = distributedCacheClient2.Get<string>(key);
Assert.AreEqual("redis configuration2 json", value2);
distributedCacheClient.Remove<string>(key);
distributedCacheClient2.Remove<string>(key);
}
[TestMethod]
public void TestFormatCacheKeyByTypeNameAlias()
{
var builder = WebApplication.CreateBuilder();
builder.Services.AddDistributedCache("test", distributedCacheOptions =>
{
distributedCacheOptions.UseStackExchangeRedisCache("RedisConfig4");
});
var serviceProvider = builder.Services.BuildServiceProvider();
var distributedCacheClient = serviceProvider.GetRequiredService<IDistributedCacheClientFactory>().Create("test");
Assert.IsNotNull(distributedCacheClient);
Assert.ThrowsException<NotImplementedException>(() =>
{
distributedCacheClient.GetOrSet("redisConfiguration", () => new CacheEntry<string>("redis configuration2 json"));
});
}
[TestMethod]
public void TestFormatCacheKeyByTypeNameAlias2()
{
var builder = WebApplication.CreateBuilder();
builder.Services.AddDistributedCache("test", distributedCacheOptions =>
{
distributedCacheOptions.UseStackExchangeRedisCache("RedisConfig4");
});
builder.Services.Configure("test", (TypeAliasOptions options) =>
{
options.GetAllTypeAliasFunc = () => new Dictionary<string, string>()
{
{ "String", "s" }
};
});
var serviceProvider = builder.Services.BuildServiceProvider();
var distributedCacheClient = serviceProvider.GetRequiredService<IDistributedCacheClientFactory>().Create("test");
Assert.IsNotNull(distributedCacheClient);
var value = distributedCacheClient.GetOrSet("redisConfiguration", () => new CacheEntry<string>("redis configuration2 json"));
Assert.AreEqual("redis configuration2 json", value);
distributedCacheClient.Remove<string>("redisConfiguration");
}
[TestMethod]
public void TestFormatCacheKeyByTypeNameAlias3()
{
var builder = WebApplication.CreateBuilder();
builder.Services.AddDistributedCache(
distributedCacheOptions =>
{
distributedCacheOptions.UseStackExchangeRedisCache("RedisConfig4");
},
typeAliasOptions =>
{
typeAliasOptions.GetAllTypeAliasFunc = () => new Dictionary<string, string>()
{
{ "String", "s" }
};
});
builder.Services.Configure((TypeAliasOptions options) =>
{
options.GetAllTypeAliasFunc = () => new Dictionary<string, string>()
{
{ "String", "s" }
};
});
var serviceProvider = builder.Services.BuildServiceProvider();
var distributedCacheClient = serviceProvider.GetRequiredService<IDistributedCacheClientFactory>().Create();
Assert.IsNotNull(distributedCacheClient);
var value = distributedCacheClient.GetOrSet("redisConfiguration", () => new CacheEntry<string>("redis configuration2 json"));
Assert.AreEqual("redis configuration2 json", value);
distributedCacheClient.Remove<string>("redisConfiguration");
}
[TestMethod]
public void TestFormatCacheKeyByTypeNameAlias4()
{
var builder = WebApplication.CreateBuilder();
builder.Services.AddDistributedCache(
distributedCacheOptions =>
{
distributedCacheOptions.UseStackExchangeRedisCache(builder.Configuration.GetSection("RedisConfig4"));
},
typeAliasOptions =>
{
typeAliasOptions.GetAllTypeAliasFunc = () => new Dictionary<string, string>()
{
{ "String", "s" }
};
});
builder.Services.Configure((TypeAliasOptions options) =>
{
options.GetAllTypeAliasFunc = () => new Dictionary<string, string>()
{
{ "String", "s" }
};
});
var serviceProvider = builder.Services.BuildServiceProvider();
var distributedCacheClient = serviceProvider.GetRequiredService<IDistributedCacheClientFactory>().Create();
Assert.IsNotNull(distributedCacheClient);
var value = distributedCacheClient.GetOrSet("redisConfiguration", () => new CacheEntry<string>("redis configuration2 json"));
Assert.AreEqual("redis configuration2 json", value);
distributedCacheClient.Remove<string>("redisConfiguration");
}
}
#pragma warning restore CS0618