1. Jar packages required by Jedis
- The Commons – the pool – 1.6. The jar
- Jedis – 2.1.0. Jar
2. Jedis common operations
1. Test connectivity
public class Demo01 {
public static void main(String[] args) {
// Connect to the local Redis service
Jedis jedis = new Jedis("127.0.0.1".6379);
// Check whether the service is running
System.out.println("connection is OK==========>: "+jedis.ping()); }}Copy the code
2. 5 + 1
① One key ② Five data types
import java.util.*;
import redis.clients.jedis.Jedis;
public class Test02
{
public static void main(String[] args)
{
Jedis jedis = new Jedis("127.0.0.1".6379);
//key
Set<String> keys = jedis.keys("*");
for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
String key = (String) iterator.next();
System.out.println(key);
}
System.out.println("jedis.exists====>"+jedis.exists("k2"));
System.out.println(jedis.ttl("k1"));
//String
//jedis.append("k1","myreids");
System.out.println(jedis.get("k1"));
jedis.set("k4"."k4_redis");
System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
jedis.mset("str1"."v1"."str2"."v2"."str3"."v3");
System.out.println(jedis.mget("str1"."str2"."str3"));
//list
System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
//jedis.lpush("mylist","v1","v2","v3","v4","v5");
List<String> list = jedis.lrange("mylist".0, -1);
for (String element : list) {
System.out.println(element);
}
//set
jedis.sadd("orders"."jd001");
jedis.sadd("orders"."jd002");
jedis.sadd("orders"."jd003");
Set<String> set1 = jedis.smembers("orders");
for (Iterator iterator = set1.iterator(); iterator.hasNext();) {
String string = (String) iterator.next();
System.out.println(string);
}
jedis.srem("orders"."jd002");
System.out.println(jedis.smembers("orders").size());
//hash
jedis.hset("hash1"."userName"."lisi");
System.out.println(jedis.hget("hash1"."userName"));
Map<String,String> map = new HashMap<String,String>();
map.put("telphone"."13811814763");
map.put("address"."atguigu");
map.put("email"."[email protected]");
jedis.hmset("hash2",map);
List<String> result = jedis.hmget("hash2"."telphone"."email");
for (String element : result) {
System.out.println(element);
}
//zset
jedis.zadd("zset01".60d."v1");
jedis.zadd("zset01".70d."v2");
jedis.zadd("zset01".80d."v3");
jedis.zadd("zset01".90d."v4");
Set<String> s1 = jedis.zrange("zset01".0, -1);
for(Iterator iterator = s1.iterator(); iterator.hasNext();) { String string = (String) iterator.next(); System.out.println(string); }}}Copy the code
3. Transaction commit
1) daily
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Response;
import redis.clients.jedis.Transaction;
public class Test03
{
public static void main(String[] args)
{
Jedis jedis = new Jedis("127.0.0.1".6379);
// Monitor the key and discard the transaction if it needs to be moved
/*3 jedis.watch("serialNum"); jedis.set("serialNum","s#####################"); jedis.unwatch(); * /
Transaction transaction = jedis.multi();// is executed as a command
Response<String> response = transaction.get("serialNum");
transaction.set("serialNum"."s002");
response = transaction.get("serialNum");
transaction.lpush("list3"."a");
transaction.lpush("list3"."b");
transaction.lpush("list3"."c");
transaction.exec();
//2 transaction.discard();
System.out.println("serialNum***********"+response.get()); }}Copy the code
(2) lock
public class TestTransaction {
public boolean transMethod(a) {
Jedis jedis = new Jedis("127.0.0.1".6379);
int balance;// Available balance
int debt;/ / in
int amtToSubtract = 10;// Actual brush quota
jedis.watch("balance");
//jedis.set("balance","5"); // This sentence should not appear, the lecture is convenient. The entry has been modified by another program
balance = Integer.parseInt(jedis.get("balance"));
if (balance < amtToSubtract) {
jedis.unwatch();
System.out.println("modify");
return false;
} else {
System.out.println("***********transaction");
Transaction transaction = jedis.multi();
transaction.decrBy("balance", amtToSubtract);
transaction.incrBy("debt", amtToSubtract);
transaction.exec();
balance = Integer.parseInt(jedis.get("balance"));
debt = Integer.parseInt(jedis.get("debt"));
System.out.println("* * * * * * *" + balance);
System.out.println("* * * * * * *" + debt);
return true; }}/** * In common sense, the watch command marks a key. If the key is marked, the transaction will fail if the key is changed by someone else before the transaction is committed. In this case, the program can usually try again. * First mark the key balance, then check whether the balance is enough, cancel the mark if it is not enough, no deduction is made; If enough, the transaction is started to update. * If the key balance is modified by someone else in the meantime, an error will be reported during the commit transaction (exec), and the program can usually catch such errors and execute again until it succeeds. * /
public static void main(String[] args) {
TestTransaction test = new TestTransaction();
boolean retValue = test.transMethod();
System.out.println("main retValue-------: "+ retValue); }}Copy the code
4. Master/slave replication
- 6379,6380 activated, individually first
- The main writing
- From reading