@ -18,7 +18,6 @@ package org.thingsboard.server.cache;
import jakarta.annotation.PostConstruct ;
import lombok.extern.slf4j.Slf4j ;
import org.apache.commons.lang3.NotImplementedException ;
import org.springframework.dao.InvalidDataAccessApiUsageException ;
import org.springframework.data.redis.connection.RedisConnection ;
import org.springframework.data.redis.connection.RedisConnectionFactory ;
import org.springframework.data.redis.connection.ReturnType ;
@ -27,7 +26,6 @@ import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.thingsboard.server.common.data.HasVersion ;
import java.io.Serializable ;
import java.util.Arrays ;
@Slf4j
public abstract class VersionedRedisTbCache < K extends VersionedCacheKey , V extends Serializable & HasVersion > extends RedisTbTransactionalCache < K , V > implements VersionedTbCache < K , V > {
@ -67,15 +65,7 @@ public abstract class VersionedRedisTbCache<K extends VersionedCacheKey, V exten
@PostConstruct
public void init ( ) {
try ( var connection = getConnection ( SET_VERSIONED_VALUE_SHA ) ) {
log . debug ( "Loading LUA with expected SHA[{}], connection [{}]" , new String ( SET_VERSIONED_VALUE_SHA ) , connection . getNativeConnection ( ) ) ;
String sha = connection . scriptingCommands ( ) . scriptLoad ( SET_VERSIONED_VALUE_LUA_SCRIPT ) ;
if ( ! Arrays . equals ( SET_VERSIONED_VALUE_SHA , StringRedisSerializer . UTF_8 . serialize ( sha ) ) ) {
log . error ( "SHA for SET_VERSIONED_VALUE_LUA_SCRIPT wrong! Expected [{}], but actual [{}], connection [{}]" , new String ( SET_VERSIONED_VALUE_SHA ) , sha , connection . getNativeConnection ( ) ) ;
}
} catch ( Throwable t ) {
log . error ( "Error on Redis versioned cache init" , t ) ;
}
loadLuaScript ( SET_VERSIONED_VALUE_SHA , SET_VERSIONED_VALUE_LUA_SCRIPT ) ;
}
@Override
@ -129,21 +119,7 @@ public abstract class VersionedRedisTbCache<K extends VersionedCacheKey, V exten
byte [ ] rawValue = getRawValue ( value ) ;
byte [ ] rawVersion = StringRedisSerializer . UTF_8 . serialize ( String . valueOf ( version ) ) ;
byte [ ] rawExpiration = StringRedisSerializer . UTF_8 . serialize ( String . valueOf ( expiration . getExpirationTimeInSeconds ( ) ) ) ;
try {
connection . scriptingCommands ( ) . evalSha ( SET_VERSIONED_VALUE_SHA , ReturnType . VALUE , 1 , rawKey , rawValue , rawVersion , rawExpiration ) ;
} catch ( InvalidDataAccessApiUsageException e ) {
log . debug ( "loading LUA [{}]" , connection . getNativeConnection ( ) ) ;
String sha = connection . scriptingCommands ( ) . scriptLoad ( SET_VERSIONED_VALUE_LUA_SCRIPT ) ;
if ( ! Arrays . equals ( SET_VERSIONED_VALUE_SHA , StringRedisSerializer . UTF_8 . serialize ( sha ) ) ) {
log . error ( "SHA for SET_VERSIONED_VALUE_LUA_SCRIPT wrong! Expected [{}], but actual [{}]" , new String ( SET_VERSIONED_VALUE_SHA ) , sha ) ;
}
try {
connection . scriptingCommands ( ) . evalSha ( SET_VERSIONED_VALUE_SHA , ReturnType . VALUE , 1 , rawKey , rawValue , rawVersion , rawExpiration ) ;
} catch ( InvalidDataAccessApiUsageException ignored ) {
log . debug ( "Slowly executing eval instead of fast evalsha" ) ;
connection . scriptingCommands ( ) . eval ( SET_VERSIONED_VALUE_LUA_SCRIPT , ReturnType . VALUE , 1 , rawKey , rawValue , rawVersion , rawExpiration ) ;
}
}
executeScript ( connection , SET_VERSIONED_VALUE_SHA , SET_VERSIONED_VALUE_LUA_SCRIPT , ReturnType . VALUE , 1 , rawKey , rawValue , rawVersion , rawExpiration ) ;
}
@Override