官方提倡使用Linux版的Redis,所以官网值提供了Linux版的Redis下载,但我们可以从GitHub上下载window版的Redis,具体链接地址如下:
-
官网下载地址:http://redis.io/download
-
github下载地址:https://github.com/MSOpenTech/redis/tags
-
从github进去后点击downloads
解压即安装
启动redis服务
启动redis客户端
安装redis服务
进入redis文件夹,打开命令窗口,执行以下命令
redis-server --service-install redis.windows.conf --service-name redis6379 --loglevel verbose
安装后需要打开服务列表并启动服务
删除redis 服务
sc 命令删除服务即可(以管理员权限删除)
sc delete redis6379
springboot整合redis代码如下
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-redis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>8</java.version>
</properties>
<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>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
application.yml
spring:
redis:
host: localhost
port: 6379
password:
database: 0
jedis:
pool:
#最大连接数
max-active: 8
#最大阻塞等待时间(负数表示没限制)
max-wait: -1ms
#最大空闲
max-idle: 8
#最小空闲
min-idle: 0
#连接超时时间
timeout: 3000ms
@Repository
public class RedisDao {
@Autowired
private StringRedisTemplate template;
public void setKey(String key,String value){
template.opsForValue().set(key,value,1,TimeUnit.MINUTES); //1分钟过期
}
public String getKey(String key){
return template.opsForValue().get(key);
}
}
测试
@SpringBootTest
class SpringRedisApplicationTests {
@Test
void contextLoads() {
}
@Autowired
private RedisDao redisDao;
@Test
public void testRedisDao() {
redisDao.setKey("key","value");
String value = redisDao.getKey("key");
System.out.println(value);
}
}