最代码官方的gravatar头像
最代码官方 2014-09-26 15:40:59

spring+springdata redis入门教程实现java端调用redis lua脚本片段和lua脚本文件

maven项目截图:

spring+springdata redis入门教程实现java端调用redis lua脚本片段和lua脚本文件

最简单的入门级实例,演示如何通过spring,springdata redis实现java调用redis的脚本和文件。

在redis server端执行如下命令:

redis-cli -h localhost -p 6379 SCRIPT LOAD "return 'zuidaima.com'"

输出得到sha值:

>052f2e2b180e2df03b33a747ba35af3f497ee526

java端调用代码示例:

@Test
public void evalScriptSnippet() {
	// >redis-cli -h localhost -p 6379 SCRIPT LOAD "return 'zuidaima.com'"
	// >>052f2e2b180e2df03b33a747ba35af3f497ee526
	final String sha = "052f2e2b180e2df03b33a747ba35af3f497ee526";
	Object ret = redisTemplate.execute(new RedisCallback<Object>() {
		public Object doInRedis(RedisConnection connection) {
			Jedis jedis = (Jedis) connection.getNativeConnection();
			return jedis.evalsha(sha, 0);
		}
	}, true);
	System.out.println(ret);
}

执行后输出结果如下:

>zuidaima.com

和在redis server上执行如下命令的效果类似:

redis-cli -h localhost -p 6379 EVALSHA 052f2e2b180e2df03b33a747ba35af3f497ee526 0

spring+springdata redis入门教程实现java端调用redis lua脚本片段和lua脚本文件

执行lua文件的流程类似,请下载代码后根据项目的需要做扩展吧。


打赏

文件名:zuidaima_redis-test.rar,文件大小:3.785K 下载
  • /
      • /zuidaima_redis-test
        • /zuidaima_redis-test/.classpath
        • /zuidaima_redis-test/.project
        • /zuidaima_redis-test/pom.xml
          • /zuidaima_redis-test/src
              • /zuidaima_redis-test/src/main
                  • /zuidaima_redis-test/src/main/resources
                    • /zuidaima_redis-test/src/main/resources/applicationContext.xml
                    • /zuidaima_redis-test/src/main/resources/test.properties
最代码最近下载分享源代码列表最近下载
安布卢飞  LV2 2019年11月20日
spuipe  LV3 2018年9月20日
shenzhenshenchuang  LV1 2018年5月16日
liuyubohao  LV1 2018年3月24日
springBoot-demo  LV1 2018年3月12日
rongchuan  LV1 2018年3月5日
1410359394  LV1 2018年2月26日
zwxw923  LV1 2017年11月22日
lll111  LV16 2017年10月13日
pszhangmingwei  LV2 2017年9月19日
最代码最近浏览分享源代码列表最近浏览
flygrass  LV12 2023年12月5日
周粥周  LV3 2022年10月12日
youwuzuichen  LV10 2022年5月11日
tianli3000  LV7 2021年7月17日
sangyy  LV10 2021年4月23日
泪染珍珠  LV9 2020年9月8日
昵称zzz  LV13 2020年7月17日
qwqzbl  LV6 2020年6月4日
2627947842 2020年5月25日
暂无贡献等级
364550246  LV15 2020年4月22日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友