【问题标题】:Is it possible to use EVAL SHA with spring-data redis?是否可以将 EVAL SHA 与 spring-data redis 一起使用?
【发布时间】:2014-07-04 19:08:06
【问题描述】:

是否可以将Redis 命令EVAL SHA 命令与Spring-Data Redis 一起使用?

我们成功使用EVAL 执行命令:redisTemplate.execute(script, null, args);, 但是每次将脚本传输到 Redis 服务器似乎会产生很大的开销。

是否可以使用 Spring-Data Redis 存储一次脚本并根据其 SHA 运行它?

【问题讨论】:

    标签: 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”方法,允许您加载脚本。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-10
        • 2011-07-25
        • 2021-11-23
        • 2013-04-07
        • 2021-09-13
        • 1970-01-01
        相关资源
        最近更新 更多