版本:
<dependency>
<groupId>redis.clients</groupId><artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
直接上源码,jedis工具类
import redis.clients.jedis.Jedis;
public class RedisUtil {
public static void setKey(String key, String value) {
Jedis jedis = new Jedis("localhost", 6379);
jedis.connect();
jedis.set(key, value);
jedis.close();
}
public static String getKey(String key) {
Jedis jedis = new Jedis("localhost", 6379);
jedis.connect();
int num = 0;
try {
while (true) {
Thread.sleep(300);
try {
String value = jedis.get(key);
if (value != null && !value.equals("")) {
jedis.del(key);
return value;
}
num++;
if (num > 100) {
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
return null;
} finally {
jedis.close();
}
}
}
使用,RedisUtil.setKey(key, data);
RedisUtil.getKey(key);