Redis 是一个使用 C 语言写成的,开源的 key-value 数据库。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set –有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。目前,Vmware在资助着redis项目的开发和维护。

既可作为数据库,又可作为缓存和消息代理,通常在企业中都会将其作为缓存使用。

redis基础:

Spring boot 和 myBatis集成例子见:

1.在pom.xml添加Redis依赖

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-redis</artifactId>
			<version>1.4.4.RELEASE</version>
		</dependency>

 

2.缓存注解添加

(1) 在启动类中添加缓存注解:@EnableCaching

package com.itheima.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching             //开启缓存
public class DemoApplication {
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

(2) 在业务逻辑类UserServiceImpl的getAllUsers()方法上添加注解:@Cacheable(value="UserCache",key="'user.getAllUsers'")

package com.itheima.springboot.service.impl;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itheima.springboot.mapper.UserMapper;
import com.itheima.springboot.po.User;
import com.itheima.springboot.service.UserService;

@Service
@Transactional
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapper;
	//查找所有用户
	@Cacheable(value="UserCache",key="'user.getAllUsers'")
	public List<User> getAllUsers() {
		return this.userMapper.getAllUsers();
	}

	//删除用户
	public void delete(Integer id) {
		this.userMapper.delete(id);
	}
}

3.实体类实现序列化接口(为了便于数据传输)

package com.itheima.springboot.po;
import java.io.Serializable;

public class User implements Serializable{
	private static final long serialVersionUID = 1L;
	private int id;
	private String username;
	private String address;
	
	public User(int id, String username, String address) {
		super();
		this.id = id;
		this.username = username;
		this.address = address;
	}
	public User() {		
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}	
	
}

4.指定Redis缓存主机地址

通常情况下,Redis和web应用并非部署在同一台机器上,此时就需要远程调用Redis.

(本文通过在本机搭建一个redis):

在application.properties中添加指定主机及端口:

server.port=8081

#db config
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/microservice
spring.datasource.username=root
spring.datasource.password=Changeme123
# log
logging.level.com.itheima.springboot=debug

spring.redis.host=192.168.0.100
spring.redis.port=6379

5.启动项目,测试

启动redis服务,并启动项目。

springboot redis存数据分组_redis

控制台信息为:

springboot redis存数据分组_spring_02

可以看出程序执行select并查出3条数据,在没有使用redis之前,每次刷新页面都会查询一次,使用redis后会发现刷新页面控制台只出现一次查询语句。说明配置的redis缓存已经生效。

清楚缓存:

当执行CRUD时,数据库数据变化,缓存也需要相应变化,为保证一致,需要在更新删除的时候清除缓存,并在下次查询重新缓存。

清除缓存:只需在相应方法使用CacheEvict注解。

@Service
@Transactional
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapper;
	//查找所有用户
	@Cacheable(value="UserCache",key="'user.getAllUsers'")
	public List<User> getAllUsers() {
		return this.userMapper.getAllUsers();
	}

	//删除用户
	@CacheEvict(value="UserCache",key="'user.getAllUsers'")
	public void delete(Integer id) {
		this.userMapper.delete(id);
	}
}