1. Redis
1.1 概述
1.1.1 引入
- 需要共享数据,借助于第三方软件(缓存服务器),来
存放/获取
数据。
1.1.2 什么是NoSQL
- NoSQL(NoSQL = Not Only SQL),意即“不仅仅是SQL”,是一项全新的数据库理念,泛指
非关系型
的数据库。 - 非关系型数据库,没有表结构的数据库。且一般都具有内存缓存功能。(提供性能)
1.1.3 常见产品
- Redis:键值(Key-Value)存储数据库
- MongoDB:文档型数据库
1.1.4 什么是Redis
Redis是用C语言开发的一个开源的高性能键值对(key-value)数据库。
官方提供测试数据:50个并发执行100000个请求,读的速度是110000次/s,写的速度是81000次/s 。数据仅供参考,根据服务器配置会有不同结果。
1.2 常见类型
它通过提供多种键值数据类型来适应不同场景下的存储需求。
目前为止Redis支持的键值数据类型如下:
1、 字符串类型:String 【常见】
2、 散列类型:hash
3、 列表类型:list
4、 集合类型:set
5、 有序集合类型:sorted set
- 总结:字符串类型,为常见的类型
- 场景1:值为字符串,直接存取。
- 场景2:值为Java对象,先将对象转换json字符串,再进行存取。
1.3 常见使用
1.3.1 下载
-
从官方网站下载压缩包
1.3.2 安装
-
解压到指定的位置
-
修改配置文件
maxheap 314572800
maxmemory 209715200
1.3.3 运行
- 在安装目录下,使用cmd运行
redis-server
- redis的端口号:6379
1.3.4 连接redis
- 在cmd命令窗口,运行
redis-cli
连接redis - 输入
ping
命令,将返回PONG
,表示成功了。
1.3.5 配置redis为window服务
redis-server.exe --service-install redis.windows.conf
1.3.6 配置系统环境变量path
-
redis安装在D盘,需要在其他盘符也可以使用redis的命令,需要配置系统环境变量path
1.4 常见操作:字符串操作
1.4.1 CMD端操作
-
添加数据
# 格式 set key value # 例如:key为bat、value为jack set bat jack
-
获得数据
#格式 get key # 例如: get bat
1.4.2 图形化操作
1) 下载
-
安装:
2)安装
- 安装过程
3) 使用
1.4.3 spring boot整合redis
1)添加依赖
<dependencies>
<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>
</dependency>
</dependencies>
2)配置yml文件
-
目前配置redis都是默认项,可以省略
#redis配置 spring: redis: database: 0 #数据库索引,取值0-15,表示16个库可选择 host: 127.0.0.1 #服务器地址 port: 6379 #服务器连接端口号
3)编写配置类
package com.czxy.config;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
@Component
public class RedisConfig {
@Bean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(redisConnectionFactory);
return stringRedisTemplate;
}
}
4)启动类
package com.czxy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RedisJavaMailApplication {
public static void main(String[] args) {
SpringApplication.run(RedisJavaMailApplication.class,args);
}
}
5)测试类
package com.czxy;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@SpringBootTest(classes=RedisJavaMailApplication.class)
public class TestRedis {
@Resource
private StringRedisTemplate stringRedisTemplate;
@Test
public void testSet() {
//添加数据
stringRedisTemplate.opsForValue().set("ds","杨恩召");
}
@Test
public void testGet() {
//获得数据
String ds = stringRedisTemplate.opsForValue().get("ds");
System.out.println(ds);
}
@Test
public void testDelete() {
//删除数据
stringRedisTemplate.delete("ds");
}
}