Skip to content

Commit 5aff414

Browse files
Update the cache key generation logic
1 parent 8b3aeee commit 5aff414

16 files changed

+241
-5
lines changed

ballerina-tests/tests/44_server_caches.bal

+24
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,27 @@ function dataProviderServerSideCacheWithDynamicResponse() returns map<[string, s
248248
};
249249
return dataSet;
250250
}
251+
252+
@test:Config {
253+
groups: ["server_cache"],
254+
dataProvider: dataProviderServerCacheWithListInput
255+
}
256+
isolated function testServerCacheWithListInput(string documentFile, string[] resourceFileNames, json variables = (), string[] operationNames = []) returns error? {
257+
string url = "http://localhost:9091/cache_with_list_input";
258+
string document = check getGraphqlDocumentFromFile("server_cache_with_list_input");
259+
260+
foreach int i in 0..< resourceFileNames.length() {
261+
json actualPayload = check getJsonPayloadFromService(url, document, variables, operationNames[i]);
262+
json expectedPayload = check getJsonContentFromFile(resourceFileNames[i]);
263+
assertJsonValuesWithOrder(actualPayload, expectedPayload);
264+
}
265+
}
266+
267+
function dataProviderServerCacheWithListInput() returns map<[string, string[], json, string[]]> {
268+
map<[string, string[], json, string[]]> dataSet = {
269+
"1": ["server_cache_with_list_input", ["server_cache_with_list_input_1", "server_cache_with_list_input_2", "server_cache_with_list_input_3", "server_cache_with_list_input_1"], (), ["A", "B", "G", "A"]],
270+
"2": ["server_cache_with_list_input", ["server_cache_with_list_input_4", "server_cache_with_list_input_5", "server_cache_with_list_input_6", "server_cache_with_list_input_4"], (), ["D", "H", "E", "D"]],
271+
"3": ["server_cache_with_list_input", ["server_cache_with_list_input_7", "server_cache_with_list_input_8", "server_cache_with_list_input_7"], (), ["F", "I", "F"]]
272+
};
273+
return dataSet;
274+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
query A {
2+
users1(ids: [1]) {
3+
id
4+
name
5+
age
6+
}
7+
}
8+
9+
query B {
10+
users1(ids: [2]) {
11+
id
12+
name
13+
age
14+
}
15+
}
16+
17+
query C {
18+
users1(ids: [3]) {
19+
id
20+
name
21+
age
22+
}
23+
}
24+
25+
query D {
26+
users2(ids: [[1], [3], [2]]) {
27+
id
28+
name
29+
age
30+
}
31+
}
32+
33+
query E {
34+
users2(ids: [[1], [2], [3]]) {
35+
id
36+
name
37+
age
38+
}
39+
}
40+
41+
query F {
42+
cities(addresses: [{number: "1", street: "Main St", city: "London"}])
43+
}
44+
45+
mutation G {
46+
updateUser(id: 1, name: "Shane", age: 30) {
47+
id
48+
name
49+
age
50+
}
51+
}
52+
53+
mutation H {
54+
updateUser(id: 1, name: "Rock", age: 22) {
55+
id
56+
name
57+
age
58+
}
59+
}
60+
61+
query I {
62+
cities(addresses: [{number: "2", street: "Main St", city: "london"}])
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"data": {
3+
"users1": [
4+
{
5+
"id": 1,
6+
"name": "John",
7+
"age": 25
8+
}
9+
]
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"data": {
3+
"users1": [
4+
{
5+
"id": 2,
6+
"name": "Jessie",
7+
"age": 17
8+
}
9+
]
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"data": {
3+
"updateUser": {
4+
"id": 1,
5+
"name": "Shane",
6+
"age": 30
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"data": {
3+
"users2": [
4+
{
5+
"id": 1,
6+
"name": "Shane",
7+
"age": 30
8+
}
9+
]
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"data": {
3+
"updateUser": {
4+
"id": 1,
5+
"name": "Rock",
6+
"age": 22
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"data": {
3+
"users2": [
4+
{
5+
"id": 1,
6+
"name": "Rock",
7+
"age": 22
8+
}
9+
]
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"data": {
3+
"cities": ["London"]
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"data": {
3+
"cities": ["London", "london"]
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"data": {
2+
"data": {
33
"status": ["dead", "alive"]
44
}
55
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"data": {
3+
"status": ["alive", "alive"]
4+
}
5+
}

ballerina-tests/tests/test_services.bal

+44
Original file line numberDiff line numberDiff line change
@@ -3000,3 +3000,47 @@ service /dynamic_response on basicListener {
30003000
return self.user;
30013001
}
30023002
}
3003+
3004+
@graphql:ServiceConfig {
3005+
cacheConfig: {
3006+
maxAge: 600
3007+
}
3008+
}
3009+
service /cache_with_list_input on basicListener {
3010+
private User user1 = {id: 1, name: "John", age: 25};
3011+
private User user2 = {id: 2, name: "Jessie", age: 17};
3012+
private Address[] addresses = [];
3013+
3014+
resource function get users1(int[] ids) returns User[] {
3015+
if ids.length() == 1 && ids[0] == 1 {
3016+
return [self.user1];
3017+
} else if ids.length() == 1 && ids[0] == 2 {
3018+
return [self.user2];
3019+
}
3020+
return [self.user1, self.user2];
3021+
}
3022+
3023+
resource function get users2(int[][] ids) returns User[] {
3024+
if ids.length() == 3 && ids[0][0] == 1 {
3025+
return [self.user1];
3026+
} else if ids.length() == 3 && ids[0][0] == 2 || ids[0][0] == 3 {
3027+
return [self.user2];
3028+
}
3029+
return [self.user1, self.user2];
3030+
}
3031+
3032+
resource function get cities(Address[] addresses) returns string[] {
3033+
self.addresses.push(...addresses);
3034+
return self.addresses.map(address => address.city);
3035+
}
3036+
3037+
remote function updateUser(int id, string name, int age) returns User|error {
3038+
self.user1 = {id:id, name:name, age:age};
3039+
return self.user1;
3040+
}
3041+
3042+
remote function updateAddress(Address address) returns Address {
3043+
self.addresses.push(address);
3044+
return address;
3045+
}
3046+
}

ballerina/common_utils.bal

+28-1
Original file line numberDiff line numberDiff line change
@@ -519,11 +519,38 @@ public isolated function __addError(Context context, ErrorDetail errorDetail) {
519519
isolated function generateArgHash(parser:ArgumentNode[] arguments, string[] parentArgHashes = [],
520520
string[] optionalFields = []) returns string {
521521
any[] argValues = [...parentArgHashes, ...optionalFields];
522-
argValues.push(...arguments.'map((arg) => arg.getValue()));
522+
argValues.push(...arguments.'map((arg) => getValueArrayFromArgumentNode(arg)));
523523
byte[] hash = crypto:hashMd5(argValues.toString().toBytes());
524524
return hash.toBase64();
525525
}
526526

527+
isolated function getValueArrayFromArgumentNode(parser:ArgumentNode argumentNode) returns anydata[] {
528+
anydata[] valueArray = [];
529+
if argumentNode.isVariableDefinition() {
530+
valueArray.push(argumentNode.getVariableValue());
531+
} else {
532+
parser:ArgumentValue|parser:ArgumentValue[] argValue = argumentNode.getValue();
533+
if argValue is parser:ArgumentValue[] {
534+
foreach parser:ArgumentValue argField in argValue {
535+
parser:ArgumentNode|Scalar? argFieldNode = argField;
536+
if argFieldNode is parser:ArgumentNode {
537+
valueArray.push(getValueArrayFromArgumentNode(argFieldNode));
538+
} else {
539+
valueArray.push(argFieldNode);
540+
}
541+
}
542+
} else {
543+
parser:ArgumentNode|Scalar? argFieldNode = argValue;
544+
if argFieldNode is parser:ArgumentNode {
545+
valueArray.push(getValueArrayFromArgumentNode(argFieldNode));
546+
} else {
547+
valueArray.push(argFieldNode);
548+
}
549+
}
550+
}
551+
return valueArray;
552+
}
553+
527554
isolated function getNullableFieldsFromType(__Type fieldType) returns string[] {
528555
string[] nullableFields = [];
529556
__Field[]? fields = unwrapNonNullype(fieldType).fields;

ballerina/response_generator.bal

+2-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ class ResponseGenerator {
153153
__Type fieldType = getFieldTypeFromParentType(self.fieldType, self.engine.getSchema().types, fieldNode);
154154
(string|int)[] clonedPath = self.path.clone();
155155
clonedPath.push(fieldNode.getAlias());
156-
Field 'field = new (fieldNode, fieldType, path = clonedPath, fieldValue = fieldValue);
156+
Field 'field = new (fieldNode, fieldType, path = clonedPath, fieldValue = fieldValue,
157+
cacheConfig = self.cacheConfig, parentArgHashes = self.parentArgHashes);
157158
self.context.resetInterceptorCount();
158159
return self.engine.resolve(self.context, 'field);
159160
}

ballerina/tests/09_cache_utils.bal

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function testCacheUtils() returns error? {
2626
Field 'field = getField(fields[0], Person, ["person"], {maxAge: 10});
2727
test:assertTrue('field.isCacheEnabled());
2828
test:assertEquals('field.getCacheMaxAge(), 10d);
29-
test:assertEquals('field.getCacheKey(), "person.TYoV48w1dQ8BbOFaQ5N2IA==");
29+
test:assertEquals('field.getCacheKey(), "person.Jq9sXPlesvC6Q7cU5RPkZA==");
3030
}
3131

3232
@test:Config {
@@ -38,7 +38,7 @@ function testCacheConfigInferring() returns error? {
3838
Field 'field = getField(fields[0], Person, ["person"], {maxAge: 10});
3939
test:assertTrue('field.getSubfields() is Field[]);
4040
Field[] subfields = <Field[]>'field.getSubfields();
41-
string[] expectedCacheKey = ["person.name.11FxOYiYfpMxmANj4kGJzg==", "person.address.t1JP49rdVkuozxi8sSh+bg=="];
41+
string[] expectedCacheKey = ["person.name.11FxOYiYfpMxmANj4kGJzg==", "person.address.nj4v+q6cUjv3W/MbZdNQXg=="];
4242
foreach int i in 0..<subfields.length() {
4343
test:assertTrue(subfields[i].isCacheEnabled());
4444
test:assertEquals(subfields[i].getCacheMaxAge(), 10d);

0 commit comments

Comments
 (0)