1.Jedis
以redis命令作为方法名称,学习成本低,简单使用。但是jedis实例是不安全的,多线程环境下需要基于连接池来使用。
2.Lettuce
lettuce是基于Netty实现的,支持同步,异步和响应式编程方式,并且是线程安全的。支持redis的哨兵模式、集群模式和管道模式。
3.Redisson
redisson是一个基于redis实现分布式、可伸缩的java数据结构集合,包含了诸如Map,Queue,Lock,Semphore,AtomicLong等强大功能。
Spring Data Redis 可以兼容 Jedis和Lettuce两个客户端。
Jedis官方网站:https://github.com/redis/jedis
1.引入依赖
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>3.7.0</version> </dependency>
单元测试依赖
<dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>5.7.0</version><scope>test</scope> </dependency>
2.建立连接
3.操作redis
4.释放资源
java">package com.xkj.org;import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;public class JedisTest {private Jedis jedis;@BeforeEachvoid setUp() {//建立连接jedis = new Jedis("192.168.171.130", 6379);//设置密码jedis.auth("123456");//选择库jedis.select(0);}@Testvoid testString() {String result = jedis.set("name", "虎子");System.out.println("result="+result);String name = jedis.get("name");System.out.println("name="+name);}@AfterEachvoid tearDown() {if(jedis!= null) {jedis.close();}}}
Hash类型的操作
java">@Testvoid testHash() {//插入hash数据Long res = jedis.hset("person", "name", "张三");jedis.hset("person", "age", "22");System.out.println("res="+res);//取出hash数据String name = jedis.hget("person", "name");System.out.println("name="+name);//全部取出hash数据Map<String, String> person = jedis.hgetAll("person");System.out.println("person="+person);}
jedis本身是线程不安全的,并且频繁的创建和销毁连接会有性能损耗,因此推荐使用jedis连接池代替jedis直连方式。
Jedis连接池
java">package com.xkj.org.utils;import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;public class JedisConnectionFactory {public static final JedisPool jedisPool;static {//配置连接池JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();jedisPoolConfig.setMaxTotal(8);jedisPoolConfig.setMaxIdle(8);jedisPoolConfig.setMaxWaitMillis(1000);//创建连接池对象jedisPool = new JedisPool(jedisPoolConfig, "192.168.171.130", 6379, 1000, "123456");}public static Jedis getJedis() {return jedisPool.getResource();}}
java">package com.xkj.org;import com.xkj.org.utils.JedisConnectionFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisFactory;import java.util.Map;public class JedisTest {private Jedis jedis;@BeforeEachvoid setUp() {//建立连接
// jedis = new Jedis("192.168.171.130", 6379);jedis = JedisConnectionFactory.getJedis();//设置密码jedis.auth("123456");//选择库jedis.select(0);}@Testvoid testString() {String result = jedis.set("name", "虎子");System.out.println("result="+result);String name = jedis.get("name");System.out.println("name="+name);}@Testvoid testHash() {//插入hash数据Long res = jedis.hset("person", "name", "张三");jedis.hset("person", "age", "22");System.out.println("res="+res);//取出hash数据String name = jedis.hget("person", "name");System.out.println("name="+name);//全部取出hash数据Map<String, String> person = jedis.hgetAll("person");System.out.println("person="+person);}@AfterEachvoid tearDown() {if(jedis!= null) {//如果是连接池,会将连接还回池子jedis.close();}}}