Skip to content

Commit 1413f02

Browse files
author
Morten Larsen
committed
Prepend a static prefix to cache keys
1 parent 698d6f2 commit 1413f02

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

src/ExpressionCache.Core/ExpressionCacheBase.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ namespace ExpressionCache.Core
88
{
99
public abstract class ExpressionCacheBase : IExpressionCacheBase
1010
{
11+
public const string CachePrefix = "ExprCache";
12+
1113
private readonly IMemoryCache _internalCache;
1214

1315
protected IExpressionCacheProvider Provider { get; }
@@ -127,7 +129,7 @@ private static string GenerateBaseCacheKey(MethodCallExpression methodCall, Meth
127129
.By(methodInfo.Name)
128130
.By(methodInfo.GetGenericArguments());
129131

130-
return keyBuilder.ToString();
132+
return CachePrefix + keyBuilder;
131133
}
132134

133135
private static string GenerateCacheKey(string baseKey, object[] arguments)

test/ExpressionCache.Core.Tests/ExpressionCacheBaseTests.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ public void GetKey_FunctionWithoutParameters_ShouldReturnCacheKeyString()
2424
var key = fixture.ExpressionCacheBase.GetKey(() => _testFunctions.FunctionWithoutParameters());
2525
var asyncKey = fixture.ExpressionCacheBase.GetKey(() => _testFunctions.FunctionWithoutParametersAsync());
2626

27-
key.ShouldBe(
27+
key.ShouldBe(CacheKeyHelper.Prefix(
2828
CacheKeyHelper.Format(_testFunctions.ClassName) +
2929
CacheKeyHelper.Format(nameof(_testFunctions.FunctionWithoutParameters))
30-
);
31-
asyncKey.ShouldBe(
30+
));
31+
asyncKey.ShouldBe(CacheKeyHelper.Prefix(
3232
CacheKeyHelper.Format(_testFunctions.ClassName) +
3333
CacheKeyHelper.Format(nameof(_testFunctions.FunctionWithoutParametersAsync))
34-
);
34+
));
3535
}
3636
}
3737

@@ -45,16 +45,16 @@ public void GetKey_FunctionWithOneParameter_ShouldReturnCacheKeyString()
4545
var key = fixture.ExpressionCacheBase.GetKey(() => _testFunctions.FunctionWithOneParameter(parameter));
4646
var asyncKey = fixture.ExpressionCacheBase.GetKey(() => _testFunctions.FunctionWithOneParameterAsync(parameter));
4747

48-
key.ShouldBe(
48+
key.ShouldBe(CacheKeyHelper.Prefix(
4949
CacheKeyHelper.Format(_testFunctions.ClassName) +
5050
CacheKeyHelper.Format(nameof(_testFunctions.FunctionWithOneParameter)) +
5151
CacheKeyHelper.Format(parameter)
52-
);
53-
asyncKey.ShouldBe(
52+
));
53+
asyncKey.ShouldBe(CacheKeyHelper.Prefix(
5454
CacheKeyHelper.Format(_testFunctions.ClassName) +
5555
CacheKeyHelper.Format(nameof(_testFunctions.FunctionWithOneParameterAsync)) +
5656
CacheKeyHelper.Format(parameter)
57-
);
57+
));
5858
}
5959
}
6060

@@ -69,18 +69,18 @@ public void GetKey_FunctionWithTwoParameters_ShouldReturnCacheKeyString()
6969
var key = fixture.ExpressionCacheBase.GetKey(() => _testFunctions.FunctionWithTwoParameters(parameterOne, parameterTwo));
7070
var asyncKey = fixture.ExpressionCacheBase.GetKey(() => _testFunctions.FunctionWithTwoParametersAsync(parameterOne, parameterTwo));
7171

72-
key.ShouldBe(
72+
key.ShouldBe(CacheKeyHelper.Prefix(
7373
CacheKeyHelper.Format(_testFunctions.ClassName) +
7474
CacheKeyHelper.Format(nameof(_testFunctions.FunctionWithTwoParameters)) +
7575
CacheKeyHelper.Format(parameterOne) +
7676
CacheKeyHelper.Format(parameterTwo)
77-
);
78-
asyncKey.ShouldBe(
77+
));
78+
asyncKey.ShouldBe(CacheKeyHelper.Prefix(
7979
CacheKeyHelper.Format(_testFunctions.ClassName) +
8080
CacheKeyHelper.Format(nameof(_testFunctions.FunctionWithTwoParametersAsync)) +
8181
CacheKeyHelper.Format(parameterOne) +
8282
CacheKeyHelper.Format(parameterTwo)
83-
);
83+
));
8484
}
8585
}
8686

test/ExpressionCache.Core.Tests/TestHelpers/CacheKeyHelper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
{
33
public static class CacheKeyHelper
44
{
5+
public static string Prefix(string key)
6+
{
7+
return ExpressionCacheBase.CachePrefix + key;
8+
}
9+
510
public static string Format<TResult>(TResult value)
611
{
712
return "{" + value + "}";

0 commit comments

Comments
 (0)