This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathGraphQLClient.cs
More file actions
157 lines (136 loc) · 4.92 KB
/
Copy pathGraphQLClient.cs
File metadata and controls
157 lines (136 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Runtime.Caching;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using GitHub.Extensions;
using Octokit.GraphQL;
using Octokit.GraphQL.Core;
namespace GitHub.Api
{
public class GraphQLClient : IGraphQLClient
{
public static readonly TimeSpan DefaultCacheDuration = TimeSpan.FromHours(8);
readonly IConnection connection;
readonly FileCache cache;
public GraphQLClient(
IConnection connection,
FileCache cache)
{
this.connection = connection;
this.cache = cache;
}
public Task ClearCache(string regionName)
{
// Switch to background thread because FileCache does not provide an async API.
return Task.Run(() => cache.ClearRegion(GetFullRegionName(regionName)));
}
public Task<T> Run<T>(
IQueryableValue<T> query,
Dictionary<string, object> variables = null,
bool refresh = false,
TimeSpan? cacheDuration = null,
string regionName = null,
CancellationToken cancellationToken = default)
{
return Run(query.Compile(), variables, refresh, cacheDuration, regionName, cancellationToken);
}
public Task<IEnumerable<T>> Run<T>(
IQueryableList<T> query,
Dictionary<string, object> variables = null,
bool refresh = false,
TimeSpan? cacheDuration = null,
string regionName = null,
CancellationToken cancellationToken = default)
{
return Run(query.Compile(), variables, refresh, cacheDuration, regionName, cancellationToken);
}
public async Task<T> Run<T>(
ICompiledQuery<T> query,
Dictionary<string, object> variables = null,
bool refresh = false,
TimeSpan? cacheDuration = null,
string regionName = null,
CancellationToken cancellationToken = default)
{
if (!query.IsMutation)
{
var wrapper = new CachingWrapper(
this,
refresh,
cacheDuration ?? DefaultCacheDuration,
GetFullRegionName(regionName));
return await wrapper.Run(query, variables, cancellationToken);
}
else
{
return await connection.Run(query, variables, cancellationToken);
}
}
string GetFullRegionName(string regionName)
{
var result = connection.Uri.Host;
if (!string.IsNullOrWhiteSpace(regionName))
{
result += Path.DirectorySeparatorChar + regionName;
}
return result.EnsureValidPath();
}
static string GetHash(string input)
{
var sb = new StringBuilder();
using (var hash = SHA256.Create())
{
var result = hash.ComputeHash(Encoding.UTF8.GetBytes(input));
foreach (var b in result)
{
sb.Append(b.ToString("x2", CultureInfo.InvariantCulture));
}
}
return sb.ToString();
}
class CachingWrapper : IConnection
{
readonly GraphQLClient owner;
readonly bool refresh;
readonly TimeSpan cacheDuration;
readonly string regionName;
public CachingWrapper(
GraphQLClient owner,
bool refresh,
TimeSpan cacheDuration,
string regionName)
{
this.owner = owner;
this.refresh = refresh;
this.cacheDuration = cacheDuration;
this.regionName = regionName;
}
public Uri Uri => owner.connection.Uri;
public Task<string> Run(string query, CancellationToken cancellationToken = default)
{
// Switch to background thread because FileCache does not provide an async API.
return Task.Run(async () =>
{
var hash = GetHash(query);
if (refresh)
{
owner.cache.Remove(hash, regionName);
}
var data = (string) owner.cache.Get(hash, regionName);
if (data != null)
{
return data;
}
var result = await owner.connection.Run(query, cancellationToken);
owner.cache.Add(hash, result, DateTimeOffset.Now + cacheDuration, regionName);
return result;
}, cancellationToken);
}
}
}
}