1.简介
Java中redis客户端有jedis、lettuce、Redission等
2.jedis的基本使用
- 引入依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.2.3</version>
</dependency>
- 从jedis连接池获取jedis对象
// resources目录下新建jedis配置参数文件
host=120.55.87.98
port=6379
password=123456
userName=null
public final class JedisConnectionFactory {
private static final JedisPool JEDIS_POOL;
private static final String host;
private static final String password;
private static final String userName;
private static final Integer port;
static {
ResourceBundle bundle = ResourceBundle.getBundle("jedis");
host = bundle.getString("host");
password = bundle.getString("password");
userName = "null".equals(bundle.getString("userName")) ? null : bundle.getString("userName");
port = Integer.parseInt(bundle.getString("port"));
JedisPoolConfig config = new JedisPoolConfig();
// 最大连接
config.setMaxTotal(10);
// 最大空闲连接
config.setMaxIdle(10);
// 最小空闲连接
config.setMinIdle(0);
// 设置最长等待时间
config.setMaxWaitMillis(200);
JEDIS_POOL = new JedisPool(config, host, port, userName, password);
}
public static Jedis getJedis() {
Jedis jedis = JEDIS_POOL.getResource();
return jedis;
}
public static void close(Jedis jedis) {
if (jedis != null) {
jedis.close();
}
}
}
- 使用jedis对象的方法,方法名和Redis的命令一样
Jedis jedis = JedisConnectionFactory.getJedis();
jedis.set("name", "李四");
String name = jedis.get("name");
System.out.println(name);
- 释放资源
jedis.close();
3.SpringDataRedis的基本使用
- SpringDataRedis是spring对redis集成的模块,其内部整合了不同的redis客户端,比如jedis、Lettuce
- 在springBoot中SpringDataRedis的简单使用:
- 导入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency>
- 在application.yml配置Redis信息
spring: redis: host: 120.55.87.98 password: 123456 lettuce: pool: max-idle: 8 #最大空闲连接 max-active: 8 #最大连接 max-wait: 100 #连接等待时间 min-idle: 0 #最小连接
- 测试
@SpringBootTest public class RedisTest { @Autowired RedisTemplate redisTemplate; @Test // 操作string类型的数据 public void testString() { redisTemplate.opsForValue().set("name", "张三"); String name = (String) redisTemplate.opsForValue().get("name"); System.out.println(name); } }
- 上述程序默认采用的是JDK序列化,其序列化结果如下图所示:缺点可读性差,且占用内存。解决方式采用其他方案进行序列化
- 修改RedisTemplate的序列化器:
- 配置如下:
// 在pom文件中添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId> </dependency> @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); // 设置连接工厂 redisTemplate.setConnectionFactory(factory); // 设置序列化器 GenericJackson2JsonRedisSerializer redisSerializer = new GenericJackson2JsonRedisSerializer(); // key和 hashKey采用 string序列化 redisTemplate.setKeySerializer(RedisSerializer.string()); redisTemplate.setHashKeySerializer(RedisSerializer.string()); // value和 hashValue采用 JSON序列化 redisTemplate.setValueSerializer(redisSerializer); redisTemplate.setHashValueSerializer(redisSerializer); return redisTemplate; } }
- 测试:
@Autowired RedisTemplate<String, Object> redisTemplate; @Test // 操作string类型的数据 public void testString() { redisTemplate.opsForValue().set("user", new User("张三", 24)); User user = (User) redisTemplate.opsForValue().get("user"); System.out.println(user); }
- 缺点:JSON序列化器会将类的class类型写入json结果中,存入Redis,会带来额外的内存开销。
- 使用StringRedisTemplate:
- 写入Redis时,手动把对象序列化为JSON
- 从Redis读取时,手动把读取到的JSON反序列化为对象
@SpringBootTest
public class RedisTest {
@Autowired
private StringRedisTemplate redisTemplate;
private static final ObjectMapper mapper = new ObjectMapper();
@Test
public void testString() throws JsonProcessingException {
User user = new User("张三", 24);
// 手动序列化
String json = mapper.writeValueAsString(user);
redisTemplate.opsForValue().set("user001", json);
String value = redisTemplate.opsForValue().get("user001");
// 反序列化
User user1 = mapper.readValue(value, User.class);
System.out.println(user1);
}
}
标签:Java,name,redis,jedis,static,序列化,public,redisTemplate,客户端
From: https://www.cnblogs.com/xiaocer/p/18188989