1 下载Redis
官网下载,下载 stable 版本,稳定版本。
建议下载5.0.8版本的Redis
2 本地安装
解压:tar zxvf redis-5.0.8.tar.gz
移动到: sudo mv redis-5.0.8 /usr/local/
切换到:cd /usr/local/redis-5.0.8/
编译测试 sudo make test
编译安装 sudo make install
3 Redis 的启动与停止
启动方式:直接启动 Redis: redis-server ,成功后会看到下图:
关闭方式:登陆客户端,在客户端执行 SHUTDOWN 可关闭 redis 服务,如果关闭不了就加一个参数,执行SHUTDOWN NOSAVE可关闭redis 服务,其中:
登陆客户端方式:redis-cli
设置完毕后启动命令为:redis-server redis.conf
查看是否启动成功:ps -ef | grep redis
4 客户端常用命令
命令用途
set key value
设置 key 的值
get key
获取 key 的值
exists key
查看此 key 是否存在,存在返回1,不存在返回0
keys *
查看所有的 key
del key
删除指定key,若成功则返回1,否则返回0
flushall
消除所有的 key
用法如下:
5 SpringBoot整合Redis
pom依赖:
org.springframework.boot
spring-boot-starter-redis
1.4.7.RELEASE
在配置文件application.properities中加入Redis的连接配置:
spring.redis.host=127.0.0.1spring.redis.port=6379
Redis自定义注入Bean组件配置:
package com.practice.demo.config;
import org.springframework.beans.factory.annotation.Autowired;
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.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;/**
* 配置Redis的两个操作组件:RedisTemplate & StringRedisTemplate
**/@Configurationpublic classCommonConfig {
@AutowiredprivateRedisConnectionFactory redisConnectionFactory;
@Beanpublic RedisTemplateredisTemplate(){
RedisTemplate redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(newStringRedisSerializer());
redisTemplate.setValueSerializer(newJdkSerializationRedisSerializer());
redisTemplate.setHashKeySerializer(newStringRedisSerializer());returnredisTemplate;
}
@BeanpublicStringRedisTemplate stringRedisTemplate(){
StringRedisTemplate stringRedisTemplate= newStringRedisTemplate();
stringRedisTemplate.setConnectionFactory(redisConnectionFactory);returnstringRedisTemplate;
}
}
Redis读写字符串测试Demo如下,需要注意的是,Redis除了支持字符串外,还支持列表、集合、有序集合、哈希存储等多种数据结构。
package com.practice.demo.config;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.*;
import java.util.concurrent.TimeUnit;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTestpublic classRedisTest {
@AutowiredprivateRedisTemplate redisTemplate;/********************字符串类型********************/
/**
* 读写变量*/@Testpublic voidwriteAndRead() {
String key= "redisKey";
String value= "redisValue";
ValueOperations valueOperations=redisTemplate.opsForValue();//设置数据存在的时间
valueOperations.set(key, value, 10, TimeUnit.SECONDS);
Object result= valueOperations.get(key);
System.out.println("读取的内容:" +result);
}
}
注意在启动前需要先将Redis启动,如果运行上述Demo后的结果与下面一致,说明Redis在SpringBoot中的配置正确。