引依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.7.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
配置文件中书写相关配置
# Redis 相关配置
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 # 连接等待时间
为了节省内存空间,我们并不会使用JSON序列化器来处理value,而是统一使用String序列化器,要求只能存储String类型的key和value。当需要存储Java对象时,手动完成对象的序列化和反序列化。
JSON处理需要此依赖,假如引入了SpringMVC就不需要手动引入此依赖了,因为SpringMVC自带了此依赖
<!--Jackson依赖-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
????SpringDataRedis中提供了RedisTemplate工具类,其中封装了各种对Redis的操作????RedisTemplate将不同数据类型的操作封装到了不同的类型中:
SpringDataRedis有以下特点:????提供了对不同Redis客户端的整合????提供了RedisTemplate统一API来操作Redis????支持Redis的发布订阅模型????支持Redis哨兵和Redis集群????支持基于Lettuce的响应式编程????支持基于JDK、JSON、字符串、Spring对象的数据序列化及反序列化????支持基于Redis的JDKCollection实现
RedisTemplate的默认序列化方式
????SpringData是Spring中数据操作的模块,包含对各种数据库的集成,其中对Redis的集成模块叫做SpringDataRedis
RedisTemplate可以接收任意Object作为值写入Redis,只不过写入前会把Object序列化为字节形式,默认是采用JDK序列化,得到的结果如下所示:可读性差内存占用较大
使用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() {
// 写入一条 String 数据
redisTemplate.opsForValue().set("love", "爱");
// 获取 String 数据
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"));
}
}
Redis连接池
????Jedis实例是线程不安全的????多线程环境下需要基于连接池来使用????这里使用的连接池技术是Jedis提供的
/**
* 返回 Redis 连接的工厂类
*/
@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);
// 最长等待时间(单位:ms)
config.setMaxWaitMillis(666);
JEDIS_POOL = new JedisPool(config, HOST, PORT, 1000, PWD);
}
public static Jedis getJedisConnection() {
return JEDIS_POOL.getResource();
}
}
连接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();
}
}
引入依赖
<!-- Redis 依赖 -->
<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>
自定义RedisTemplate的序列化方式
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
// 创建 Template
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
// 设置连接工厂
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 设置序列化工具
GenericJackson2JsonRedisSerializer jsonSerializer = new GenericJackson2JsonRedisSerializer();
// key 和 hashKey 使用 String 序列化
redisTemplate.setKeySerializer(RedisSerializer.string());
redisTemplate.setHashKeySerializer(RedisSerializer.string());
// value 和 hashValue 使用 JSON 序列化
redisTemplate.setValueSerializer(jsonSerializer);
redisTemplate.setHashValueSerializer(jsonSerializer);
return redisTemplate;
}
}
Jedis客户端
????JedisPool:Redis连接池????JedisPoolConfig:Redis连接池的配置
文章为作者独立观点,不代表股票交易接口观点