Extract ConcurrentLruCache for reuse in NamedParameterJdbcTemplate
Closes gh-24197
This commit is contained in:
@@ -18,7 +18,6 @@ package org.springframework.jdbc.core.namedparam;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
@@ -45,6 +44,7 @@ import org.springframework.jdbc.support.KeyHolder;
|
||||
import org.springframework.jdbc.support.rowset.SqlRowSet;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ConcurrentLruCache;
|
||||
|
||||
/**
|
||||
* Template class with a basic set of JDBC operations, allowing the use
|
||||
@@ -76,17 +76,9 @@ public class NamedParameterJdbcTemplate implements NamedParameterJdbcOperations
|
||||
/** The JdbcTemplate we are wrapping. */
|
||||
private final JdbcOperations classicJdbcTemplate;
|
||||
|
||||
private volatile int cacheLimit = DEFAULT_CACHE_LIMIT;
|
||||
|
||||
/** Cache of original SQL String to ParsedSql representation. */
|
||||
@SuppressWarnings("serial")
|
||||
private final Map<String, ParsedSql> parsedSqlCache =
|
||||
new LinkedHashMap<String, ParsedSql>(DEFAULT_CACHE_LIMIT, 0.75f, true) {
|
||||
@Override
|
||||
protected boolean removeEldestEntry(Map.Entry<String, ParsedSql> eldest) {
|
||||
return size() > getCacheLimit();
|
||||
}
|
||||
};
|
||||
private volatile ConcurrentLruCache<String, ParsedSql> parsedSqlCache =
|
||||
new ConcurrentLruCache<>(DEFAULT_CACHE_LIMIT, NamedParameterUtils::parseSqlStatement);
|
||||
|
||||
|
||||
/**
|
||||
@@ -133,17 +125,17 @@ public class NamedParameterJdbcTemplate implements NamedParameterJdbcOperations
|
||||
|
||||
/**
|
||||
* Specify the maximum number of entries for this template's SQL cache.
|
||||
* Default is 256.
|
||||
* Default is 256. 0 indicates no caching, always parsing each statement.
|
||||
*/
|
||||
public void setCacheLimit(int cacheLimit) {
|
||||
this.cacheLimit = cacheLimit;
|
||||
this.parsedSqlCache = new ConcurrentLruCache<>(cacheLimit, NamedParameterUtils::parseSqlStatement);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the maximum number of entries for this template's SQL cache.
|
||||
*/
|
||||
public int getCacheLimit() {
|
||||
return this.cacheLimit;
|
||||
return this.parsedSqlCache.sizeLimit();
|
||||
}
|
||||
|
||||
|
||||
@@ -441,12 +433,7 @@ public class NamedParameterJdbcTemplate implements NamedParameterJdbcOperations
|
||||
* @return a representation of the parsed SQL statement
|
||||
*/
|
||||
protected ParsedSql getParsedSql(String sql) {
|
||||
if (getCacheLimit() <= 0) {
|
||||
return NamedParameterUtils.parseSqlStatement(sql);
|
||||
}
|
||||
synchronized (this.parsedSqlCache) {
|
||||
return this.parsedSqlCache.computeIfAbsent(sql, NamedParameterUtils::parseSqlStatement);
|
||||
}
|
||||
return this.parsedSqlCache.get(sql);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user