首页>代码>spring+springdata redis入门教程实现java端调用redis lua脚本片段和lua脚本文件>/zuidaima_redis-test/src/main/java/com/zuidaima/redis/test/RedicTest.java
package com.zuidaima.redis.test;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

import redis.clients.jedis.Jedis;

@ContextConfiguration(value = { "classpath:applicationContext.xml" })
public class RedicTest extends AbstractJUnit4SpringContextTests {

	@Autowired
	public RedisTemplate<String, String> redisTemplate;

	public ValueOperations<String, String> valueOps;

	@Before
	public void before() {
		valueOps = redisTemplate.opsForValue();
	}

	@Test
	public void evalScriptSnippet() {
		// >redis-cli -h localhost -p 6379 SCRIPT LOAD "return 'zuidaima.com'"
		// >>052f2e2b180e2df03b33a747ba35af3f497ee526
		// >redis-cli -h localhost -p 6379 EVALSHA 052f2e2b180e2df03b33a747ba35af3f497ee526 0
		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);
	}

	@Test
	public void evalScriptFile() {
		// >redis-cli -h localhost -p 6379 SCRIPT LOAD "$(cat /root/zuidaima_com.lua)"
		// >>09e924a67ea78ceec2c1d64e21090587d172b406
		// >redis-cli -h localhost -p 6379 EVALSHA 09e924a67ea78ceec2c1d64e21090587d172b406 0
		final String sha = "09e924a67ea78ceec2c1d64e21090587d172b406";
		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);
	}

	@After
	public void destory() {
	}
}
最近下载更多
安布卢飞  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日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友