<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>3.7.0version>
dependency>
测试代码:
package com.test.springdataredis.com.test;
import org.junit.jupiter.api.AfterAll;
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;
@BeforeEach
void setUp(){
//建立连接
jedis = new Jedis("172.23.10.22",6379);
jedis.auth("1234");
//选择库
jedis.select(0);
}
@Test
void testHash(){
jedis.hset("test:1","name", "ljq");
jedis.hset("test:1","sex", "man");
System.out.println(jedis.hget("test:1","name"));
}
@Test
void testString(){
//存数据
String result = jedis.set("name","刘佳奇");
System.out.println("result="+result);
String name = jedis.get("name");
System.out.println("name="+name);
}
@AfterEach
void tearDown(){
if (jedis!=null){
jedis.close();
}
}
}
配置连接池代码:
package com.test.springdataredis.utils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class JedisConnectionFactory {
private static final JedisPool jedisPool;
static {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(8);//最大连接数
poolConfig.setMaxIdle(8);//最大空闲链接
poolConfig.setMinIdle(0);//最小空闲连接
poolConfig.setMaxWaitMillis(1000);//最大等待时间
jedisPool = new JedisPool(poolConfig,"172.23.10.22",6379,1000, "1234");
}
public static Jedis getJedis(){
return jedisPool.getResource();
}
}
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-pool2artifactId>
dependency>
配置信息:
spring:
redis:
host: 172.23.10.22
port: 6379
password: 1234
lettuce:
pool:
max-active: 8
max-idle: 8
min-idle: 0
max-wait: 100ms
测试代码:
package com.test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.test.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.Map;
@SpringBootTest
class RedisStringTests {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate redisTemplate;
@Test
void testString() {
// 写入一条String数据
stringRedisTemplate.opsForValue().set("name", "胡歌");
// 获取string数据
Object name = stringRedisTemplate.opsForValue().get("name");
System.out.println("name = " + name);
}
@Test
void testObject() {
// 写入一条object数据
redisTemplate.opsForValue().set("name", "胡歌");
// 获取一条object数据
Object name = stringRedisTemplate.opsForValue().get("name");
System.out.println("name = " + name);
}
}
注意:利用方法2,redisTemplate.opsForValue().set(“name”, “胡歌”);
胡歌会被当做一个对象去序列化,所以在redis库里面查到的是这样的:
package com.test.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory){
// 创建RedisTemplate对象
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 设置连接工厂
template.setConnectionFactory(connectionFactory);
// 创建JSON序列化工具
GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
// 设置Key的序列化
template.setKeySerializer(RedisSerializer.string());
template.setHashKeySerializer(RedisSerializer.string());
// 设置Value的序列化
template.setValueSerializer(jsonRedisSerializer);
template.setHashValueSerializer(jsonRedisSerializer);
// 返回
return template;
}
}
添加一个用于polo
手动序列化使用StringRedisTemplate
添加序列化等依赖:
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>2.14.2version>
<scope>testscope>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.14.2version>
<scope>testscope>
dependency>
user代码:
package com.test.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
public class User {
private String name;
private Integer age;
public User() {
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
}
我们可以直接传入object
使用RedisTemplate。会根据object类型存入redis:
get时自动反序列化对象user
测试代码:
//自动反序列化对象user
@Test
void testUser() {
redisTemplate.opsForValue().set("user:100", new User("ljq",109));
User user = (User)redisTemplate.opsForValue().get("user:100");
System.out.println("class = " + user.getClass());
System.out.println("user = " + user);
}
//
但是上述代码会多一个class的key,大量的对象可能造成卡顿
此方法常用
手动序列化代码:
@Test
void testSaveUser() throws JsonProcessingException {
// 创建对象
User user = new User("虎哥", 21);
// 手动序列化
String json = mapper.writeValueAsString(user);
// 写入数据
stringRedisTemplate.opsForValue().set("user:200", json);
// 获取数据
String jsonUser = stringRedisTemplate.opsForValue().get("user:200");
// 手动反序列化
User user1 = mapper.readValue(jsonUser, User.class);
System.out.println("user1 = " + user1);
}
另外
另外补充:
hash手动反系列化
//自动反序列化对象java.util.LinkedHashMap
@Test
void testHash() {
stringRedisTemplate.opsForHash().put("user:400", "name", "虎哥");
stringRedisTemplate.opsForHash().put("user:400", "age", "21");
Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries("user:400");//所有的键值对
System.out.println("class = " + entries.getClass());
System.out.println("entries = " + entries);
}