RedisClient redisClient = RedisClient.create("redis://localhost:6379/0");
StatefulRedisConnection<String, String> connection = redisClient.connect();
RedisCommands<String, String> syncCommands = connection.sync();
String loaded = syncCommands.scriptLoad("return 1+2");
Object result = syncCommands.evalsha(loaded, ScriptOutputType.INTEGER, new String[0], "");
evalsha
方法对应redis的EVALSHA
命令,根据给定的 sha1 校验码,执行缓存在服务器中的脚本
将脚本缓存到服务器的操作可以通过scriptLoad
方法进行,对应redis的SCRIPT LOAD
命令
RedisCodec
时的问题Lettuce的connection对象是和RedisCodec
挂钩的,默认使用的是StringCodec.UTF8
,适用于key和value都是String的情况
当使用自定义的RedisCodec
时,比如SerializedObjectCodec
,在scriptLoadt时,也会调用自定义的RedisCodec
,报错ERR Error compiling script (new function): user_script:1: unexpected symbol near 'ᆲ'
根据#1090,有两种解决办法:
第二种方法没有实践成功,故使用了第一种方法
根据#10106.0版本后会修复这个问题,让scriptLoad
方法默认使用StringCodec