安装redis
1、检查安装依赖程序
yum install gcc-c++
yum install -y tcl
yum install wget
2、获取安装文件
wget http://download.redis.io/releases/redis-4.0.7.tar.gz
3、解压文件
自己新建一个目录将redis解压到里面
tar -zxvf redis-4.0.7.tar.gz
mv redis-4.0.7 /usr/local/redis
4、进入目录
cd /usr/local/redis
5、编译安装
make
make install
6、设置配置文件路径
mkdir -p /etc/redis
cp redis.conf /etc/redis
7、修改配置文件
redis.conf是redis的配置文件,redis.conf在redis源码目录。
注意修改port作为redis进程的端口,port默认6379。
redis有两种启动方式
01:直接运行bin/redis-server将以前端模式启动,前端模式启动的缺点是ssh命令窗口关闭则redis-server程序结束,不推荐使用此方法。
02:后端模式启动
修改redis.conf配置文件, daemonize yes 以后端模式启动
打开redis.conf,使用命令 ? daemonize 快速查找到daemonize然后修改。
vi /etc/redis/redis.conf
仅修改: daemonize yes (no–>yes)
8、启动
/usr/local/bin/redis-server /etc/redis/redis.conf 启动服务
9、查看启动
ps -ef | grep redis
10、使用客户端
redis-cli 启动客户端
set name zmfx
OK
get name
“zmfx”
11.关闭客户端
redis-cli shutdown
12、开机启动配置
echo “/usr/local/bin/redis-server /etc/redis/redis.conf &” >> /etc/rc.local
开机启动要配置在 rc.local 中,而 /etc/profile 文件,要有用户登录了,才会被执行。
13、设置密码
因为这是给局域网内的很多人使用,所以设置一个访问密码很有必要。
修改redis.conf文件配置
使用命令 :/ requirepass 快速查找到 # requirepass foobared 然后去掉注释,这个foobared改为自己的密码。然后wq保存。
14、重启redis
redis-cli -h 127.0.0.1 -p 6379 -a password
redis 127.0.0.1:6379> keys *
- “myset”
- “mysortset”
redis 127.0.0.1:6379> select 1
OK
与springboot进行结合
pom.xml文件
<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-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
application.properties文件
server.port=8081
#数据库连接
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/user?useUnicode=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=zhangxian11
#mybatis配置
mybatis.type-aliases-package=com.example.redisdemo.pojo
mybatis.mapper-locations=classpath:mapper/*.xml
#mybatis的下划线转驼峰配置
#mybatis.configuration.map-underscore-to-camel-case=true
#另外一种打印语句的方式
#mybatis.configuration.log-impl=org.appache.ibatis.logging.stdout.StdOutImpl
#将themilef的默认缓存禁用,热加载生效
spring.thymeleaf.cache=false
#启用分页插件[或者程序中进行配置]
#pagehelper.helper-dialect=mysql
#pagehelper.reasonable=true
## Redis 配置
## Redis数据库索引(默认为0)
spring.redis.database=0
## Redis服务器地址
spring.redis.host=172.16.238.161
## Redis服务器连接端口
spring.redis.port=6379
## Redis服务器连接密码(默认为空)
spring.redis.password=
## 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-idle=8
## 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=100
## 连接池中的最大空闲连接
## 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
## 连接超时时间(毫秒)
spring.redis.timeout=1200
实体类
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private int uid;
private String userName;
private String passWord;
private int salary;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public User(int uid, String userName, String passWord, int salary) {
super();
this.uid = uid;
this.userName = userName;
this.passWord = passWord;
this.salary = salary;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
}
service接口
public interface UserService {
//查询所以数据
List<User> queryAll();
//查询id为条件的数据
User findUserById(int id);
//修改数据
int updateUser(User user);
//删除数据
int deleteUserById(int id);
}
serviceImpl实现类
@Service
public class UserServiceImpl implements UserService {
/**
* Ω
*/
@Autowired
private UserDao userDao;
@Autowired
private RedisTemplate redisTemplate;
/*
*获取所以数据存入到一个key中,可以设定设定一个定时任务让数据不断的能够得到更新
*/
public List<User> queryAll() {
String key="userlist";
ValueOperations<String,List<User>> operations=redisTemplate.opsForValue();
boolean haskey=redisTemplate.hasKey(key);
if(haskey){
List<User> list=operations.get(key);
System.out.println("==========从缓存中获得数据=========");
return list;
}else {
List<User> list=userDao.queryAll();
System.out.println("==========从数据表中获得数据=========");
operations.set(key,list,5,TimeUnit.HOURS);
return list;
}
}
/**
* 获取用户:先从缓存中获取用户,没有则取数据表中 数据,再将数据写入缓存
*/
public User findUserById(int id) {
String key = "user_" + id;
ValueOperations<String, User> operations = redisTemplate.opsForValue();
boolean hasKey = redisTemplate.hasKey(key);
if (hasKey) {
User user = operations.get(key);
System.out.println("==========从缓存中获得数据=========");
System.out.println(user.getUserName());
System.out.println("==============================");
return user;
} else {
User user = userDao.findUserById(id);
System.out.println("==========从数据表中获得数据=========");
System.out.println(user.getUserName());
System.out.println("==============================");
// 写入缓存
operations.set(key, user, 5, TimeUnit.HOURS);
return user;
}
}
/**
* 更新用户:先更新数据表,成功之后,删除原来的缓存,再更新缓存
*/
public int updateUser(User user) {
ValueOperations<String, User> operations = redisTemplate.opsForValue();
int result = userDao.updateUser(user);
if (result != 0) {
String key = "user_" + user.getUid();
boolean haskey = redisTemplate.hasKey(key);
if (haskey) {
redisTemplate.delete(key);
System.out.println("删除缓存中的key=========>" + key);
}
// 再将更新后的数据加入缓存
User userNew = userDao.findUserById(user.getUid());
if (userNew != null) {
operations.set(key, userNew, 3, TimeUnit.HOURS);
}
}
return result;
}
/**
* 删除用户:删除数据表中数据,然后删除缓存
*/
public int deleteUserById(int id) {
int result = userDao.deleteUserById(id);
String key = "user_" + id;
if (result != 0) {
boolean hasKey = redisTemplate.hasKey(key);
if (hasKey) {
redisTemplate.delete(key);
System.out.println("删除了缓存中的key:" + key);
}
}
return result;
}
Controller类
@Controller
@RequestMapping("/test")
public class UserController {
@Autowired
private UserService userService;
@ResponseBody
@RequestMapping("/testController")
public String testController(){
return "success";
}
@ResponseBody
@RequestMapping("/queryAll")
public List<User> queryAll(){
List<User> lists = userService.queryAll();
return lists;
}
@ResponseBody
@RequestMapping("/findUserById")
public Map<String, Object> findUserById(@RequestParam int id){
User user = userService.findUserById(id);
Map<String, Object> result = new HashMap<>();
result.put("uid", user.getUid());
result.put("uname", user.getUserName());
result.put("pass", user.getPassWord());
result.put("salary", user.getSalary());
return result;
}
@ResponseBody
@RequestMapping("/updateUser")
public String updateUser(){
User user=new User();
// user.setUid(1);
// user.setUserName("cat");
// user.setPassWord("miaomiao");
// user.setSalary(4000);
int result = userService.updateUser(user);
if(result != 0){
return "update user success";
}
return "fail";
}
@ResponseBody
@RequestMapping("/deleteUserById")
public String deleteUserById(@RequestParam int id){
int result = userService.deleteUserById(id);
if(result != 0){
return "delete success";
}
return "delete fail";
}
}