零、Redis 的 Java 客户端有哪些?
; 二、Jedis 客户端
Jedis 的使用:https://redis.io/docs/clients/java/
(1) 引依赖
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>3.7.0version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13.2version>
dependency>
(2) 连接 Redis 服务并测试
public class testJedis {
private Jedis jedis;
private static final String HOST = "192.168.88.130";
private static final String PWD = "root";
private static final int PORT = 6379;
@Before
public void setUp() {
jedis = new Jedis(HOST, PORT);
jedis.auth(PWD);
jedis.select(0);
}
@Test
public void testString() {
String result = jedis.set("name", "张国庆");
System.out.println("result = " + result);
String name = jedis.get("name");
System.out.println("name = " + name);
}
@After
public void tearDown() {
if (jedis != null) jedis.close();
}
}
(3) Redis 连接池
🌼 Jedis 实例是线程不安全的 🌼 多线程环境 下需要基于连接池来使用 🌼 这里使用的连接池技术是 Jedis 提供的
@SuppressWarnings("all")
public class JedisConnectionFactory {
private static final int PORT = 6379;
private static final String HOST = "192.168.88.130";
private static final String PWD = "root";
private static final JedisPool JEDIS_POOL;
static {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(8);
config.setMaxIdle(8);
config.setMinIdle(0);
config.setMaxWaitMillis(666);
JEDIS_POOL = new JedisPool(config, HOST, PORT, 1000, PWD);
}
public static Jedis getJedisConnection() {
return JEDIS_POOL.getResource();
}
}
🍀 JedisPool:Redis 连接池 🍀 JedisPoolConfig:Redis 连接池的配置
三、SpringDataRedis 介绍
🍃 SpringData是 Spring 中数据操作的模块,包含对各种数据库的集成,其中对 Redis 的集成模块叫做 SpringDataRedis
🍃 官网地址:spring.io/projects/sp…
SpringDataRedis 有以下特点:
😀 提供了对不同 Redis 客户端的整合( Lettuce 和 Jedis)
😀 提供了 RedisTemplate 统一 API 来操作 Redis
😀 支持 Redis 的发布订阅模型
😀 支持 Redis 哨兵和 Redis 集群
😀 支持基于 Lettuce 的响应式编程
😀 支持基于 JDK、JSON、字符串、Spring 对象的数据序列化及反序列化
😀 支持基于 Redis 的 JDKCollection 实现
🌿 SpringDataRedis 中提供了 RedisTemplate 工具类,其中封装了各种对 Redis 的操作
🌿 RedisTemplate 将不同 数据类型(String、List、Set、SortedSet、Hash)的操作封装到了不同的类型中:
; 四、SpringBoot 中集成 SpringDataRedis
(1) 引入依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-pool2artifactId>
dependency>
(2) 配置文件中书写相关配置
spring:
redis:
host: 192.168.88.130
port: 6379
password: root
lettuce:
pool:
max-active: 8
max-idle: 8
min-idle: 0
max-wait: 100
(3) RedisTemplate 的默认序列化方式
- RedisTemplate 可以接收任意 Object 作为值写入 Redis,只不过写入前会把 Object 序列化为字节形式,默认是采用 JDK 序列化,得到的结果如下所示:
- 可读性差
- 内存占用较大
; (4) 自定义 RedisTemplate 的序列化方式
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
GenericJackson2JsonRedisSerializer jsonSerializer = new GenericJackson2JsonRedisSerializer();
redisTemplate.setKeySerializer(RedisSerializer.string());
redisTemplate.setHashKeySerializer(RedisSerializer.string());
redisTemplate.setValueSerializer(jsonSerializer);
redisTemplate.setHashValueSerializer(jsonSerializer);
return redisTemplate;
}
}
JSON 处理需要此依赖,假如引入了 SpringMVC 就不需要手动引入此依赖了,因为 SpringMVC 自带了此依赖
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
dependency>
(5) 使用 RedisTemplate 操作 Redis 数据库
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
private Long id;
private String name;
private short age;
private Date birthday;
}
@SpringBootTest
@SpringBootTest
class ApplicationTest {
@Resource
private RedisTemplate<String, Object> redisTemplate;
@Test
void testString() {
redisTemplate.opsForValue().set("love", "爱");
Object love = redisTemplate.opsForValue().get("love");
System.out.println("love = " + love);
}
@Test
void testSaveObject() {
Person p1 = new Person(1L, "陈铭酒", 25, new Date());
Person p2 = new Person(2L, "厉尘澜", 22, new Date());
redisTemplate.opsForValue().set("crm:person:1", p1);
redisTemplate.opsForValue().set("crm:person:2", p2);
System.out.println(redisTemplate.opsForValue().get("crm:person:1"));
System.out.println(redisTemplate.opsForValue().get("crm:person:2"));
}
}
为了在反序列化时知道对象的类型,JSON 序列化器会将类的class 类型写入 json 结果中并存入 Redis,会带来额外的内存开销(Redis 中会存入无关的内容)
为了节省内存空间,我们并不会使用 JSON 序列化器来处理value,而是统一使用 String 序列化器,要求只能存储 String 类型的 key 和 value。当需要存储 Java 对象时,手动完成对象的序列化和反序列化。
(6) StringRedisTemplate
- Spring 默认提供了一个 StringRedisTemplate 类
- 它的 key 和 value 的序列化方式默认就是 String
@SpringBootTest
class ApplicationTestStringRedisTemplate {
@Resource
private StringRedisTemplate stringRedisTemplate;
@Test
void testString() {
stringRedisTemplate.opsForValue().set("cmj", "陈铭酒");
Object cmj = stringRedisTemplate.opsForValue().get("cmj");
System.out.println("cmj = " + cmj);
}
@Resource
private ObjectMapper objectMapper;
@Test
void testSaveObject() throws JsonProcessingException {
Person p = new Person(3L, "刘德华", 33, new Date());
String pStr = objectMapper.writeValueAsString(p);
stringRedisTemplate.opsForValue().set("crm:person:3", pStr);
String pStrFrom = stringRedisTemplate.opsForValue().get("crm:person:3");
Person person = objectMapper.readValue(pStrFrom, Person.class);
System.out.println("person = " + person);
}
}