Java - Redis Utils
原创
©著作权归作者所有:来自51CTO博客作者peerslee的原创作品,请联系作者获取转载授权,否则将追究法律责任
RedisUtil
package redis.utils;
import java.util.List;
import redis.clients.jedis.Jedis;
public class RedisUtil {
private Jedis client = null;
public RedisUtil(String REDIS_URL) {
client = new Jedis(REDIS_URL);
}
// 右存
public void push(String listName, List<Object> inputValue) {
// 存入的是基本变量, String
inputValue.forEach(e -> client.rpush(listName, String.valueOf(e)));
}
// 左取
public List<String> pop(String listName, Integer count) {
List<String> list = null;
list = client.lrange(listName, 0, count - 1);
client.ltrim(listName, count, -1);
return list;
}
// Size
public Long queueSize(String listName) {
return client.llen(listName);
}
// 清空
public void flush() {
client.flushAll();
}
}
Demo
package redis.main;
import java.util.Arrays;
import java.util.List;
import redis.utils.RedisUtil;
public class Demo {
public static void main(String[] args) {
RedisUtil redisUtil = new RedisUtil("localhost");
List<Object> dbList = Arrays.asList("Redis", "Mongodb", "Mysql");
redisUtil.push("dbList", dbList);
List<String> resList = redisUtil.pop("dbList", 2);
resList.forEach(System.out::println);
System.out.println("size: " + redisUtil.queueSize("dbList"));
redisUtil.flush();
}
}