1. Redis 基本操作
redis官方网站:https://redis.io/ Windows资源下载地址:https://github.com/microsoftarchive/redis
- 安装完成配置好环境变量之后启动客户端
redis-cli - 默认16个库,切换库
select 1 - 刷新当前库
flushdb
1.1 String
- 存入使用 set ,单词拼接使用:不使用下划线
set test:count 1 - 取使用 get,
get test:count - 对值进行计算,加一
incr test:count 减一decr test:count
1.2 哈希
- 存数据使用 hset
hset test:user id 1 ,hset test:user username zhangsan - 取数据使用 hget
hget test:user id ,hget test:user username
1.3 列表
-
在左边存数据 lpush lpush test:ids 101 102 103 -
列表长度 llen llen test:ids -
从左访问具体索引值 lindex lindex test:ids 0 -
访问某一段数据值 lrange lrange test:ids 0 2 -
从右边弹出数据 rpop rpop test:ids
1.4 集合
-
存入数据 sadd sadd test aaa bbb ccc ddd eee -
统计个数 scard scard test -
随机弹出数据 spop spop test -
显示集合中元素 smembers smembers test
1.5 有序集合
-
存入数据 zadd zadd test1 10 aaa 20 bbb 30 ccc 40 ddd 50 eee -
查询个数 zcard zcard test1 -
查询某个元素的分数 zscore zscore test1 ccc -
返回某个值排名 zrank zrank test1 bbb -
返回某个范围的数据 zrange zrange test1 0 2
1.6 全局命令
-
查询库里一共有多少key keys * -
查询库里test开头的key keys test* -
查看某一个值的类型 type test -
查看某个 key 是否存在,存在返回1,不存在返回0 exists test -
删除某个 key,成功返回1,不成功返回0 del test -
设置某个 key 的过期时间(秒),成功返回1,不成功返回0 expire test 10
2. Spring 整合 Redis
2.1 引入依赖
不用声明版本,父pom中声明了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
如,在父pom 的 父pom 中定义了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
2.2 配置 Redis
- 在 application.properties 中声明:访问哪个库,host地址,端口号
# RedisProperties
spring.redis.database=11
spring.redis.host=localhost
spring.redis.port=6379
- 在 config 下实现 RedisConfig 类
- 注入连接工厂才能访问数据库
RedisConnectionFactory factory - 实例化 bean
new RedisTemplate<>(); - 设置工厂后有访问数据库能力
template.setConnectionFactory(factory); - 指定序列化方式(数据转化方式)
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) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
template.setKeySerializer(RedisSerializer.string());
template.setValueSerializer(RedisSerializer.json());
template.setHashKeySerializer(RedisSerializer.string());
template.setHashValueSerializer(RedisSerializer.json());
template.afterPropertiesSet();
return template;
}
}
2.3 测试访问 Redis
创建测试类 RedisTests,当前 redis 11号库里啥都没有
注入
@Autowired
private RedisTemplate redisTemplate;
2.3.1 测试 Sting
- set
- increment
- decrement
- 控制台查看输出结果
- 查看 redis 11号库内容
@Test
public void testStrings() {
String redisKey = "test:count";
redisTemplate.opsForValue().set(redisKey, 1);
System.out.println(redisTemplate.opsForValue().get(redisKey));
System.out.println(redisTemplate.opsForValue().increment(redisKey));
System.out.println(redisTemplate.opsForValue().decrement(redisKey));
}
2.3.2 测试哈希
@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"));
}
2.3.3 测试列表
@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));
System.out.println(redisTemplate.opsForList().index(redisKey, 0));
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));
}
2.3.4 测试集合
@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));
}
2.3.5 测试有序集合
@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));
System.out.println(redisTemplate.opsForZSet().score(redisKey, "八戒"));
System.out.println(redisTemplate.opsForZSet().reverseRank(redisKey, "八戒"));
System.out.println(redisTemplate.opsForZSet().reverseRange(redisKey, 0, 2));
}
2.3.6 全局命令
@Test
public void testKeys() {
redisTemplate.delete("test:user");
System.out.println(redisTemplate.hasKey("test:user"));
redisTemplate.expire("test:students", 10, TimeUnit.SECONDS);
}
2.4 多次访问同一个 key
BoundValueOperations 是访问数据类型的单词
@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());
}
2.5 编程式事务
事务内命令不会立即执行,提交后统一执行 使用编程式事务进行管理,声明式事务用的少
- 调用 redisTemplate ,方法内部做匿名实现
SessionCallback() 里方法execute 重写,内部实现事务逻辑- 启用事务
operations.multi(); - 提交事务
operations.exec();
@Test
public void testTransactional() {
Object obj = redisTemplate.execute(new SessionCallback() {
@Override
public Object execute(RedisOperations operations) throws DataAccessException {
String redisKey = "test:tx";
operations.multi();
operations.opsForSet().add(redisKey, "zhangsan");
operations.opsForSet().add(redisKey, "lisi");
operations.opsForSet().add(redisKey, "wangwu");
System.out.println(operations.opsForSet().members(redisKey));
return operations.exec();
}
});
System.out.println(obj);
}
|