package com.wh;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import java.util.Map;
public class JedisTest {
@Test
public void test() {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.set("wh1", "wh1");
String value = jedis.get("wh1");
System.out.println(value);
jedis.hset("wh2", "name", "name");
jedis.hset("wh2", "age", "age");
Map<String, String> map = jedis.hgetAll("wh2");
System.out.println(map);
jedis.close();
}
}
package com.wh;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisDataException;
public class JedisPractice1 {
public static void main(String[] args) {
Service service1 = new Service("user1");
Service service2 = new Service("user2");
Service service3 = new Service("user3");
service1.start();
service2.start();
service3.start();
}
}
class Service extends Thread {
private String id;
public Service(String id) {
this.id = id;
}
@Override
public void run() {
Jedis jedis = new Jedis("127.0.0.1", 6379);
while (true) {
try {
String value = jedis.get(id);
if (value == null) {
jedis.setex(id, 20, Long.MAX_VALUE - 5 + "");
} else {
Long v1 = jedis.incr(id);
Thread.sleep(1000);
System.out.println("user " + id + " request..." + String.valueOf(5-(Long.MAX_VALUE-v1)));
}
} catch (JedisDataException e) {
System.out.println("user " + id + " please wait...");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
jedis.close();
}
}
}
}
info指令 查看信息
watch的key发生改变后,后续其他的所有事务都会被取消。
不能在事务里watch。
watch发生在事务之前。
高级数据类型