添加依赖到pom,xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.6.6</version>
</dependency>
配置Redis
配置数据库参数
在application.properties配置redis数据库
#RedisProperties
#redis16个库(0-15),选择使用哪一个库
spring.redis.database=11
#访问这个库的ip(我这里是本机)
spring.redis.host=localhost
#redis的端口号,默认6379
spring.redis.port=6379
编写配置类,构造RedisTemplate
package com.nowcoder.community.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.RedisSerializer;
//声明配置类
@Configuration
public class RedisConfig {
//装配第三方对象
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
// RedisConnectionFactory factory这是连接工厂,会自动 注入装配
// 实例化bean
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 把工厂设置到template
template.setConnectionFactory(factory);
// java类型的数据存到redis数据库,需要设置数据转换格式(序列化方式)
// 设置key的序列化方式
template.setKeySerializer(RedisSerializer.string());
// 设置value的序列化方式 json格式
template.setValueSerializer(RedisSerializer.json());
// 设置hash的key的序列化方式
template.setHashKeySerializer(RedisSerializer.string());
// 设置hash的value的序列化方式,json格式
template.setHashValueSerializer(RedisSerializer.json());
// 让这些设置生效需要触发一下
template.afterPropertiesSet();
return template;
}
}
访问Redis(测试类)
启动redis
package com.nowcoder.community;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.concurrent.TimeUnit;
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
public class RedisTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testStrings() {
String redisKey = "test:count";
// 存储
redisTemplate.opsForValue().set(redisKey, 1);
// 读取
System.out.println(redisTemplate.opsForValue().get(redisKey));
// 值 +1
System.out.println(redisTemplate.opsForValue().increment(redisKey));
// 值 -1
System.out.println(redisTemplate.opsForValue().decrement(redisKey));
}
@Test
public void testHashes() {
String redisKey = "test:user";
redisTemplate.opsForHash().put(redisKey, "id", 1);
redisTemplate.opsForHash().put(redisKey, "username", "zhangsan");
System.out.println(redisTemplate.opsForHash().get(redisKey, "id"));
System.out.println(redisTemplate.opsForHash().get(redisKey, "username"));
}
@Test
public void testLists() {
String redisKey = "test:ids";
redisTemplate.opsForList().leftPush(redisKey, 101);
redisTemplate.opsForList().leftPush(redisKey, 102);
redisTemplate.opsForList().leftPush(redisKey, 103);
System.out.println(redisTemplate.opsForList().size(redisKey));
// 获取index位置对应的数据
System.out.println(redisTemplate.opsForList().index(redisKey, 0));
// 按照范围index 0-2的数据(前闭区间后闭区间)
System.out.println(redisTemplate.opsForList().range(redisKey, 0, 2));
// 从左边出队
System.out.println(redisTemplate.opsForList().leftPop(redisKey));
System.out.println(redisTemplate.opsForList().leftPop(redisKey));
System.out.println(redisTemplate.opsForList().leftPop(redisKey));
}
@Test
public void testSets() {
String redisKey = "test:teachers";
redisTemplate.opsForSet().add(redisKey, "刘备", "关羽", "张飞", "赵云", "诸葛亮");
System.out.println(redisTemplate.opsForSet().size(redisKey));
// 是随机弹出
System.out.println(redisTemplate.opsForSet().pop(redisKey));
System.out.println(redisTemplate.opsForSet().members(redisKey));
}
@Test
public void testSortedSets() {
String redisKey = "test:students";
redisTemplate.opsForZSet().add(redisKey, "唐僧", 80);
redisTemplate.opsForZSet().add(redisKey, "悟空", 90);
redisTemplate.opsForZSet().add(redisKey, "八戒", 50);
redisTemplate.opsForZSet().add(redisKey, "沙僧", 70);
redisTemplate.opsForZSet().add(redisKey, "白龙马", 60);
// 统计多少个数据
System.out.println(redisTemplate.opsForZSet().zCard(redisKey));
// 查询指定key的score
System.out.println(redisTemplate.opsForZSet().score(redisKey, "八戒"));
// 统计排名 reverse 大到小
System.out.println(redisTemplate.opsForZSet().reverseRank(redisKey, "八戒"));
System.out.println(redisTemplate.opsForZSet().reverseRange(redisKey, 0, 2));
}
@Test
public void testKeys() {
// 删除
redisTemplate.delete("test:user");
//判断是否还有
System.out.println(redisTemplate.hasKey("test:user"));
//设置过期时间
redisTemplate.expire("test:students", 10, TimeUnit.SECONDS);
}
// 多次访问同一个key,批量发送命令,节约网络开销.
@Test
public void testBoundOperations() {
String redisKey = "test:count";
BoundValueOperations operations = redisTemplate.boundValueOps(redisKey);
// 累加
operations.increment();
operations.increment();
operations.increment();
operations.increment();
operations.increment();
System.out.println(operations.get());
}
// 编程式事务,不太满足ACID
@Test
public void testTransaction() {
//
Object result = redisTemplate.execute(new SessionCallback() {
@Override
public Object execute(RedisOperations redisOperations) throws DataAccessException {
String redisKey = "text:tx";
// 启用事务
redisOperations.multi();
redisOperations.opsForSet().add(redisKey, "zhangsan");
redisOperations.opsForSet().add(redisKey, "lisi");
redisOperations.opsForSet().add(redisKey, "wangwu");
System.out.println(redisOperations.opsForSet().members(redisKey));
// 提交事务
return redisOperations.exec();
}
});
System.out.println(result);
}
}