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