Skip to content

cache interpolation results #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ interface Message {
String template();

Map<String, Object> attributes();

String lookupkey();
}

/**
Expand Down
19 changes: 8 additions & 11 deletions validator/src/main/java/io/avaje/validation/core/DMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,20 @@

import java.util.Map;

final class DMessage implements ValidationContext.Message {
final record DMessage(String template, Map<String, Object> attributes, int dedupNumber)
implements ValidationContext.Message {

private final String template;
private final Map<String, Object> attributes;
// templates can be the same across multiple adapters
// these numbers ensures no cache collision
private static int messageCounter = 0;

DMessage(String template, Map<String, Object> attributes) {
this.template = template;
this.attributes = attributes;
}

@Override
public String template() {
return template;
this(template, attributes, messageCounter++);
}

@Override
public Map<String, Object> attributes() {
return attributes;
public String lookupkey() {
return template + dedupNumber;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package io.avaje.validation.core;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

final class DTemplateLookup {
private final DResourceBundleManager bundleManager;
private final Map<String, String> messageCache = new HashMap<>();

DTemplateLookup(DResourceBundleManager defaultBundle) {
this.bundleManager = defaultBundle;
Expand All @@ -17,7 +14,7 @@ String lookup(String template, Locale resolvedLocale) {
return template;
}
final String key = template.substring(1, template.length() - 1);
final String msg = messageCache.computeIfAbsent(key + resolvedLocale, k -> bundleManager.message(key, resolvedLocale));
final String msg = bundleManager.message(key, resolvedLocale);
if (msg != null) {
return msg;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package io.avaje.validation.core;

import io.avaje.validation.adapter.ValidationAdapter;
import io.avaje.validation.adapter.ValidationRequest;

import java.lang.reflect.Type;
import java.util.Locale;

import io.avaje.validation.adapter.ValidationAdapter;

final class DValidationType<T> implements ValidationType<T> {

private final DValidator validator;
Expand Down
14 changes: 10 additions & 4 deletions validator/src/main/java/io/avaje/validation/core/DValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand All @@ -18,7 +19,6 @@

import io.avaje.lang.Nullable;
import io.avaje.validation.Validator;
import io.avaje.validation.Validator.AnnotationAdapterBuilder;
import io.avaje.validation.adapter.ValidationAdapter;
import io.avaje.validation.adapter.ValidationContext;
import io.avaje.validation.adapter.ValidationRequest;
Expand All @@ -32,6 +32,7 @@ final class DValidator implements Validator, ValidationContext {
private final MessageInterpolator interpolator;
private final LocaleResolver localeResolver;
private final DTemplateLookup templateLookup;
private final Map<String, String> messageCache = new ConcurrentHashMap<>();

DValidator(
List<AdapterFactory> factories,
Expand Down Expand Up @@ -118,9 +119,14 @@ ValidationRequest request(@Nullable Locale locale) {
String interpolate(Message msg, Locale requestLocale) {
// resolve the locale to use to produce the message
final Locale locale = localeResolver.resolve(requestLocale);
// lookup in resource bundles using resolved locale and template
final String template = templateLookup.lookup(msg.template(), locale);
return interpolator.interpolate(template, msg.attributes());

return messageCache.computeIfAbsent(
msg.lookupkey() + locale,
k -> {
// lookup in resource bundles using resolved locale and template
final String template = templateLookup.lookup(msg.template(), locale);
return interpolator.interpolate(template, msg.attributes());
});
}

/** Implementation of Validator.Builder. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

abstract class BasicTest {

protected final Validator validator =
protected static final Validator validator =
Validator.builder()
.add(Address.class, AddressValidationAdapter::new)
.add(Contact.class, ContactValidationAdapter::new)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,22 @@ class NotNullLocaleTest extends BasicTest {

@Test
void testSize_DefaultLocale() {
var contact = new Contact("ok", null);
ConstraintViolation constraint = one(contact, Locale.ENGLISH);
final var contact = new Contact("ok", null);
final ConstraintViolation constraint = one(contact, Locale.ENGLISH);
assertThat(constraint.message()).isEqualTo("must not be null");
}

@Test
void testSize_DefaultLocale2() {
final var contact = new Contact("ok", null);
final ConstraintViolation constraint = one(contact, Locale.ENGLISH);
assertThat(constraint.message()).isEqualTo("must not be null");
}

@Test
void testSize_DE() {
var contact = new Contact("ok", null);
ConstraintViolation constraint = one(contact, Locale.GERMAN);
final var contact = new Contact("ok", null);
final ConstraintViolation constraint = one(contact, Locale.GERMAN);
assertThat(constraint.message()).isEqualTo("darf nicht null sein");
}

Expand Down