文章目录
- [1、Redis 基本操作](#1、Redis 基本操作)
- [2、Redis 数据类型](#2、Redis 数据类型)
- [3、Spring Boot 整合 Redis](#3、Spring Boot 整合 Redis)
1、Redis 基本操作
Redis 默认有 16 个数据库,使用的是第 0 个,切换数据库
select 0
添加数据/修改数据
set key value
查询数据
get key
批量添加
mset k1 v1 k2 v2...
批量查询
mget k1 k2
删除数据
del key
查询所有的 key
keys *
清除当前数据库
flushdb
清除所有数据库
flushall
查看 key 是否存在
exists key
设置有效期
expire key 10
查看有效期
ttl key
2、Redis 数据类型
String
追加字符串
append key value
查看字符串长度
strlen key
自增
incr key
递减
decr key
指定递增长度
incrby k v
指定递减长度
decrby k v
字符串截取
getrange k start end
修改局部字段
setrange k start v
List
从左侧添加
lpush k v...
从右侧添加
rpush k v...
取值
lrange k start end
删除,左侧移除
lpop k
右侧移除
rpop k
通过下标获取值
lindex k index
删除集合中指定的值,count 是删除的个数
lrem k count v
通过下标修改集合中的值
lset k index v
获取长度
llen k
截取list
ltrim k start end
查看集合是否存在
exists k
Set
添加数据
sadd k v
查询数据
smembers k
判断集合中是否存在某个值
sismember k v
获取集合长度
scard k
删除元素
srem k v1 v2...
随机取值
srandmember k
Hash
存值
hset hash k1 v1 k2 v2
取值
hget hash k1
存多个值
hmset hash k1 a k2 b k3 c
取多个值
hmget hash k1 k2 k3
取所有值
hgetall hash
删除数据
hdel hash k1 k2
获取长度
hlen k
判断集合中是否存在某个值
hexists hahs k
获取集合中所有 key
hkeys hash
获取集合中所有 value
hvals hash
Zset
添加数据
zadd set index v
查询数据
zrange set 0 -1
升序查询
zrangebyscore score -inf +inf withscores
降序查询
zrevrange score 0 -1 withscores
删除数据
zrem score jack
3、Spring Boot 整合 Redis
Spring Data Redis
xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
yaml
spring:
redis:
database: 0
host: 192.168.248.138
port: 6379
java
package com.southwind.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student implements Serializable {
private Integer id;
private String name;
private Double score;
private Date birthday;
}
java
package com.southwind.controller;
import com.southwind.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
@RestController
public class StudentController {
@Autowired
private RedisTemplate redisTemplate;
@PostMapping("/set")
public void set(@RequestBody Student student){
this.redisTemplate.opsForValue().set("stu", student);
}
@GetMapping("/get/{key}")
public Student get(@PathVariable("key") String key){
return (Student) this.redisTemplate.opsForValue().get(key);
}
@PutMapping("/put")
public void update(@RequestBody Student student){
this.redisTemplate.opsForValue().set("stu", student);
}
@DeleteMapping("/delete/{key}")
public Boolean delete(@PathVariable("key") String key){
this.redisTemplate.delete(key);
return this.redisTemplate.hasKey(key);
}
}
java
package com.southwind.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.southwind"))
.build().apiInfo(new ApiInfoBuilder()
.title("Redis测试")
.description("测试")
.version("V1.0")
.build());
}
}
字符串
java
@PostMapping("/string")
public String string(){
String str = "Hello World";
this.redisTemplate.opsForValue().set("str", str);
return (String) this.redisTemplate.opsForValue().get("str");
}
List
java
@PostMapping("/list")
public void list(){
ListOperations<String,String> list = redisTemplate.opsForList();
list.leftPush("list", "Hello");
list.leftPush("list", "World");
list.leftPush("list","Java");
list.rightPush("list", "1");
list.rightPush("list", "2");
list.rightPush("list", "3");
}
Set
java
@PostMapping("/setadd")
public void setadd(){
SetOperations<String,String> set = this.redisTemplate.opsForSet();
set.add("set", "Hello");
set.add("set", "World");
set.add("set", "Java");
}
Zset
java
@PostMapping("/zset")
public void zset(){
ZSetOperations<String,String> set = this.redisTemplate.opsForZSet();
set.add("zset", "Hello",1);
set.add("zset", "World",2);
set.add("zset", "Java",3);
}
Hash
java
@PostMapping("/hash")
public void hash(){
HashOperations<String,String,String> hash = this.redisTemplate.opsForHash();
hash.put("hash", "id", "1");
hash.put("hash", "name", "tom");
hash.put("hash", "age","22" );
}