Refactor executeScript to not load script on init, but on execute

This commit is contained in:
Andrii Landiak 2025-01-20 17:44:09 +02:00
parent f4372feff9
commit 5d07beb0de
2 changed files with 7 additions and 20 deletions

View File

@ -271,24 +271,17 @@ public abstract class RedisTbTransactionalCache<K extends Serializable, V extend
connection.stringCommands().set(rawKey, rawValue, this.cacheTtl, setOption);
}
protected void loadLuaScript(byte[] expectedSha, byte[] luaScript) {
try (var connection = getConnection(expectedSha)) {
log.debug("Loading LUA with expected SHA [{}], connection [{}]", new String(expectedSha), connection.getNativeConnection());
String actualSha = connection.scriptingCommands().scriptLoad(luaScript);
if (!Arrays.equals(expectedSha, StringRedisSerializer.UTF_8.serialize(actualSha))) {
String message = String.format("SHA for LUA script wrong! Expected [%s], but actual [%s], connection [%s]",
new String(expectedSha), actualSha, connection.getNativeConnection());
throw new IllegalStateException(message);
}
}
}
protected void executeScript(RedisConnection connection, byte[] scriptSha, byte[] luaScript, ReturnType returnType, int numKeys, byte[]... keysAndArgs) {
try {
connection.scriptingCommands().evalSha(scriptSha, returnType, numKeys, keysAndArgs);
} catch (InvalidDataAccessApiUsageException ignored) {
log.debug("Loading LUA [{}]", connection.getNativeConnection());
connection.scriptingCommands().scriptLoad(luaScript);
log.debug("Loading LUA with expected SHA [{}], connection [{}]", new String(scriptSha), connection.getNativeConnection());
String actualSha = connection.scriptingCommands().scriptLoad(luaScript);
if (!Arrays.equals(scriptSha, StringRedisSerializer.UTF_8.serialize(actualSha))) {
String message = String.format("SHA for LUA script wrong! Expected [%s], but actual [%s], connection [%s]",
new String(scriptSha), actualSha, connection.getNativeConnection());
throw new IllegalStateException(message);
}
try {
connection.scriptingCommands().evalSha(scriptSha, returnType, numKeys, keysAndArgs);
} catch (InvalidDataAccessApiUsageException exception) {

View File

@ -15,7 +15,6 @@
*/
package org.thingsboard.server.cache;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.NotImplementedException;
import org.springframework.data.redis.connection.RedisConnection;
@ -63,11 +62,6 @@ public abstract class VersionedRedisTbCache<K extends VersionedCacheKey, V exten
super(cacheName, cacheSpecsMap, connectionFactory, configuration, valueSerializer);
}
@PostConstruct
public void init() {
loadLuaScript(SET_VERSIONED_VALUE_SHA, SET_VERSIONED_VALUE_LUA_SCRIPT);
}
@Override
protected byte[] doGet(K key, RedisConnection connection) {
if (!key.isVersioned()) {