【问题标题】:Is it possible to use EVAL SHA with spring-data redis?是否可以将 EVAL SHA 与 spring-data redis 一起使用?
【发布时间】:2014-07-04 19:08:06
【问题描述】:
【问题讨论】:
标签:
java
redis
spring-data-redis
【解决方案1】:
默认的 ScriptExecutor 通过检索
脚本的 SHA1 并尝试首先运行 evalsha,后退
如果脚本尚未出现在 Redis 脚本缓存中,则进行评估。
see spring-data redis docs
情况是“当我们有主/从配置时,即使我在两台服务器上都安装了相同的脚本并使用 evalsha 调用主服务器,主服务器每次都会使用 eval 命令将整个脚本转发给从服务器”。
see this thread
【解决方案2】:
也许你可以这样做:
final String scriptString1= script1.getScriptAsString();
redisTemplate.execute(new RedisCallback<String>(){
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
DefaultStringRedisConnection newConnection = new DefaultStringRedisConnection(connection);
return newConnection.scriptLoad(scriptString1);
}
});
DefaultStringRedisConnection 具有“scriptLoad”方法,允许您加载脚本。