Skip to content

Commit

Permalink
Prepend a static prefix to cache keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Baune8D committed Oct 17, 2017
1 parent 698d6f2 commit 1413f02
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/ExpressionCache.Core/ExpressionCacheBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace ExpressionCache.Core
{
public abstract class ExpressionCacheBase : IExpressionCacheBase
{
public const string CachePrefix = "ExprCache";

private readonly IMemoryCache _internalCache;

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

return keyBuilder.ToString();
return CachePrefix + keyBuilder;
}

private static string GenerateCacheKey(string baseKey, object[] arguments)
Expand Down
24 changes: 12 additions & 12 deletions test/ExpressionCache.Core.Tests/ExpressionCacheBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public void GetKey_FunctionWithoutParameters_ShouldReturnCacheKeyString()
var key = fixture.ExpressionCacheBase.GetKey(() => _testFunctions.FunctionWithoutParameters());
var asyncKey = fixture.ExpressionCacheBase.GetKey(() => _testFunctions.FunctionWithoutParametersAsync());

key.ShouldBe(
key.ShouldBe(CacheKeyHelper.Prefix(
CacheKeyHelper.Format(_testFunctions.ClassName) +
CacheKeyHelper.Format(nameof(_testFunctions.FunctionWithoutParameters))
);
asyncKey.ShouldBe(
));
asyncKey.ShouldBe(CacheKeyHelper.Prefix(
CacheKeyHelper.Format(_testFunctions.ClassName) +
CacheKeyHelper.Format(nameof(_testFunctions.FunctionWithoutParametersAsync))
);
));
}
}

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

key.ShouldBe(
key.ShouldBe(CacheKeyHelper.Prefix(
CacheKeyHelper.Format(_testFunctions.ClassName) +
CacheKeyHelper.Format(nameof(_testFunctions.FunctionWithOneParameter)) +
CacheKeyHelper.Format(parameter)
);
asyncKey.ShouldBe(
));
asyncKey.ShouldBe(CacheKeyHelper.Prefix(
CacheKeyHelper.Format(_testFunctions.ClassName) +
CacheKeyHelper.Format(nameof(_testFunctions.FunctionWithOneParameterAsync)) +
CacheKeyHelper.Format(parameter)
);
));
}
}

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

key.ShouldBe(
key.ShouldBe(CacheKeyHelper.Prefix(
CacheKeyHelper.Format(_testFunctions.ClassName) +
CacheKeyHelper.Format(nameof(_testFunctions.FunctionWithTwoParameters)) +
CacheKeyHelper.Format(parameterOne) +
CacheKeyHelper.Format(parameterTwo)
);
asyncKey.ShouldBe(
));
asyncKey.ShouldBe(CacheKeyHelper.Prefix(
CacheKeyHelper.Format(_testFunctions.ClassName) +
CacheKeyHelper.Format(nameof(_testFunctions.FunctionWithTwoParametersAsync)) +
CacheKeyHelper.Format(parameterOne) +
CacheKeyHelper.Format(parameterTwo)
);
));
}
}

Expand Down
5 changes: 5 additions & 0 deletions test/ExpressionCache.Core.Tests/TestHelpers/CacheKeyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
{
public static class CacheKeyHelper
{
public static string Prefix(string key)
{
return ExpressionCacheBase.CachePrefix + key;
}

public static string Format<TResult>(TResult value)
{
return "{" + value + "}";
Expand Down

0 comments on commit 1413f02

Please sign in to comment.