Don't remove an element from the cache that was queued to be created during the current request

This avoids a remove Infinispan call in multi-node and cross-DC setups.

Closes #20404
This commit is contained in:
Alexander Schwartz 2023-05-09 14:13:06 +02:00 committed by Michal Hajas
parent fc0e47caa4
commit 5cd0d51fa6

View file

@ -95,6 +95,11 @@ public class InfinispanKeycloakTransaction implements KeycloakTransaction {
public String toString() { public String toString() {
return String.format("CacheTaskWithValue: Operation 'put' for key %s", key); return String.format("CacheTaskWithValue: Operation 'put' for key %s", key);
} }
@Override
public Operation getOperation() {
return Operation.PUT;
}
}); });
} }
} }
@ -116,6 +121,11 @@ public class InfinispanKeycloakTransaction implements KeycloakTransaction {
public String toString() { public String toString() {
return String.format("CacheTaskWithValue: Operation 'put' for key %s, lifespan %d TimeUnit %s", key, lifespan, lifespanUnit); return String.format("CacheTaskWithValue: Operation 'put' for key %s, lifespan %d TimeUnit %s", key, lifespan, lifespanUnit);
} }
@Override
public Operation getOperation() {
return Operation.PUT;
}
}); });
} }
} }
@ -140,6 +150,11 @@ public class InfinispanKeycloakTransaction implements KeycloakTransaction {
public String toString() { public String toString() {
return String.format("CacheTaskWithValue: Operation 'putIfAbsent' for key %s", key); return String.format("CacheTaskWithValue: Operation 'putIfAbsent' for key %s", key);
} }
@Override
public Operation getOperation() {
return Operation.PUT;
}
}); });
} }
} }
@ -186,7 +201,14 @@ public class InfinispanKeycloakTransaction implements KeycloakTransaction {
Object taskKey = getTaskKey(cache, key); Object taskKey = getTaskKey(cache, key);
// TODO:performance Eventual performance optimization could be to skip "cache.remove" if item was added in this transaction (EG. authenticationSession valid for single request due to automatic SSO login) CacheTask current = tasks.get(taskKey);
if (current != null) {
if (current instanceof CacheTaskWithValue && ((CacheTaskWithValue<?>) current).getOperation() == Operation.PUT) {
tasks.remove(taskKey);
return;
}
}
tasks.put(taskKey, new CacheTask() { tasks.put(taskKey, new CacheTask() {
@Override @Override
@ -230,7 +252,9 @@ public class InfinispanKeycloakTransaction implements KeycloakTransaction {
void execute(); void execute();
} }
public abstract class CacheTaskWithValue<V> implements CacheTask { public enum Operation { PUT, OTHER }
public static abstract class CacheTaskWithValue<V> implements CacheTask {
protected V value; protected V value;
public CacheTaskWithValue(V value) { public CacheTaskWithValue(V value) {
@ -244,6 +268,10 @@ public class InfinispanKeycloakTransaction implements KeycloakTransaction {
public void setValue(V value) { public void setValue(V value) {
this.value = value; this.value = value;
} }
public Operation getOperation() {
return Operation.OTHER;
}
} }
// Ignore return values. Should have better performance within cluster / cross-dc env // Ignore return values. Should have better performance within cluster / cross-dc env