Skip to content

Commit d026437

Browse files
committed
Add WithCacheKey extensions.
1 parent b65baa5 commit d026437

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed

src/Ardalis.Specification/Builders/Builder_Cache.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,78 @@ public static ICacheSpecificationBuilder<T> EnableCache<T>(
9393
Specification<T>.IsChainDiscarded = !condition;
9494
return (SpecificationBuilder<T>)builder;
9595
}
96+
97+
/// <summary>
98+
/// Sets the cache key for the specification.
99+
/// </summary>
100+
/// <typeparam name="T">The type of the entity.</typeparam>
101+
/// <typeparam name="TResult">The type of the result.</typeparam>
102+
/// <param name="builder">The specification builder.</param>
103+
/// <param name="cacheKey">The cache key to be used.</param>
104+
/// <returns>The updated ordered specification builder.</returns>
105+
/// <exception cref="ArgumentException">If specificationName is null or empty.</exception>
106+
public static ICacheSpecificationBuilder<T, TResult> WithCacheKey<T, TResult>(
107+
this ISpecificationBuilder<T, TResult> builder,
108+
string cacheKey) where T : class
109+
=> WithCacheKey(builder, cacheKey, true);
110+
111+
/// <summary>
112+
/// Sets the cache key for the specification.
113+
/// </summary>
114+
/// <typeparam name="T">The type of the entity.</typeparam>
115+
/// <typeparam name="TResult">The type of the result.</typeparam>
116+
/// <param name="builder">The specification builder.</param>
117+
/// <param name="cacheKey">The cache key to be used.</param>
118+
/// <param name="condition">The condition to evaluate.</param>
119+
/// <returns>The updated ordered specification builder.</returns>
120+
/// <exception cref="ArgumentException">If specificationName is null or empty.</exception>
121+
public static ICacheSpecificationBuilder<T, TResult> WithCacheKey<T, TResult>(
122+
this ISpecificationBuilder<T, TResult> builder,
123+
string cacheKey,
124+
bool condition) where T : class
125+
{
126+
if (condition)
127+
{
128+
builder.Specification.CacheKey = cacheKey;
129+
}
130+
131+
Specification<T, TResult>.IsChainDiscarded = !condition;
132+
return (SpecificationBuilder<T, TResult>)builder;
133+
}
134+
135+
/// <summary>
136+
/// Sets the cache key for the specification.
137+
/// </summary>
138+
/// <typeparam name="T">The type of the entity.</typeparam>
139+
/// <param name="builder">The specification builder.</param>
140+
/// <param name="cacheKey">The cache key to be used.</param>
141+
/// <returns>The updated ordered specification builder.</returns>
142+
/// <exception cref="ArgumentException">If specificationName is null or empty.</exception>
143+
public static ICacheSpecificationBuilder<T> WithCacheKey<T>(
144+
this ISpecificationBuilder<T> builder,
145+
string cacheKey) where T : class
146+
=> WithCacheKey(builder, cacheKey, true);
147+
148+
/// <summary>
149+
/// Sets the cache key for the specification.
150+
/// </summary>
151+
/// <typeparam name="T">The type of the entity.</typeparam>
152+
/// <param name="builder">The specification builder.</param>
153+
/// <param name="cacheKey">The cache key to be used.</param>
154+
/// <param name="condition">The condition to evaluate.</param>
155+
/// <returns>The updated ordered specification builder.</returns>
156+
/// <exception cref="ArgumentException">If specificationName is null or empty.</exception>
157+
public static ICacheSpecificationBuilder<T> WithCacheKey<T>(
158+
this ISpecificationBuilder<T> builder,
159+
string cacheKey,
160+
bool condition) where T : class
161+
{
162+
if (condition)
163+
{
164+
builder.Specification.CacheKey = cacheKey;
165+
}
166+
167+
Specification<T>.IsChainDiscarded = !condition;
168+
return (SpecificationBuilder<T>)builder;
169+
}
96170
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
namespace Tests.Builders;
2+
3+
public class Builder_Cache
4+
{
5+
public record Customer(int Id, string Name);
6+
7+
[Fact]
8+
public void DoesNothing_GivenNoEnableCache()
9+
{
10+
var spec1 = new Specification<Customer>();
11+
var spec2 = new Specification<Customer, string>();
12+
13+
spec1.CacheKey.Should().BeNull();
14+
spec1.CacheEnabled.Should().BeFalse();
15+
16+
spec2.CacheKey.Should().BeNull();
17+
spec2.CacheEnabled.Should().BeFalse();
18+
}
19+
20+
[Fact]
21+
public void DoesNothing_GivenEnableCacheWithFalseCondition()
22+
{
23+
var spec1 = new Specification<Customer>();
24+
spec1.Query
25+
.EnableCache("asd", false);
26+
27+
var spec2 = new Specification<Customer, string>();
28+
spec2.Query
29+
.EnableCache("asd", false);
30+
31+
spec1.CacheKey.Should().BeNull();
32+
spec1.CacheEnabled.Should().BeFalse();
33+
34+
spec2.CacheKey.Should().BeNull();
35+
spec2.CacheEnabled.Should().BeFalse();
36+
}
37+
38+
[Fact]
39+
public void SetsCacheKey_GivenEnableCache()
40+
{
41+
var spec1 = new Specification<Customer>();
42+
spec1.Query
43+
.EnableCache("asd", "x", "y");
44+
45+
var spec2 = new Specification<Customer, string>();
46+
spec2.Query
47+
.EnableCache("asd", "x", "y");
48+
49+
spec1.CacheKey.Should().Be("asd-x-y");
50+
spec1.CacheEnabled.Should().BeTrue();
51+
52+
spec1.CacheKey.Should().Be("asd-x-y");
53+
spec2.CacheEnabled.Should().BeTrue();
54+
}
55+
56+
[Fact]
57+
public void DoesNothing_GivenNoWithCacheKey()
58+
{
59+
var spec1 = new Specification<Customer>();
60+
var spec2 = new Specification<Customer, string>();
61+
62+
spec1.CacheKey.Should().BeNull();
63+
spec1.CacheEnabled.Should().BeFalse();
64+
65+
spec2.CacheKey.Should().BeNull();
66+
spec2.CacheEnabled.Should().BeFalse();
67+
}
68+
69+
[Fact]
70+
public void DoesNothing_GivenWithCacheKeyWithFalseCondition()
71+
{
72+
var key = "someKey";
73+
74+
var spec1 = new Specification<Customer>();
75+
spec1.Query
76+
.WithCacheKey(key, false);
77+
78+
var spec2 = new Specification<Customer, string>();
79+
spec2.Query
80+
.WithCacheKey(key, false);
81+
82+
spec1.CacheKey.Should().BeNull();
83+
spec1.CacheEnabled.Should().BeFalse();
84+
85+
spec2.CacheKey.Should().BeNull();
86+
spec2.CacheEnabled.Should().BeFalse();
87+
}
88+
89+
[Fact]
90+
public void SetsCacheKey_GivenWithCacheKey()
91+
{
92+
var key = "someKey";
93+
94+
var spec1 = new Specification<Customer>();
95+
spec1.Query
96+
.WithCacheKey(key);
97+
98+
var spec2 = new Specification<Customer, string>();
99+
spec2.Query
100+
.WithCacheKey(key);
101+
102+
spec1.CacheKey.Should().Be(key);
103+
spec1.CacheEnabled.Should().BeTrue();
104+
105+
spec1.CacheKey.Should().Be(key);
106+
spec2.CacheEnabled.Should().BeTrue();
107+
}
108+
}

0 commit comments

Comments
 (0)