Jedis
官方推荐的操作Redis的中间件,SpringBoot已经有整合RedisTemplate
- 导入包
- 建立连接
- 操作
- 可关闭连接,或者直接就使用jedis连接池
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
里面的API接口名和操作名一样,根据API文档使用就行
import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;
public class TestRedis {
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("hello", "world");
jsonObject.put("name", "haoyun");
Jedis localhost = new Jedis("localhost");
Transaction multi = localhost.multi();
String result = jsonObject.toJSONString();
try {
multi.set("user1", result);
multi.set("user2", result);
multi.exec();
} catch (Exception e) {
multi.discard();
e.printStackTrace();
} finally {
System.out.println(localhost.get("user1"));
System.out.println(localhost.get("user2"));
localhost.close();
}
}
}
剩下的就是自由发挥了