Skip to content

Commit e6f75f2

Browse files
FIND-13403: Support unique cache for query
- Add SingleResultCache for set cache_uniq=true
1 parent 06f640d commit e6f75f2

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

APIs/src/EpiServer.ContentGraph/Api/Querying/GraphQueryBuilder.cs

+19-12
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
using EPiServer.ServiceLocation;
1212
using EPiServer.ContentGraph.Helpers.Text;
1313
using EPiServer.ContentGraph.Helpers;
14+
using EPiServer.ContentGraph.Tracing;
1415

1516
namespace EPiServer.ContentGraph.Api.Querying
1617
{
17-
public class GraphQueryBuilder : IQuery
18+
public class GraphQueryBuilder : IQuery, ITraceable
1819
{
1920
private readonly IHttpClientFactory _httpClientFactory;
2021
private readonly HttpClient _httpClient;
@@ -24,7 +25,9 @@ public class GraphQueryBuilder : IQuery
2425
private const string UnCachedPath = "?cache=false";
2526
private Dictionary<string, IFragmentBuilder> _fragmentBuilders;
2627
private readonly List<string> typeQueries = new List<string>();
27-
public Action<JsonRequest> RequestActions;
28+
public Action<OptiGraphOptions> GraphOptionsAction;
29+
30+
Guid ITraceable.TraceId => Guid.NewGuid();
2831

2932
public GraphQueryBuilder()
3033
{
@@ -116,6 +119,7 @@ public GraphQueryBuilder OperationName(string op)
116119
}
117120
private string GetServiceUrl()
118121
{
122+
UpdateQueryPath();
119123
return _optiGraphOptions.GatewayAddress + _optiGraphOptions.QueryPath;
120124
}
121125
private string GetAuthorization(string body)
@@ -311,19 +315,22 @@ private void AdditionalInformation(JsonRequest request, string body)
311315
{
312316
request.AddRequestHeader("cg-include-deleted", "true");
313317
}
314-
if (!_optiGraphOptions.Cache)
315-
{
316-
Regex regex = new Regex(@"\?cache=\w*");
317-
_optiGraphOptions.QueryPath = _optiGraphOptions.QueryPath.Replace(regex.Match(_optiGraphOptions.QueryPath).Value, UnCachedPath);
318-
}
319-
//apply actions on request before send
320-
ApplyRequestActions(request);
321318
}
322-
internal void ApplyRequestActions(JsonRequest request)
319+
private void UpdateQueryPath()
323320
{
324-
if (RequestActions.IsNotNull())
321+
Regex regex = new Regex(@"\?cache=\w*");
322+
if (regex.IsMatch(_optiGraphOptions.QueryPath))
325323
{
326-
RequestActions(request);
324+
_optiGraphOptions.QueryPath =
325+
_optiGraphOptions.QueryPath
326+
.Replace(regex.Match(_optiGraphOptions.QueryPath).Value, $"?cache={_optiGraphOptions.Cache.ToString().ToLower()}");
327+
}
328+
else
329+
{
330+
_optiGraphOptions.QueryPath = $"{_optiGraphOptions.QueryPath}?cache={_optiGraphOptions.Cache.ToString().ToLower()}";
331+
}
332+
if (GraphOptionsAction.IsNotNull()) {
333+
GraphOptionsAction(_optiGraphOptions);
327334
}
328335
}
329336
public void AddQuery(string typeQuery)

APIs/src/EpiServer.ContentGraph/Extensions/GraphQueryBuilderExtension.cs

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using EPiServer.ContentGraph.Api.Querying;
1+
using Azure.Core;
2+
using EPiServer.ContentGraph.Api.Querying;
3+
using EPiServer.ContentGraph.Tracing;
24

35
namespace EPiServer.ContentGraph.Extensions
46
{
@@ -29,7 +31,18 @@ public static TypeQueryBuilder<T> BeginType<T>(this GraphQueryBuilder queryBuild
2931
/// <returns></returns>
3032
public static GraphQueryBuilder SingleResultCache(this GraphQueryBuilder queryBuilder)
3133
{
32-
queryBuilder.RequestActions = request => request.AddRequestHeader("cache_uniq", "true");
34+
queryBuilder.GraphOptionsAction = options =>
35+
{
36+
if (options.QueryPath.Contains("cache=true"))
37+
{
38+
options.QueryPath += "&cache_uniq=true";
39+
}
40+
else
41+
{
42+
//log warning messge
43+
Trace.Instance.Add(new TraceEvent(queryBuilder, "cache_uniq is set to true but cache is not enable. Please enable it in the config before use."));
44+
}
45+
};
3346
return queryBuilder;
3447
}
3548
}

APIs/src/Testing/EPiServer.ContentGraph.IntegrationTests/TestSupport/IntegrationFixture.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private static void ConfigureServices(IServiceCollection services)
100100
//o.EnablePreviewFeatures = true;// optional
101101
});
102102

103-
services.AddContentGraphClient();
103+
services.AddContentGraphClient(options=> options.Cache = false);
104104
services.AddContentGraph();
105105
services.AddScoped<IFilterForVisitor, CustomForVisitor>();
106106
services.AddScoped<IFilterForVisitor, FilterPublishedForVisitor>();

0 commit comments

Comments
 (0)