0%

Redis-入门

了解redis功能和作用,会使用Redis

目的:了解redis功能和作用,会使用Redis
参考:https://www.jianshu.com/p/56999f2b8e3b
功能:redis是内存数据库,可将数据存放在内存中。读写效率是mysql的几个量级。
作用:缓存数据,大大提升访问速度。高速读写,并发
场景:缓存热点数据,高速读写,并发场合。

Redis-入门

1.准备redis环境

1.下载

下载地址:https://github.com/MicrosoftArchive/redis/releases

版本3.0.503.zip

img

2.解压到指定位置

img

3.启动redis服务器:

解压目录中新建文件startup.cmd,内容为:redis-server redis.windows.conf

启动redis服务器:双击startup.cmd

img

img

4.测试

打开同一个文件夹下的 redis-cli.exe
输入测试语句

1
2
3
set key value

get key

结果:

1
“value”

2.java中使用

1.项目环境

我的环境 idea:2018.3.3 jdk:1.8.0_131
idea新建springboot项目

maven添加jar包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!-- Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<!--先注释掉才能使用Junit-->
<!--<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>-->
</dependency>

2.添加配置文件

application.properties中添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0

3.操作Spring

新建类ConnectRedisTest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest()
public class ConnectRedisTest {

@Autowired
private StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();

//存储String
@Test
public void string() throws Exception{

System.out.println("1");
//
stringRedisTemplate.opsForValue().set("key","value");
stringRedisTemplate.opsForValue().set("key2","value2");
Assert.assertEquals("value3",stringRedisTemplate.opsForValue().get("key"));
System.out.println("2");
}
}

4.存储对象

新建类User,注意需要实现接口Serializable。这里我还是用lombok

1
2
3
4
5
6
7
8
9
@Getter
@Setter
@ToString
public class User implements Serializable{

private String id;
private String name;

}

ConnectRedisTest中注入RedisTemplate,添加测试方法object

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 @Autowired
RedisTemplate redisTemplate = new RedisTemplate();

//存储对象
@Test
public void object() throws Exception{

System.out.println("1");
User user = new User();
user.setId("1");
user.setName("zs");
//User需实现序列化接口
redisTemplate.opsForValue().set("user",user);
System.out.println(user.toString());
}

5.操作List

ConnectRedisTest添加测试方法listlistRemove

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//List
@Test
public void list() throws Exception{
System.out.println("1");
redisTemplate.opsForList().rightPush("list","1");
redisTemplate.opsForList().rightPush("list","2");
redisTemplate.opsForList().rightPush("list","A");

redisTemplate.opsForList().leftPush("list","0");

List<String> list = redisTemplate.opsForList().range("list", 0, -1);
System.out.println(list.toString());//[0, 1, 2, A]

List<String> list2 = redisTemplate.opsForList().range("list", 0, 3);
System.out.println(list2.toString());//[0, 1, 2, A]

List<String> list3 = redisTemplate.opsForList().range("list", 1, 2);
System.out.println(list3.toString());//[0, 1, 2, A]
}

@Test
public void listRemove(){
System.out.println(redisTemplate.opsForList().range("list", 0, -1).toString());//[0, 0, 1, 2, 1, 2, 1, 2, A]
//删除第一个为A的元素
redisTemplate.opsForList().remove("list", 1, "A");
System.out.println(redisTemplate.opsForList().range("list", 0, -1));//[0, 0, 1, 2, 1, 2, 1, 2]


//删除所有的value为0的元素
redisTemplate.opsForList().remove("list", 0, "0");
System.out.println(redisTemplate.opsForList().range("list", 0, -1));//[1, 2, 1, 2, 1, 2]

}

3.windows下后台启动

1.安装成windows服务

1
redis-server --service-install redis.windows.conf

image-20201029104307873

image-20201029104407433

2.启动Redis

1
redis-server --service-start

image-20201029104515578

3.停止、卸载

1
2
redis-server --service-stop
redis-server --service-uninstall