Skip to content

Commit

Permalink
Revise PartTree query caching.
Browse files Browse the repository at this point in the history
See #3588
Original pull request: #3653
  • Loading branch information
mp911de committed Nov 29, 2024
1 parent d254681 commit 8a5e092
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ default Select instantiate(Class<?> resultType, Collection<JpqlQueryBuilder.Path
/**
* Select a single attribute.
*
* @param name
* @param path
* @return
*/
default Select select(JpqlQueryBuilder.PathExpression path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,8 @@ private Query restrictMaxResultsIfNecessary(Query query, @Nullable ScrollPositio

protected JpqlQueryCreator createCreator(Sort sort, JpaParametersParameterAccessor accessor) {

JpqlQueryCreator jpqlQueryCreator;
synchronized (cache) {
jpqlQueryCreator = cache.get(sort, accessor); // this caching thingy is broken due to IS NULL rendering for
// simple properties
}

JpqlQueryCreator jpqlQueryCreator = cache.get(sort, accessor); // this caching thingy is broken due to IS NULL
// rendering for
if (jpqlQueryCreator != null) {
return jpqlQueryCreator;
}
Expand All @@ -307,9 +303,7 @@ protected JpqlQueryCreator createCreator(Sort sort, JpaParametersParameterAccess
return creator;
}

synchronized (cache) {
cache.put(sort, accessor, creator);
}
cache.put(sort, accessor, creator);

return creator;
}
Expand Down Expand Up @@ -377,13 +371,12 @@ private Sort getDynamicSort(JpaParametersParameterAccessor accessor) {
*/
private class CountQueryPreparer extends QueryPreparer {

private volatile JpqlQueryCreator cached;
private final PartTreeQueryCache cache = new PartTreeQueryCache();

@Override
protected JpqlQueryCreator createCreator(Sort sort, JpaParametersParameterAccessor accessor) {

JpqlQueryCreator cached = this.cached;

JpqlQueryCreator cached = cache.get(Sort.unsorted(), accessor);
if (cached != null) {
return cached;
}
Expand All @@ -393,7 +386,9 @@ protected JpqlQueryCreator createCreator(Sort sort, JpaParametersParameterAccess
getQueryMethod().getResultProcessor().getReturnedType(), provider, templates, em);

if (!accessor.getParameters().hasDynamicProjection()) {
return this.cached = new CacheableJpqlCountQueryCreator(creator);
cached = new CacheableJpqlCountQueryCreator(creator);
cache.put(Sort.unsorted(), accessor, cached);
return cached;
}

return creator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.springframework.data.jpa.repository.query;

import java.util.BitSet;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
Expand All @@ -31,12 +32,12 @@
*/
class PartTreeQueryCache {

private final Map<CacheKey, JpqlQueryCreator> cache = new LinkedHashMap<>() {
private final Map<CacheKey, JpqlQueryCreator> cache = Collections.synchronizedMap(new LinkedHashMap<>() {
@Override
protected boolean removeEldestEntry(Map.Entry<CacheKey, JpqlQueryCreator> eldest) {
return size() > 256;
}
};
});

@Nullable
JpqlQueryCreator get(Sort sort, JpaParametersParameterAccessor accessor) {
Expand Down

0 comments on commit 8a5e092

Please sign in to comment.