Skip to content

Commit f6eb825

Browse files
author
dmalanij
committed
PerUserCacheKeyGenerator implementation
Created a class for generating cache keys considering the current user identity
1 parent 82fc5b7 commit f6eb825

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Net.Http.Headers;
2+
using System.Web.Http.Controllers;
3+
4+
namespace WebApi.OutputCache.V2
5+
{
6+
public class PerUserCacheKeyGenerator : DefaultCacheKeyGenerator
7+
{
8+
public override string MakeCacheKey(HttpActionContext context, MediaTypeHeaderValue mediaType, bool excludeQueryString = false)
9+
{
10+
var baseKey = MakeBaseKey(context);
11+
var parameters = FormatParameters(context, excludeQueryString);
12+
var userIdentity = FormatUserIdentity(context);
13+
14+
return string.Format("{0}{1}:{2}:{3}", baseKey, parameters, userIdentity, mediaType);
15+
}
16+
17+
protected virtual string FormatUserIdentity(HttpActionContext context)
18+
{
19+
return context.RequestContext.Principal.Identity.Name.ToLower();
20+
}
21+
}
22+
}

src/WebApi.OutputCache.V2/WebApi.OutputCache.V2.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<Compile Include="ICacheKeyGenerator.cs" />
6464
<Compile Include="IgnoreCacheOutputAttribute.cs" />
6565
<Compile Include="InvalidateCacheOutputAttribute.cs" />
66+
<Compile Include="PerUserCacheKeyGenerator.cs" />
6667
<Compile Include="Properties\AssemblyInfo.cs" />
6768
<Compile Include="CacheOutputAttribute.cs" />
6869
<Compile Include="TimeAttributes\CacheOutputUntilCacheAttribute.cs" />
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using NUnit.Framework;
2+
using System;
3+
using System.Security.Principal;
4+
5+
namespace WebApi.OutputCache.V2.Tests
6+
{
7+
[TestFixture]
8+
public class PerUserCacheKeyGeneratorTests : CacheKeyGenerationTestsBase<PerUserCacheKeyGenerator>
9+
{
10+
private const string UserIdentityName = "SomeUserIDon'tMind";
11+
12+
[SetUp]
13+
public override void Setup()
14+
{
15+
base.Setup();
16+
context.RequestContext.Principal = new GenericPrincipal(new GenericIdentity(UserIdentityName), new string[0]);
17+
}
18+
19+
protected override PerUserCacheKeyGenerator BuildCacheKeyGenerator()
20+
{
21+
return new PerUserCacheKeyGenerator();
22+
}
23+
24+
private string FormatUserIdentityForAssertion()
25+
{
26+
return UserIdentityName.ToLower();
27+
}
28+
29+
[Test]
30+
public void NoParametersIncludeQueryString_ShouldReturnBaseKeyAndQueryStringAndUserIdentityAndMediaTypeConcatenated()
31+
{
32+
var cacheKey = cacheKeyGenerator.MakeCacheKey(context, mediaType, false);
33+
34+
AssertCacheKeysBasicFormat(cacheKey);
35+
Assert.AreEqual(String.Format("{0}-{1}:{2}:{3}", BaseCacheKey, requestUri.Query.Substring(1), FormatUserIdentityForAssertion(), mediaType), cacheKey,
36+
"Key does not match expected <BaseKey>-<QueryString>:<UserIdentity>:<MediaType>");
37+
}
38+
39+
[Test]
40+
public void NoParametersExcludeQueryString_ShouldReturnBaseKeyAndUserIdentityAndMediaTypeConcatenated()
41+
{
42+
var cacheKey = cacheKeyGenerator.MakeCacheKey(context, mediaType, true);
43+
44+
AssertCacheKeysBasicFormat(cacheKey);
45+
Assert.AreEqual(String.Format("{0}:{1}:{2}", BaseCacheKey, FormatUserIdentityForAssertion(), mediaType), cacheKey,
46+
"Key does not match expected <BaseKey>:<UserIdentity>:<MediaType>");
47+
}
48+
49+
[Test]
50+
public void WithParametersIncludeQueryString_ShouldReturnBaseKeyAndArgumentsAndQueryStringAndUserIdentityAndMediaTypeConcatenated()
51+
{
52+
AddActionArgumentsToContext();
53+
var cacheKey = cacheKeyGenerator.MakeCacheKey(context, mediaType, false);
54+
55+
AssertCacheKeysBasicFormat(cacheKey);
56+
Assert.AreEqual(String.Format("{0}-{1}&{2}:{3}:{4}", BaseCacheKey, FormatActionArgumentsForKeyAssertion(), requestUri.Query.Substring(1), FormatUserIdentityForAssertion(), mediaType), cacheKey,
57+
"Key does not match expected <BaseKey>-<Arguments>&<QueryString>:<UserIdentity>:<MediaType>");
58+
}
59+
60+
[Test]
61+
public void WithParametersExcludeQueryString_ShouldReturnBaseKeyAndArgumentsAndUserIdentityAndMediaTypeConcatenated()
62+
{
63+
AddActionArgumentsToContext();
64+
var cacheKey = cacheKeyGenerator.MakeCacheKey(context, mediaType, true);
65+
66+
AssertCacheKeysBasicFormat(cacheKey);
67+
Assert.AreEqual(String.Format("{0}-{1}:{2}:{3}", BaseCacheKey, FormatActionArgumentsForKeyAssertion(), FormatUserIdentityForAssertion(), mediaType), cacheKey,
68+
"Key does not match expected <BaseKey>-<Arguments>:<UserIdentity>:<MediaType>");
69+
}
70+
}
71+
}

test/WebApi.OutputCache.V2.Tests/WebApi.OutputCache.V2.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<Compile Include="ConnegTests.cs">
8585
<SubType>Code</SubType>
8686
</Compile>
87+
<Compile Include="PerUserCacheKeyGeneratorTests.cs" />
8788
<Compile Include="DefaultCacheKeyGeneratorTests.cs" />
8889
<Compile Include="InlineInvalidateTests.cs">
8990
<SubType>Code</SubType>

0 commit comments

Comments
 (0)