redis与lua脚本(redis学习十四)

版权声明:随意转载。 https://blog.csdn.net/dengjili/article/details/85202558

lua语法学习

相关语法整理:https://github.com/dengjili/spring-redis/blob/master/src/main/resources/redis学习.docx

相关代码

https://github.com/dengjili/spring-redis/tree/master/src/main/java/redis/lua

redis客户端操作lua脚本

http://www.runoob.com/redis/redis-scripting.html

redis与lua结合入门

package redis.lua;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;

import redis.clients.jedis.Jedis;

public class RedisStringTest {
	
	private static final Logger logger = LoggerFactory.getLogger(RedisStringTest.class);

	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("redis-conf/string/string-redis.xml");
		RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
		Jedis jedis = (Jedis) redisTemplate.getConnectionFactory().getConnection().getNativeConnection();
		String rtn = (String) jedis.eval("return 'hello lua redis'");
		logger.debug("redis返回值:{}", rtn);
		
		// 带参数的
		jedis.eval("redis.call('set', KEYS[1], ARGV[1])", 1, "test-key", "test-value");
		rtn = jedis.get("test-key");
		logger.debug("带参数的redis返回值:{}", rtn);
		
		// 缓存脚本
		String sha1 = jedis.scriptLoad("redis.call('set', KEYS[1], ARGV[1])");
		logger.debug("缓存脚本返回sha值:{}", sha1);
		
		jedis.evalsha(sha1, 1, "sha-key", "sha-value");
		rtn = jedis.get("sha-key");
		logger.debug("缓存脚本的redis返回值:{}", rtn);

		jedis.close();
	}

}

redis与lua操作Object

package redis.lua;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;

import redis.bean.Person;

public class RedisObjectTest {
	
	private static final Logger logger = LoggerFactory.getLogger(RedisObjectTest.class);

	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("redis-conf/string/string-redis.xml");
		RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
		
		DefaultRedisScript<Person> redisScript = new DefaultRedisScript<>();
		// 设置脚本
		redisScript.setScriptText("redis.call('set', KEYS[1], ARGV[1]) return redis.call('get', KEYS[1])");
		
		List<String> keyList = new ArrayList<>();
		keyList.add("role1");
		
		Person person = new Person();
		person.setId(1);
		person.setName("和钟娜公司1");
		
		String sha1 = redisScript.getSha1();
		logger.debug("sha值:{}", sha1);
		
		// 设置返回类型,不然拿不到值
		redisScript.setResultType(Person.class);
		
		JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
		
		Person person2 = (Person) redisTemplate.execute(redisScript, jdkSerializationRedisSerializer, jdkSerializationRedisSerializer, keyList, person);
		logger.debug("person2值:{}", person2.getName());
	}

}

redis与文件加载lua脚本

实用

package redis.lua;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;

import redis.clients.jedis.Jedis;

public class RedisFileTest {
	
	private static final Logger logger = LoggerFactory.getLogger(RedisFileTest.class);

	public static void main(String[] args) throws Exception {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("redis-conf/string/string-redis.xml");
		RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
		
		// 组件流
		InputStream inputStream = RedisFileTest.class.getClassLoader().getResourceAsStream("compare.lua");
		ByteArrayOutputStream baos = new ByteArrayOutputStream(inputStream.available());
		// 包装流
		BufferedInputStream bis = new BufferedInputStream(inputStream);
		BufferedOutputStream bos = new BufferedOutputStream(baos);
		
		byte[] buffer = new byte[1024]; 
		int len = 0;
		while ((len = bis.read(buffer)) > 0) {
			bos.write(buffer, 0, len);
			// 如果不调用该方法,数据都保持在缓存区,最终baos为空数据
			bos.flush();
		}
		// 打印文件内容
		byte[] byteArray = baos.toByteArray();
		logger.debug("{}", new String(byteArray));
		
		Jedis jedis = (Jedis) redisTemplate.getConnectionFactory().getConnection().getNativeConnection();
		byte[] sha1 = jedis.scriptLoad(byteArray);
		
		logger.debug("sha1值:{}", new String(sha1));
		
		// Object rtn = jedis.evalsha(sha1, 2, "key1".getBytes(), "key2".getBytes(), "2".getBytes(), "4".getBytes());
		Object rtn = jedis.evalsha(new String(sha1), 2, "key1", "key2", "5", "4");
		logger.debug("返回值:{}", rtn);
	}

}

猜你喜欢

转载自blog.csdn.net/dengjili/article/details/85202558