首页  

redis脚本实例     所属分类 redis 浏览量 2269
直接执行脚本
public Object eval(final String script, final List keys, final List args)

加载脚本
public String scriptLoad(final String script)

指定的脚本是否存在
public Boolean scriptExists(final String sha1)

执行脚本
public Object evalsha(final String sha1, final int keyCount, final String... params)

清除所有 Lua 脚本缓存
public String scriptFlush() 

redis.clients.jedis.exceptions.JedisNoScriptException: NOSCRIPT No matching script. Please use EVAL.

 

test.lua local key = KEYS[1] local id = redis.call('get',key) if(id == false) then redis.call('set',key,1) return key.."0001" else redis.call('set',key,id+1) return key..string.format('%04d',id + 1) end
ScriptTest.java import java.util.ArrayList; import java.util.List; import redis.clients.jedis.Jedis; public class ScriptTest { private static final String FILE = "/Users/dugang/work/redisdemo/script/test.lua"; public static void main(String[] args) throws Exception { System.out.println("hello,ScriptTest"); Jedis jedis = new Jedis("127.0.0.1"); System.out.println(jedis.info()); String key = "scriptTestKey"; String script = "return redis.call('get',KEYS[1])"; List<String> keys = new ArrayList<String>(); keys.add(key); List<String> params = new ArrayList<String>(); // 直接执行脚本 // params 不能为 Null ,会 空指针 Object result = jedis.eval(script,keys,params); System.out.println("eval.result="+result); // 读取脚本内容 script = new String(Utils.readBytes(FILE)); // 加载脚本到缓存 String sha = jedis.scriptLoad(script); // fac8c1886f2fe23364cce40d2cdb50a2cfea7cd9 System.out.println("sha ="+sha); // 指定脚本是否存在 System.out.println("exist="+jedis.scriptExists(sha)); // 再次加载脚本,得到同样的返回值 String sha2 = jedis.scriptLoad(script); System.out.println("sha2="+sha2); // Redis Script Flush 命令用于清除所有 Lua 脚本缓存。 // jedis.scriptFlush(); // redis.clients.jedis.exceptions.JedisNoScriptException: NOSCRIPT No matching script. Please use EVAL. // 执行脚本 result = jedis.evalsha(sha,1,key); System.out.println("evalsha.result="+result); String str = jedis.get(key); System.out.println("str="+str); jedis.close(); } } redis lua脚本支持不太友好,与aerospike有很大的差距 不支持嵌套数据结构 脚本管理功能弱 , 很容易出问题 ,集群重启了, 需要在所有节点上手工加载注册脚本 , 一个 scriptFlush命令 ,所有的脚本缓存就清了 ,得重新加载注册 完整代码 https://gitee.com/dyyx/redisdemo/blob/master/script/test.lua https://gitee.com/dyyx/redisdemo/blob/master/src/main/java/dyyx/ScriptTest.java

上一篇     下一篇
MapReduce

Hbase

zookeeper

redis pipeline 与 lua 比较

redis slow log

git恢复删除的文件